实验代码:
#include<iostream>
#include<string>
using namespace std;
class Score {
public:
Score(int student_count=2)
{
student_nr = student_count;
}
void InputStudentNameAndScore()
{
for (int i = 0; i < student_nr; 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 ShowStudentNameAndScore()
{
for (int student_id = 0; student_id < student_nr; student_id++)
{
cout << "姓名:" << name_array[student_id] << " 科目A成绩:" << score_table[student_id][0] << " 科目B成绩:" << score_table[student_id][1] << " 科目C成绩: " << score_table[student_id][2] << endl;
}
}
void ShowStudentAvgScore(int student_id)
{
float avgA = 0;
float avgB = 0;
float avgC = 0;
float avg[100];
for (student_id = 0; student_id < student_nr; student_id++)
{
avgA = avgA + score_table[student_id][0];
avgB = avgB + score_table[student_id][1];
avgC = avgC + score_table[student_id][2];
avg[student_id ]= score_table[student_id][0] + score_table[student_id][1] + score_table[student_id][2];
}
avgA = avgA / student_nr;
avgB = avgB / student_nr;
avgC = avgC / student_nr;
for (int student_id = 0; student_id < student_nr; student_id++)
{
cout << "姓名:" << name_array[student_id] << " 平均成绩:" << avg[student_id]/3 << endl;
}
cout << "课程名称:A 平均成绩:" << avgA << endl;
cout << "课程名称:B 平均成绩:" << avgB << endl;
cout << "课程名称:C 平均成绩:" << avgC<< endl;
}
void OrderScoreByCourse()
{
int i, j, k, l = student_nr;
string XM_temp;
float score_temp;
for (j = 0; j < 3; j++)
{
for (i= 0; i < student_nr; i++)
{
name_array[i];
}
for (i = 0; i < student_nr - 1; i++)
{
for (k = i + 1; k < student_nr; k++)
{
if (score_table[i][j] < score_table[k][j])
{
score_temp = score_table[i][j];
score_table[i][j] = score_table[k][j];
score_table[k][j] = score_temp;
XM_temp = name_array[i];
name_array[i] = name_array[k];
name_array[k] = XM_temp;
}
}
}
switch (j)
{
case 0:cout << "课程名称 A:" << endl; break;
case 1:cout << "课程名称 B:" << endl; break;
case 2:cout << "课程名称 C:" << endl; break;
}
for (i = 0; i < student_nr; i++)
{
cout << "姓名:" << name_array[i] << " 成绩:" <<score_table[i][j] << endl;
if (i == student_nr - 1)
{
cout << "" << endl;
}
}
}
}
private:
float score_table[100][3];
int student_nr;
string name_array[100];
};
int main()
{
int num;
cout << "默认学生数目为2,最多100个,如需改变请按1,无需改变请按0:";
cin >> num;
while ((num != 0) && (num != 1))
{
cout << "输入错误,需要改变请按1,无需改变请按0" << endl;
cin >> num;
}
if (num == 1)
{
int student_numebr;
cout << "请输入学生数目:";
cin >> student_numebr;
Score x(student_numebr);
x.InputStudentNameAndScore();
x.ShowStudentNameAndScore();
x.ShowStudentAvgScore(0);
x.OrderScoreByCourse();
}
if (num == 0)
{
Score x;
x.InputStudentNameAndScore();
x.ShowStudentNameAndScore();
x.ShowStudentAvgScore(0);
x.OrderScoreByCourse();
}
return 0;
}
非代码的内容:
(程序流程图)