202130310252
#include<iostream>
#include<string>
using namespace std;
class StudentSore {
private:
float score_table[100][3];
string name_array[100];
int student_nr;
int n;
public:
StudentSore() //定义构造函数
{
student_nr = n;
}
StudentSore(int student_count) //定义带有一个参数的构造函数
{
student_count = student_nr;
}
void InputStudentNameAndSore() //定义成员函数
{
cout << "请输入学生总数:" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "请输入学生姓名:" << endl;
cin >> name_array[i];
cout << "请输入科目A成绩:" << endl;
cin >> score_table[i][0];
cout << "请输入科目B成绩:" << endl;
cin >> score_table[i][1];
cout << "请输入科目C成绩:" << endl;
cin >> score_table[i][2];
}
}
void ShowStudentNameAndSore() //定义成员函数
{cout << "学生总数:" << n << endl;
for (int i = 0; i < n; i++)
{
cout << "学生姓名:" << name_array[i] << endl;
cout << "科目A成绩:" << score_table[i][0] << " 科目B成绩:" << score_table[i][1] << " 科目C成绩:" << score_table[i][2] << endl;
}
}
void ShowStudentAvgSore() //定义成员函数
{
float avg;
for (int m = 0; m < n; m++)
{
avg = 0;
for (int j = 0; j < 3; j++)
{
avg = avg + score_table[m][j];
}
avg = avg / 3; cout << "姓名:" << name_array[m] << " 平均成绩:" << avg << endl;
}
}
void ShowCourseSore() //定义成员函数
{float A_avg = 0, B_avg = 0, C_avg = 0;
for (int m = 0; m < n; m++)
{
A_avg = A_avg + score_table[m][0];
B_avg = B_avg + score_table[m][1];
C_avg = C_avg + score_table[m][2];
}
A_avg = A_avg / n;
B_avg = B_avg / n;
C_avg = C_avg / n;
cout << "课程名称:A" << "平均成绩:" << A_avg << endl;
cout << "课程名称:B" << "平均成绩:" << B_avg << endl;
cout << "课程名称:C" << "平均成绩:" << C_avg << endl;
}
void OrderScoreBySourse() //定义成员函数
{
float temp;
for (int j = 0; j < 3; j++) {
for (int i = 0; i < n; i++) {
if (score_table[i][j] < score_table[i+1][j ])
{
temp = score_table[i][j];
score_table[i][j] = score_table[i+1][j];
score_table[i+1][j] = temp;
}
}
}
cout << "科目A的成绩排序为:";
for (int i = 0; i < n; i++) {
cout << score_table[i][0] << " ";
}
cout << "\n科目B的成绩排序为:";
for (int i = 0; i < n; i++) {
cout << score_table[i][1] << " ";
}
cout << "\n科目C的成绩排序为:";
for (int i = 0; i < n; i++) {
cout << score_table[i][2] << " ";
}
cout << endl;
}
};
int main()
{
StudentSore x;
x.InputStudentNameAndSore();
x.ShowStudentNameAndSore();
x.ShowStudentAvgSore();
x.ShowCourseSore();
x.OrderScoreBySourse();
}
程序流程图: