有N名学生,每名学生含有如下信息:学号、姓名、某四门课的成绩,并计算其总分,用一结构体数组表示之。然后实现以下功能: (1)将这些数据存放至文件stuf.dat中; (2)将文件中的数据读出至结构体数组(或链表)中,并显示之; (3)输出总分最高分和最低分的名字; (4)输出总分在340分,单科成绩不低于80分的名单; (5)求出各科平均分数; (6)按总分排名; (7)输出补考名单并存入文件。c加加
时间: 2024-01-16 15:02:48 浏览: 115
好的,我来为您解答。
首先,我们需要定义一个结构体来表示每个学生的信息:
```cpp
struct Student {
int num; // 学号
char name[20]; // 姓名
int score[4]; // 四门课成绩
int total; // 总分
};
```
接下来,我们可以实现以下功能:
1. 将数据存放至文件stuf.dat中
```cpp
#include <fstream>
void saveToFile(Student* students, int n) {
std::ofstream fout("stuf.dat", std::ios::binary);
fout.write((char*)students, sizeof(Student) * n);
fout.close();
}
```
2. 将文件中的数据读出至结构体数组中,并显示之
```cpp
#include <iostream>
#include <fstream>
void readFromFile(Student* students, int n) {
std::ifstream fin("stuf.dat", std::ios::binary);
fin.read((char*)students, sizeof(Student) * n);
fin.close();
}
void displayStudents(Student* students, int n) {
for (int i = 0; i < n; i++) {
std::cout << "学号:" << students[i].num << ",姓名:" << students[i].name << ",成绩:";
for (int j = 0; j < 4; j++) {
std::cout << students[i].score[j] << " ";
}
std::cout << ",总分:" << students[i].total << std::endl;
}
}
```
3. 输出总分最高分和最低分的名字
```cpp
#include <algorithm>
void findMinMax(Student* students, int n) {
int maxTotal = students[0].total, minTotal = students[0].total;
std::string maxName = students[0].name, minName = students[0].name;
for (int i = 1; i < n; i++) {
if (students[i].total > maxTotal) {
maxTotal = students[i].total;
maxName = students[i].name;
}
if (students[i].total < minTotal) {
minTotal = students[i].total;
minName = students[i].name;
}
}
std::cout << "总分最高的学生是:" << maxName << ",总分为:" << maxTotal << std::endl;
std::cout << "总分最低的学生是:" << minName << ",总分为:" << minTotal << std::endl;
}
```
4. 输出总分在340分,单科成绩不低于80分的名单
```cpp
void findQualified(Student* students, int n) {
std::cout << "总分在340分,且四门课成绩均不低于80分的学生:" << std::endl;
for (int i = 0; i < n; i++) {
if (students[i].total >= 340 && students[i].score[0] >= 80 && students[i].score[1] >= 80 && students[i].score[2] >= 80 && students[i].score[3] >= 80) {
std::cout << students[i].name << std::endl;
}
}
}
```
5. 求出各科平均分数
```cpp
void calculateAverage(Student* students, int n) {
double avg[4] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
avg[j] += students[i].score[j];
}
}
for (int i = 0; i < 4; i++) {
std::cout << "第" << i+1 << "门课的平均分数为:" << avg[i] / n << std::endl;
}
}
```
6. 按总分排名
```cpp
bool cmp(const Student& a, const Student& b) {
return a.total > b.total;
}
void sortByTotal(Student* students, int n) {
std::sort(students, students+n, cmp);
std::cout << "按总分排名的结果:" << std::endl;
for (int i = 0; i < n; i++) {
std::cout << i+1 << ". " << students[i].name << ",总分:" << students[i].total << std::endl;
}
}
```
7. 输出补考名单并存入文件
```cpp
void outputReExamList(Student* students, int n) {
std::ofstream fout("reexam.txt");
fout << "补考名单:" << std::endl;
for (int i = 0; i < n; i++) {
bool needReExam = false;
for (int j = 0; j < 4; j++) {
if (students[i].score[j] < 60) {
needReExam = true;
break;
}
}
if (needReExam) {
fout << students[i].name << std::endl;
}
}
fout.close();
}
```
最后,我们可以在主函数中调用上述函数来实现整个程序:
```cpp
int main() {
int n;
std::cout << "请输入学生人数:" << std::endl;
std::cin >> n;
Student* students = new Student[n];
// 输入学生信息
for (int i = 0; i < n; i++) {
std::cout << "请输入第" << i+1 << "个学生的信息:" << std::endl;
std::cout << "学号:";
std::cin >> students[i].num;
std::cout << "姓名:";
std::cin >> students[i].name;
std::cout << "四门课的成绩:";
for (int j = 0; j < 4; j++) {
std::cin >> students[i].score[j];
students[i].total += students[i].score[j];
}
}
// 存储数据到文件
saveToFile(students, n);
// 从文件中读取数据
readFromFile(students, n);
// 显示学生信息
displayStudents(students, n);
// 查找总分最高和最低的学生
findMinMax(students, n);
// 查找总分在340分,且四门课成绩均不低于80分的学生
findQualified(students, n);
// 计算各门课的平均分数
calculateAverage(students, n);
// 按总分排名
sortByTotal(students, n);
// 输出补考名单
outputReExamList(students, n);
delete[] students;
return 0;
}
```
希望能够帮到您!
阅读全文