m0_74749779 2023-06-05 23:41 采纳率: 50%

# 结构体数组读取文件信息失败，读取不了

#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;
}
// 读入学生信息
{
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 读取学生信息*/
//                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();
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

}

• 写回答

#### 2条回答默认 最新

• 喝茶品人生 2023-06-06 00:56
关注

对你的代码修改了很多，原本的txt可以私信给我

#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;
}

{
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;

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;
}

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

• 系统已结题 6月14日
• 已采纳回答 6月6日
• 修改了问题 6月5日
• 创建了问题 6月5日

#### 悬赏问题

• ¥15 WPF动态创建页面内容
• ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验，如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
• ¥15 SQL数据库操作问题
• ¥100 关于lm339比较电路出现的问题
• ¥15 Matlab安装yalmip和cplex功能安装失败
• ¥15 加装宝马安卓中控改变开机画面
• ¥15 STK安装问题问问大家，这种情况应该怎么办
• ¥15 关于罗技鼠标宏lua文件的问题
• ¥15 halcon ocr mlp 识别问题
• ¥15 已知曲线满足正余弦函数，根据其峰值，还原出整条曲线