#include <iostream> #include <fstream> #include <algorithm> using namespace std; struct Student { int id; string name; string className; double score1, score2, score3; double average; }; bool cmp(Student a, Student b) { return a.average > b.average; //按照平均成绩从高到低排序 } int main() { Student students[3]; //输入学生信息 for (int i = 0; i < 3; i++) { cout << "请输入第" << i+1 << "名学生的学号、姓名、班级信息、3门课程成绩(用空格隔开):" << endl; cin >> students[i].id >> students[i].name >> students[i].className >> students[i].score1 >> students[i].score2 >> students[i].score3; students[i].average = (students[i].score1 + students[i].score2 + students[i].score3) / 3; } //将学生信息输出到文件f1中 ofstream fout1("f1.txt"); if (!fout1) { cout << "文件打开失败!" << endl; return 0; } for (int i = 0; i < 3; i++) { fout1 << students[i].id << " " << students[i].name << " " << students[i].className << " "; fout1 << students[i].score1 << " " << students[i].score2 << " " << students[i].score3 << " "; fout1 << students[i].average << endl; } fout1.close(); //按照平均成绩从高到低排序 sort(students, students+3, cmp); //将学生信息输出到文件f2中 ofstream fout2("f2.txt"); if (!fout2) { cout << "文件打开失败!" << endl; return 0; } for (int i = 0; i < 3; i++) { fout2 << students[i].id << " " << students[i].name << " " << students[i].className << " "; fout2 << students[i].score1 << " " << students[i].score2 << " " << students[i].score3 << " "; fout2 << students[i].average << endl; } fout2.close(); cout << "学生信息已经成功输出到文件f1和f2中!" << endl; return 0; } 基于这个案例简单加其他功能或者改写,写出改写后的完整代码
时间: 2023-06-27 21:02:18 浏览: 210
以下是改写后的完整代码,增加了从文件中读取学生信息和显示学生信息的功能:
```cpp
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
struct Student {
int id;
string name;
string className;
double score1, score2, score3;
double average;
};
bool cmp(Student a, Student b) {
return a.average > b.average; //按照平均成绩从高到低排序
}
void inputStudentInfo(Student students[]) {
//输入学生信息
for (int i = 0; i < 3; i++) {
cout << "请输入第" << i+1 << "名学生的学号、姓名、班级信息、3门课程成绩(用空格隔开):" << endl;
cin >> students[i].id >> students[i].name >> students[i].className >> students[i].score1 >> students[i].score2 >> students[i].score3;
students[i].average = (students[i].score1 + students[i].score2 + students[i].score3) / 3;
}
}
void outputStudentInfo(ofstream& fout, Student students[]) {
//将学生信息输出到文件中
if (!fout) {
cout << "文件打开失败!" << endl;
return;
}
for (int i = 0; i < 3; i++) {
fout << students[i].id << " " << students[i].name << " " << students[i].className << " ";
fout << students[i].score1 << " " << students[i].score2 << " " << students[i].score3 << " ";
fout << students[i].average << endl;
}
fout.close();
cout << "学生信息已经成功输出到文件中!" << endl;
}
void readStudentInfo(ifstream& fin, Student students[]) {
//从文件中读取学生信息
if (!fin) {
cout << "文件打开失败!" << endl;
return;
}
for (int i = 0; i < 3; i++) {
fin >> students[i].id >> students[i].name >> students[i].className >> students[i].score1 >> students[i].score2 >> students[i].score3;
students[i].average = (students[i].score1 + students[i].score2 + students[i].score3) / 3;
}
fin.close();
cout << "学生信息已经成功从文件中读取!" << endl;
}
void displayStudentInfo(Student students[]) {
//显示学生信息
cout << "学号\t姓名\t班级\t成绩1\t成绩2\t成绩3\t平均成绩" << endl;
for (int i = 0; i < 3; i++) {
cout << students[i].id << "\t" << students[i].name << "\t" << students[i].className << "\t";
cout << students[i].score1 << "\t" << students[i].score2 << "\t" << students[i].score3 << "\t";
cout << students[i].average << endl;
}
}
int main() {
Student students[3];
//输入学生信息并输出到文件f1中
inputStudentInfo(students);
ofstream fout1("f1.txt");
outputStudentInfo(fout1, students);
//从文件f1中读取学生信息并按照平均成绩从高到低排序,然后输出到文件f2中
ifstream fin("f1.txt");
readStudentInfo(fin, students);
sort(students, students+3, cmp);
ofstream fout2("f2.txt");
outputStudentInfo(fout2, students);
//显示学生信息
displayStudentInfo(students);
return 0;
}
```
阅读全文