一、程序代码
(202130310154)
#include<iostream>
using namespace std;
class Score {
public:
Score()
{
s_nums = 2;
}
Score(int s_num)
{
s_nums = s_num;
}
~Score() {}
void InputNameAndScore();
void ShowStudentNameAndScore();
void ShowScoreAve();
void ScoreOrder();
private:
int s_nums;
float score[100][3] = {};
string name[100] = {};
};
void Score::InputNameAndScore() {
int i = 0;
for (i = 0; i < s_nums; i++)
{
cout << "请输入学生姓名:" << endl;
cin >> name[i];
cout << "请输入科目A的成绩:" << endl;
cin >> score[i][0];
cout << "请输入科目B的成绩:" << endl;
cin >> score[i][1];
cout << "请输入科目C的成绩:" << endl;
cin >> score[i][2];
}
}
void Score::ShowStudentNameAndScore()
{
int i;
float ave0;
cout << "" << endl;
for (i = 0; i < s_nums; i++)
{
cout << "姓名:" << name[i] << " 科目A成绩:" << score[i][0];
cout << "科目B成绩:" << score[i][1];
cout << "科目C成绩:" << score[i][2] << endl;
ave0 = (score[i][0] + score[i][1] + score[i][2]) / 3;
cout << "姓名:" << name[i] << " 平均成绩为:" << ave0 << endl << endl;
}
}
void Score::ShowScoreAve() //各科平均成绩
{
float ave1 = 0, ave2 = 0, ave3 = 0;
int i;
for (i = 0; i < s_nums; i++)
{
ave1 += score[i][0];
ave2 += score[i][1];
ave3 += score[i][2];
}
ave1 /= s_nums;
ave2 /= s_nums;
ave3 /= s_nums;
cout << "科目A平均成绩为:" << ave1 << endl;
cout << "科目B平均成绩为:" << ave2 << endl;
cout << "科目C平均成绩为:" << ave3 << endl << endl;
}
void Score::ScoreOrder()
{
int i, j, k, l = s_nums;
string xingming_temp;
float score_temp;
string xingming[50];
for (j = 0; j < 3; j++)
{
for (i = 0; i < s_nums; i++)
{
xingming[i] = name[i];
}
for (i = 0; i < s_nums - 1; i++)
{
for (k = i + 1; k < s_nums; k++)
{
if (score[i][j] < score[k][j])
{
score_temp = score[i][j];
score[i][j] = score[k][j];
score[k][j] = score_temp;
xingming_temp = xingming[i];
xingming[i] = xingming[k];
xingming[k] = xingming_temp;
}
}
}
l = s_nums;
switch (j)
{
case 0:cout << "课程名称 A:" << endl; break;
case 1:cout << "课程名称 B:" << endl; break;
case 2:cout << "课程名称 C:" << endl; break;
}
for (i = 0; i < s_nums; i++)
{
cout << "姓名:" << xingming[i] << " 成绩:" << score[i][j] << endl;
if (i == s_nums - 1)
{
cout << "" << endl;
}
}
}
}
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.InputNameAndScore();
x.ShowStudentNameAndScore();
x.ShowScoreAve();
x.ScoreOrder();
}
if (num == 0)
{
Score x;
x.InputNameAndScore();
x.ShowStudentNameAndScore();
x.ShowScoreAve();
x.ScoreOrder();
}
}
二、运行结果