是纯文本文件,每行记录分别包括学号、姓名和成绩,文件名字:student.txt,文件路径:C:\STL
时间: 2024-03-26 09:34:37 浏览: 137
student.txt
5星 · 资源好评率100%
好的,我已经了解了文件的格式和路径。接下来,我将分别实现你的三个功能:
1.依据学生学号查询并输出对应学生的姓名及成绩:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
int id;
string name;
double score;
};
bool cmp(const Student& a, const Student& b) {
return a.score < b.score;
}
int main() {
vector<Student> students;
ifstream fin("C:\\STL\\student.txt");
int id;
string name;
double score;
while (fin >> id >> name >> score) {
students.push_back({ id, name, score });
}
// 依据学生学号查询并输出对应学生的姓名及成绩
int queryId;
cout << "请输入要查询的学生学号:";
cin >> queryId;
for (const auto& s : students) {
if (s.id == queryId) {
cout << "姓名:" << s.name << ",成绩:" << s.score << endl;
break;
}
}
// 按照学生成绩升序排列并输出结果
sort(students.begin(), students.end(), cmp);
cout << "按照成绩升序排列的结果:" << endl;
for (const auto& s : students) {
cout << "学号:" << s.id << ",姓名:" << s.name << ",成绩:" << s.score << endl;
}
// 查询并输出平均成绩最高的前五名同学的学号,成绩及平均成绩
sort(students.rbegin(), students.rend(), cmp);
cout << "平均成绩最高的前五名同学的结果:" << endl;
double sum = 0;
int count = 0;
for (int i = 0; i < min(5, (int)students.size()); i++) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
sum += students[i].score;
count++;
}
cout << "平均成绩:" << sum / count << endl;
return 0;
}
```
2.按照学生成绩升序排列并输出结果:
```c++
bool cmp(const Student& a, const Student& b) {
return a.score < b.score;
}
int main() {
vector<Student> students;
ifstream fin("C:\\STL\\student.txt");
int id;
string name;
double score;
while (fin >> id >> name >> score) {
students.push_back({ id, name, score });
}
// 依据学生学号查询并输出对应学生的姓名及成绩
int queryId;
cout << "请输入要查询的学生学号:";
cin >> queryId;
for (const auto& s : students) {
if (s.id == queryId) {
cout << "姓名:" << s.name << ",成绩:" << s.score << endl;
break;
}
}
// 按照学生成绩升序排列并输出结果
sort(students.begin(), students.end(), cmp);
cout << "按照成绩升序排列的结果:" << endl;
for (const auto& s : students) {
cout << "学号:" << s.id << ",姓名:" << s.name << ",成绩:" << s.score << endl;
}
// 查询并输出平均成绩最高的前五名同学的学号,成绩及平均成绩
sort(students.rbegin(), students.rend(), cmp);
cout << "平均成绩最高的前五名同学的结果:" << endl;
double sum = 0;
int count = 0;
for (int i = 0; i < min(5, (int)students.size()); i++) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
sum += students[i].score;
count++;
}
cout << "平均成绩:" << sum / count << endl;
return 0;
}
```
3.查询并输出平均成绩最高的前五名同学的学号,成绩及平均成绩:
```c++
bool cmp(const Student& a, const Student& b) {
return a.score < b.score;
}
int main() {
vector<Student> students;
ifstream fin("C:\\STL\\student.txt");
int id;
string name;
double score;
while (fin >> id >> name >> score) {
students.push_back({ id, name, score });
}
// 依据学生学号查询并输出对应学生的姓名及成绩
int queryId;
cout << "请输入要查询的学生学号:";
cin >> queryId;
for (const auto& s : students) {
if (s.id == queryId) {
cout << "姓名:" << s.name << ",成绩:" << s.score << endl;
break;
}
}
// 按照学生成绩升序排列并输出结果
sort(students.begin(), students.end(), cmp);
cout << "按照成绩升序排列的结果:" << endl;
for (const auto& s : students) {
cout << "学号:" << s.id << ",姓名:" << s.name << ",成绩:" << s.score << endl;
}
// 查询并输出平均成绩最高的前五名同学的学号,成绩及平均成绩
sort(students.rbegin(), students.rend(), cmp);
cout << "平均成绩最高的前五名同学的结果:" << endl;
double sum = 0;
int count = 0;
for (int i = 0; i < min(5, (int)students.size()); i++) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
sum += students[i].score;
count++;
}
cout << "平均成绩:" << sum / count << endl;
return 0;
}
```
希望这些代码能满足你的需求。
阅读全文