C++习题(学生成绩统计)
程序员文章站
2024-01-16 10:43:22
...
C++习题
题目
一个学习小组有6人,共有5门课程,编程实现以下要求:(1)求第一门课程的平均成绩。
(2)找出有一门课程以上不及格的学员,输出该学员的所有成绩和平均成绩。
(3)找出5门课程都在90分以上的学员以及平均成绩在95分以上的学员。
代码实现
#include <iostream>
using namespace std;
//定义结构体
typedef struct {
string name;
float coursegrade[5];
}Student;
//求第一门课程的平均成绩
void printAverageFirstCourseGrade(Student* student) {
float sumgrade=0;
for (int i = 0; i < 6; i++) {
sumgrade += (student + i)->coursegrade[1];
}
cout<<"第一门课程的平均成绩为:"<<sumgrade / 6<<endl;
}
//找出有一门课程以上不及格的学员,输出该学员的所有成绩和平均成绩
void printFlunkStudentData(Student* student) {
for (int i = 0; i < 6; i++) {
int signal = 0;
for (int j = 0; j < 5; j++) {
if ((student + i)->coursegrade[j] < 60) {
signal = 1;
}
if (signal) {
cout << (student + i)->name << endl;
for (int k = 0; k < 5; k++) {
cout << "第"<<k+1<<"科成绩为:"<<(student + i)->coursegrade[k]<<endl;
}
cout << "平均成绩为:"<<((student + i)->coursegrade[0] + (student + i)->coursegrade[1] + (student + i)->coursegrade[2] + (student + i)->coursegrade[3] + (student + i)->coursegrade[4]) / 5<<endl;
break;
}
}
}
}
//找出5门课程都在90分以上的学员以及平均成绩在95分以上的学员
void printEliteStudentData(Student* student) {
cout << "5门课程都在90分以上的学员名单:" << endl;
for (int i = 0; i < 6; i++) {
int signal = 1;
for (int j = 0; j < 5; j++) {
if ((student + i)->coursegrade[j] <= 90) {
signal = 0;
}
}
if (signal) {
cout << (student + i)->name << endl;
}
}
cout << "平均成绩在95分以上的学员名单:" << endl;
for (int i = 0; i < 6; i++) {
int signal = 1;
for (int j = 0; j < 5; j++) {
if ((student + i)->coursegrade[j] <= 90) {
signal = 0;
}
}
if ((((student + i)->coursegrade[0] + (student + i)->coursegrade[1] + (student + i)->coursegrade[2] + (student + i)->coursegrade[3] + (student + i)->coursegrade[4]) / 5) > 95) {
cout << (student + i)->name << endl;
}
}
}
int main()
{
Student student[6];
for (int i = 0; i < 6; i++) {
cout << "请输入第" << i+1 << "个学生的姓名:" << endl;
cin >> student[i].name;
cout << "请输入5门成绩(中间以空格断开,按回车键结束):" << endl;
for (int j = 0; j < 5; j++) {
cin >> student[i].coursegrade[j];
}
}
while (true)
{
cout << "请选择您需要进行的操作:" << endl;
cout << "(1)求第一门课程的平均成绩" << endl;
cout << "(2)找出有一门课程以上不及格的学员,输出该学员的所有成绩和平均成绩" << endl;
cout << "(3)找出5门课程都在90分以上的学员以及平均成绩在95分以上的学员" << endl;
cout << "(0)退出" << endl;
int signal;
cin >> signal;
if (signal == 1) { printAverageFirstCourseGrade(student); }
if (signal == 2) { printFlunkStudentData(student); }
if (signal == 3) { printEliteStudentData(student); }
if (signal == 0) { break; }
}
return 0;
}
上一篇: 0921-输出优化
下一篇: 西门子PLC中各个组织块OB作用