代码读取不了文件结构体数组读不了txt文件里的信息。希望有会的能帮忙改一下,不要用类与对象,指针这些因为还没学看不懂。谢谢!
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <cassert>
#include<vector>
using namespace std;
// 学生结构体
struct Student {
string id; // 学号
string name; // 姓名
double chinese; // 语文成绩
double math; // 数学成绩
double english; // 英语成绩
double average; // 考试成绩
int rank; // 考试名次
double peer_score; // 同学互评分
double morality_score; // 品德成绩
double teacher_score; // 任课教师评分
double total_score; // 综合测评总分
int total_rank; // 综合测评名次
};
const int MAX_STUDENT_NUM = 1000;
Student studentss[MAX_STUDENT_NUM]; // 学生数组
int student_num = 0; // 学生数
const string filePath2 = "information2.txt";
// 按照从大到小的顺序比较学生成绩,用于排序
bool compare_averages(const Student& s1, const Student& s2)
{
return s1.average > s2.average;
}
// 按照从大到小的顺序比较学生总分,用于排序
bool compare_total_scores(const Student& s1, const Student& s2)
{
return s1.total_score > s2.total_score;
}
void getline()
{
int count = 0;
char line[128];
fstream information2_file;
information2_file.open(filePath2, ios::in);
while (!information2_file.eof())
{
information2_file.getline(line, sizeof(line));
count++;
}
information2_file.close();
student_num = count;
}
// 读入学生信息
void read_students()
{
ifstream information2_file; // 打开文件
// assert(fin.is_open());
information2_file.open(filePath2, ios::in);
int i = 0;
while (i<student_num)
{
information2_file>>studentss[i].id >> studentss[i].name >> studentss[i].chinese >> studentss[i].math >> studentss[i].english >> studentss[i].peer_score >> studentss[i].morality_score >> studentss[i].teacher_score;
i++;
}
information2_file.close();//关闭文件
sort(studentss, studentss +student_num);
}
// 计算学生考试成绩和名次
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()
{ int count;
string id;
cout << "请输入学号:";
cin >> id;
bool found = false;
for (int i = 0; i < count; 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(Student studentss[], int count)
{
for (int i = 0; i < count; i++)
{
string id;
cout << "请输入学号:";
cin >> id;
bool found = false;
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;
}
}
}
void bSystem()
{
cout << "进入学生数据信息处理系统" << endl;
/* step 1 读取学生信息*/
read_students(); // 初始化全局学生数组 studentss
// for (int i = 0; i < 10; ++i) {
// cout<<studentss[i].id<<studentss[i].name<<endl;
// }
int selectA = 0;
while (selectA != 3)
{
cout << endl;
/* step 2 增删改查*/
cout << "1. 查询数据 " << endl;
cout << "2. 修改数据" << endl;
cout << "3. 退出子系统" << endl;
cout << endl << endl;// 输入
cin >> selectA;
cout << "您输入的是 " << selectA << endl;
// Student studentss[MAX_STUDENT_NUM]; // 学生数组
getline();
read_students();
if (selectA == 1)
{
query_student_info();
}
else if (selectA == 2)
{
modify_student_info(studentss, 1);
}
else if (selectA == 3)
{
break;
}
else
{
cout << "错误的输入,请重新输入!" << endl;
}
}
}
int main()
{
bSystem();
//1
}