7-1 统计学生的总信息
分数 17
单位 武汉理工大学
编写一个学生类CStudent,要求:
1.包含学号、姓名、成绩的数据成员,以及记录学生信息的静态数据(总人数、总成绩、最高成绩、最低成绩)。
2.类中定义构造函数.
3.输出函数StudentInformation()输出每个学生的学号、姓名、成绩。
4.输出函数Output()输出学生总数、总成绩、平均成绩、最高成绩、最低成绩。
在主函数中实现:
1.先输入5个学生的信息,再输出每个学生的学号、姓名、成绩;
2.统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。
输入样例:
输入5个学生的学号、姓名、成绩
1001 s1 97.5
1002 s2 83
1003 s3 93
1004 s4 62.5
1005 s5 77
输出样例:
第一部分输出每个学生的学号、姓名、成绩
第二部分输出学生总数、总成绩
第三部分输出平均成绩、最高成绩、最低成绩
1001,s1,97.5
1002,s2,83
1003,s3,93
1004,s4,62.5
1005,s5,77
5,413
82.6,97.5,62.5
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class CStudent {
private:
int num; // 学号
string name; // 姓名
double score; // 成绩
static int total; // 总人数
static double totalScore; // 总成绩
static double maxScore; // 最高成绩
static double minScore; // 最低成绩
public:
CStudent(int n, string na, double s) {
num = n;
name = na;
score = s;
total++;
totalScore += s;
if (s > maxScore) {
maxScore = s;
}
if (s < minScore) {
minScore = s;
}
}
void StudentInformation() {
cout << num << "," << name << "," << score << endl;
}
static void Output() {
cout << total << "," << totalScore << endl;
cout << totalScore / total << "," << maxScore << "," << minScore << endl;
}
};
// 初始化静态数据成员
int CStudent::total = 0;
double CStudent::totalScore = 0;
double CStudent::maxScore = 0;
double CStudent::minScore = 100;
int main() {
vector<CStudent> students;
for (int i = 0; i < 5; i++) {
int num;
string name;
double score;
cin >> num >> name >> score;
students.push_back(CStudent(num, name, score));
}
for (int i = 0; i < 5; i++) {
students[i].StudentInformation();
}
CStudent::Output();
return 0;}