txt文件里有30组学生信息,但读取得时候只能读29个,第一位同学的读不了
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;
const int MAX_STUDENT_NUM = 100;
string file_path; // 存储读入文件的路径
int student_num = 0; // 记录读入的学生数量
struct Student
{
string id;
string name;
int chinese;
int math;
int english;
double peer_score;
double morality_score;
double teacher_score;
double average;
int rank;
double total_score;
int total_rank;
} studentss[MAX_STUDENT_NUM];
bool compare_averages(Student a, Student b)
{
if (a.average == b.average)
{
return a.id < b.id;
}
return a.average > b.average;
}
bool compare_total_scores(Student a, Student b)
{
if (a.total_score == b.total_score)
{
return a.id < b.id;
}
return a.total_score > b.total_score;
}
void read_students()
{
ifstream input_file(file_path);
if (!input_file.is_open())
{
cout << "无法打开文件!" << endl;
return;
}
string line;
getline(input_file, line);
while (getline(input_file, line))
{
stringstream ss(line);
ss >> studentss[student_num].id >> studentss[student_num].name >> studentss[student_num].chinese >> studentss[student_num].math >> studentss[student_num].english >> studentss[student_num].peer_score >> studentss[student_num].morality_score >> studentss[student_num].teacher_score;
student_num++;
}
input_file.close();
cout << "成功读入 " << student_num << " 名学生的信息!" << endl;
}
void calc_exam_score_and_rank()
{
for (int i = 0; i < student_num; i++)
{
studentss[i].average = (studentss[i].chinese + studentss[i].math + studentss[i].english) / 3.0;
}
sort(studentss, studentss + student_num, compare_averages);
for (int i = 0; i < student_num; i++)
{
if (i == 0)
{
studentss[i].rank = 1;
}
else if (studentss[i].average == studentss[i - 1].average)
{
studentss[i].rank = studentss[i - 1].rank;
}
else
{
studentss[i].rank = i + 1;
}
}
}
void calc_total_score_and_rank()
{
for (int i = 0; i < student_num; i++)
{
studentss[i].total_score = studentss[i].average * 0.6 + studentss[i].peer_score * 0.1 + studentss[i].morality_score * 0.1 + studentss[i].teacher_score * 0.2;
}
sort(studentss, studentss + student_num, compare_total_scores);
for (int i = 0; i < student_num; i++)
{
if (i == 0)
{
studentss[i].total_rank = 1;
}
else if (studentss[i].total_score == studentss[i - 1].total_score)
{
studentss[i].total_rank = studentss[i - 1].total_rank;
}
else
{
studentss[i].total_rank = i + 1;
}
}
}
void query_student_info()
{
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < student_num; i++)
{
if (studentss[i].id == id)
{
cout << "学号:" << studentss[i].id << endl;
cout << "姓名:" << studentss[i].name << endl;
cout << "语文成绩:" << studentss[i].chinese << endl;
cout << "数学成绩:" << studentss[i].math << endl;
cout << "英语成绩:" << studentss[i].english << endl;
cout << "平均分:" << studentss[i].average << endl;
cout << "排名:" << studentss[i].rank << endl;
cout << "同学互评分:" << studentss[i].peer_score << endl;
cout << "品德成绩:" << studentss[i].morality_score << endl;
cout << "任课教师评分:" << studentss[i].teacher_score << endl;
cout << "综合测评总分:" << studentss[i].total_score << endl;
cout << "综合测评名次:" << studentss[i].total_rank << endl;
found = true;
break;
}
}
if (!found)
{
cout << "没有找到对应学生信息!" << endl;
}
}
void modify_student_info()
{
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < student_num; i++)
{
if (studentss[i].id == id)
{
cout << "请输入新的语文成绩:";
cin >> studentss[i].chinese;
cout << "请输入新的数学成绩:";
cin >> studentss[i].math;
cout << "请输入新的英语成绩:";
cin >> studentss[i].english;
cout << "请输入新的同学互评分:";
cin >> studentss[i].peer_score;
cout << "请输入新的品德成绩:";
cin >> studentss[i].morality_score;
cout << "请输入新的任课教师评分:";
cin >> studentss[i].teacher_score;
studentss[i].average = (studentss[i].chinese + studentss[i].math + studentss[i].english) / 3.0;
calc_exam_score_and_rank();
calc_total_score_and_rank();
found = true;
break;
}
}
if (!found)
{
cout << "没有找到对应学生信息!" << endl;
}
}
void delete_student_info()
{
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
for (int i = 0; i < student_num; i++)
{
if (studentss[i].id == id)
{
for (int j = i; j < student_num - 1; j++)
{
studentss[j] = studentss[j + 1];
}
student_num--;
cout << "学生信息删除成功!" << endl;
return;
}
}
cout << "没有找到要删除的学生信息!" << endl;
}
void export_student_info()
{
string file_path;
cout << "请输入要导出学生成绩的文件路径:";
cin >> file_path;
ofstream output_file(file_path);
if (!output_file.is_open())
{
cout << "无法打开文件!" << endl;
return;
}
output_file << "学号\t姓名\t语文\t数学\t英语\t平均分\t排名\t互评分\t品德\t教师评分\t总分\t总排名\n";
for (int i = 0; i < student_num; i++)
{
output_file << studentss[i].id << "\t" << studentss[i].name << "\t" << studentss[i].chinese << "\t" << studentss[i].math << "\t" << studentss[i].english << "\t" << studentss[i].average << "\t" << studentss[i].rank << "\t" << studentss[i].peer_score << "\t" << studentss[i].morality_score << "\t" << studentss[i].teacher_score << "\t" << studentss[i].total_score << "\t" << studentss[i].total_rank << "\n";
}
output_file.close();
cout << "学生成绩已成功导出到文件 " << file_path << endl;
}
void bSystem()
{
cout << "进入学生数据信息处理系统" << endl;
read_students();
calc_exam_score_and_rank();
calc_total_score_and_rank();
int selectA = 0;
while (selectA != 5)
{
cout << endl;
cout << "1. 查询数据 " << endl;
cout << "2. 修改数据" << endl;
cout << "3. 删除数据" << endl;
cout << "4. 导出学生成绩" << endl;
cout << "5. 退出子系统" << endl;
cout << endl << endl;
cin >> selectA;
cout << "您输入的是 " << selectA << endl;
if (selectA == 1)
{
query_student_info();
}
else if (selectA == 2)
{
modify_student_info();
}
else if (selectA == 3)
{
delete_student_info();
}
else if (selectA == 4)
{
export_student_info();
}
else if (selectA == 5)
{
break;
}
else
{
cout << "错误的输入,请重新输入!" << endl;
}
}
}
int main()
{
cout << "请输入学生信息文件的路径:";
cin >> file_path;
bSystem();
return 0;
}