题目描述
从键盘输入某班级30个学生的学号、姓名、三门课(语文、数学、英语)的成绩,分别实现如下功能:
(1)统计每个学生的总分和平均分;
(2)按总分由大到小排出名次;
(3)打印出成绩在全班平均分以上的学生名单和数据信息。
(4)任意输入一个学号,能够查找出该学生在班级中的排名及其考试成绩。
要求:各个功能要求分别用独立的函数来实现,在主函数中通过菜单选择来实现各个功能。且要求使用结构体数组来存储和处理30个学生信息,定义的结构体主要包括:学号、姓名、三门课成绩、总分、平均分、名次等成员。
实现代码
#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
int id;
string name;
int chinese, math, english;
int total;
double average;
int rank;
};
Student students[30];
// 统计每个学生的总分和平均分
void calculateScore() {
for (int i = 0; i < 30; i++) {
students[i].total = students[i].chinese + students[i].math + students[i].english;
students[i].average = students[i].total / 3.0;
}
}
// 按总分由大到小排出名次
void sortStudents() {
sort(students, students + 30, [](const Student& a, const Student& b) {
return a.total > b.total;
});
for (int i = 0; i < 30; i++) {
students[i].rank = i + 1;
}
}
// 打印出成绩在全班平均分以上的学生名单和数据信息
void printAboveAverage() {
double classAverage = 0;
for (int i = 0; i < 30; i++) {
classAverage += students[i].total;
}
classAverage /= 30.0;
cout << "全班平均分:" << classAverage << endl;
cout << "成绩在全班平均分以上的学生名单和数据信息:" << endl;
for (int i = 0; i < 30; i++) {
if (students[i].total >= classAverage) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",语文:" << students[i].chinese << ",数学:" << students[i].math << ",英语:" << students[i].english << ",总分:" << students[i].total << ",平均分:" << students[i].average << endl;
}
}
}
// 任意输入一个学号,能够查找出该学生在班级中的排名及其考试成绩
void searchStudent() {
int id;
cout << "请输入学号:";
cin >> id;
for (int i = 0; i < 30; i++) {
if (students[i].id == id) {
cout << "该学生在班级中的排名为:" << students[i].rank << endl;
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",语文:" << students[i].chinese << ",数学:" << students[i].math << ",英语:" << students[i].english << ",总分:" << students[i].total << ",平均分:" << students[i].average << endl;
return;
}
}
cout << "未找到该学生" << endl;
}
int main() {
// 从键盘输入每个学生的信息
for (int i = 0; i < 30; i++) {
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "学号:";
cin >> students[i].id;
cout << "姓名:";
cin >> students[i].name;
cout << "语文成绩:";
cin >> students[i].chinese;
cout << "数学成绩:";
cin >> students[i].math;
cout << "英语成绩:";
cin >> students[i].english;
}
// 统计每个学生的总分和平均分
calculateScore();
// 按总分由大到小排出名次
sortStudents();
int choice;
while (true) {
cout << endl;
cout << "请选择功能:" << endl;
cout << "1. 统计每个学生的总分和平均分" << endl;
cout << "2. 按总分由大到小排出名次" << endl;
cout << "3. 打印出成绩在全班平均分以上的学生名单和数据信息" << endl;
cout << "4. 查找学生排名及考试成绩" << endl;
cout << "0. 退出" << endl;
cout << "请选择:" << endl;
cin >> choice;
switch (choice) {
case 1:
calculateScore();
for (int i = 0; i < 30; i++) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",总分:" << students[i].total << ",平均分:" << students[i].average << endl;
}
break;
case 2:
sortStudents();
for (int i = 0; i < 30; i++) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",总分:" << students[i].total << ",排名:" << students[i].rank << endl;
}
break;
case 3:
printAboveAverage();
break;
case 4:
searchStudent();
break;
case 0:
return 0;
default:
cout << "输入错误,请重新选择" << endl;
break;
}
}
return 0;
}
运行截图
© 版权声明
本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!
THE END