设有学生信息如下:学号(长整型)、姓名(字符串型)、年龄(整型)、课程1成绩(实型)、课程2成绩(实型)、课程3成绩(实型)、课程4成绩(实型)、课程5成绩(实型)、课程6成绩(实型)、总分(实型)、平均分(实型)。试编写程序,输入3个学生的上述信息,计算每个学生的总分、平均分,然后输出每个学生学号、姓名、总分、平均分.
时间: 2023-07-04 22:06:43 浏览: 68
下面是一份 C++ 代码,可以实现上述功能:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Student {
long long id;
string name;
int age;
double score1, score2, score3, score4, score5, score6;
double totalScore, averageScore;
};
int main() {
const int NUM_STUDENTS = 3;
Student students[NUM_STUDENTS];
for(int i = 0; i < NUM_STUDENTS; i++) {
cout << "Enter student " << i+1 << " information: " << endl;
cout << "ID: ";
cin >> students[i].id;
cout << "Name: ";
cin >> students[i].name;
cout << "Age: ";
cin >> students[i].age;
cout << "Scores (6): ";
cin >> students[i].score1 >> students[i].score2 >> students[i].score3
>> students[i].score4 >> students[i].score5 >> students[i].score6;
students[i].totalScore = students[i].score1 + students[i].score2 + students[i].score3
+ students[i].score4 + students[i].score5 + students[i].score6;
students[i].averageScore = students[i].totalScore / 6.0;
}
cout << "Student Information: " << endl;
for(int i = 0; i < NUM_STUDENTS; i++) {
cout << "ID: " << students[i].id << endl;
cout << "Name: " << students[i].name << endl;
cout << "Total Score: " << students[i].totalScore << endl;
cout << "Average Score: " << students[i].averageScore << endl;
}
return 0;
}
```
这里使用了一个结构体 `Student` 来存储每个学生的信息,包括学号、姓名、年龄、6门课程的成绩、总分和平均分。首先通过输入三个学生的信息,计算出他们的总分和平均分,最后输出每个学生的学号、姓名、总分和平均分。
阅读全文