定义一个学生成绩结构体类型,包含“学号”、“姓名”、“性别”、“年龄”、“班级”、“英语”、“数学”、“物理”、“总分”、“名次”等信息。编写6个函数分别用于: (1) 使用结构体数组,输入全班10名
时间: 2023-10-01 21:02:05 浏览: 290
使用结构体数组,利用函数实现按学生学号和姓名的查询操作.c
学生的信息;(2) 计算每个学生的总分和平均分;(3) 对全班学生按照总分从高到低排序;(4) 统计每个班级的平均总分;(5) 统计全班英语、数学、物理三门科目的平均分;(6) 根据学生总分排名输出学生信息。
以下是代码实现:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int N = 10;
struct Student {
string id;
string name;
string gender;
int age;
string cls;
float eng;
float math;
float phy;
float total;
int rank;
};
void input(Student stus[]) {
for (int i = 0; i < N; i++) {
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "学号:";
cin >> stus[i].id;
cout << "姓名:";
cin >> stus[i].name;
cout << "性别:";
cin >> stus[i].gender;
cout << "年龄:";
cin >> stus[i].age;
cout << "班级:";
cin >> stus[i].cls;
cout << "英语成绩:";
cin >> stus[i].eng;
cout << "数学成绩:";
cin >> stus[i].math;
cout << "物理成绩:";
cin >> stus[i].phy;
}
}
void calcTotal(Student stus[]) {
for (int i = 0; i < N; i++) {
stus[i].total = stus[i].eng + stus[i].math + stus[i].phy;
}
}
void calcRank(Student stus[]) {
sort(stus, stus + N, [](const Student& a, const Student& b) {
return a.total > b.total;
});
for (int i = 0; i < N; i++) {
stus[i].rank = i + 1;
}
}
void calcAvg(Student stus[], float& engAvg, float& mathAvg, float& phyAvg) {
float engSum = 0, mathSum = 0, phySum = 0;
for (int i = 0; i < N; i++) {
engSum += stus[i].eng;
mathSum += stus[i].math;
phySum += stus[i].phy;
}
engAvg = engSum / N;
mathAvg = mathSum / N;
phyAvg = phySum / N;
}
void calcClsAvg(Student stus[], float& cls1Avg, float& cls2Avg) {
float cls1Sum = 0, cls2Sum = 0;
int cls1Cnt = 0, cls2Cnt = 0;
for (int i = 0; i < N; i++) {
if (stus[i].cls == "1") {
cls1Sum += stus[i].total;
cls1Cnt++;
}
else {
cls2Sum += stus[i].total;
cls2Cnt++;
}
}
cls1Avg = cls1Sum / cls1Cnt;
cls2Avg = cls2Sum / cls2Cnt;
}
void printRank(Student stus[]) {
cout << "学号\t姓名\t性别\t年龄\t班级\t英语\t数学\t物理\t总分\t名次" << endl;
for (int i = 0; i < N; i++) {
cout << stus[i].id << "\t" << stus[i].name << "\t" << stus[i].gender << "\t" << stus[i].age << "\t" << stus[i].cls << "\t" << stus[i].eng << "\t" << stus[i].math << "\t" << stus[i].phy << "\t" << stus[i].total << "\t" << stus[i].rank << endl;
}
}
int main() {
Student stus[N];
float engAvg = 0, mathAvg = 0, phyAvg = 0;
float cls1Avg = 0, cls2Avg = 0;
input(stus);
calcTotal(stus);
calcRank(stus);
calcAvg(stus, engAvg, mathAvg, phyAvg);
calcClsAvg(stus, cls1Avg, cls2Avg);
printRank(stus);
cout << "全班英语平均分:" << engAvg << endl;
cout << "全班数学平均分:" << mathAvg << endl;
cout << "全班物理平均分:" << phyAvg << endl;
cout << "1班平均总分:" << cls1Avg << endl;
cout << "2班平均总分:" << cls2Avg << endl;
return 0;
}
```
阅读全文