结构体成绩管理系统c++
时间: 2023-11-30 07:42:09 浏览: 76
以下是一个使用结构体数组实现的C++成绩管理系统,支持数据验证、排序和统计功能:
```cpp
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// 定义学生结构体
struct Student {
string id; // 学号
string name; // 姓名
int score[3]; // 三门课程的成绩
int totalScore; // 总分
double avgScore; // 平均分
};
// 数据验证函数,确保输入的成绩在0-100之间
bool validateScore(int score) {
return score >= 0 && score <= 100;
}
// 录入学生信息函数
void inputStudent(Student &s) {
cout << "请输入学生的学号:";
cin >> s.id;
cout << "请输入学生的姓名:";
cin >> s.name;
for (int i = 0; i < 3; i++) {
int score;
do {
cout << "请输入" << s.name << "的第" << i + 1 << "门课程成绩:";
cin >> score;
} while (!validateScore(score)); // 数据验证
s.score[i] = score;
}
}
// 计算总分和平均分函数
void calculateScore(Student &s) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += s.score[i];
}
s.totalScore = sum;
s.avgScore = sum / 3.0;
}
// 按总分排序函数
bool cmp(Student a, Student b) {
return a.totalScore > b.totalScore;
}
// 输出学生信息函数
void outputStudent(Student s) {
cout << "学号:" << s.id << ",姓名:" << s.name << ",成绩:";
for (int i = 0; i < 3; i++) {
cout << s.score[i] << " ";
}
cout << ",总分:" << s.totalScore << ",平均分:" << s.avgScore << endl;
}
int main() {
const int N = 5; // 学生数量
Student students[N];
// 录入学生信息
for (int i = 0; i < N; i++) {
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
inputStudent(students[i]);
calculateScore(students[i]);
}
// 按总分排序
sort(students, students + N, cmp);
// 输出学生信息
for (int i = 0; i < N; i++) {
outputStudent(students[i]);
}
return 0;
}
```
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)