#include<iostream>
#include<string>
using namespace std;
class StudentScore {
public:
StudentScore()
{
student_nr = 2;
}
StudentScore(int student_count)
{
student_nr = student_count;
}
void InputStudentNameAndScore();
void ShowStudentNameAndScore();
void ShowStdentAvgScore(int student_id);
void ShowCourseAvgScore(string course_name);
void OrderScoreByCourse(string course_name);
private:
float score_table[100][3];
string name_array[100];
int student_nr;
};
int main()
{
StudentScore x;
x.InputStudentNameAndScore();
x.ShowStudentNameAndScore();
x.ShowStdentAvgScore(1);
x.ShowCourseAvgScore("A");
x.OrderScoreByCourse("B");
return 0;
}
void StudentScore::InputStudentNameAndScore()
{
for (int i = 0; i < student_nr; i++)
{
string str;
cout << "请输入学生名字:" << endl;
cin >> str;
name_array[i] = str;
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 StudentScore::ShowStudentNameAndScore()
{
for (int i = 0; i < student_nr; i++)
{
cout << "姓名:" << name_array[i]
<< " 科目A成绩:" << score_table[i][0]
<< " 科目B成绩:" << score_table[i][1]
<< " 科目C成绩:" << score_table[i][2] << endl;
}
}
void StudentScore::ShowStdentAvgScore(int student_id)
{
float StudentAvgScore;
StudentAvgScore = (score_table[student_id][0] + score_table[student_id][1] + score_table[student_id][2]) / 3;
cout << "姓名:" << name_array[student_id]
<< " 平均成绩:" << StudentAvgScore << endl;
}
void StudentScore::ShowCourseAvgScore(string course_name)
{
float CourseAvgScore, sum;
sum = 0;
if (course_name == "A")
for (int i = 0; i < student_nr; i++)
sum += score_table[i][0];
if (course_name == "B")
for (int i = 0; i < student_nr; i++)
sum += score_table[i][1];
if (course_name == "C")
for (int i = 0; i < student_nr; i++)
sum += score_table[i][2];
CourseAvgScore = sum / student_nr;
cout << "课程名称:" << course_name << " 平均成绩:" << CourseAvgScore << endl;
}
void StudentScore::OrderScoreByCourse(string course_name)
{
if (course_name == "A")
{
for (int i = 0; i < student_nr - 1; i++)
{
for (int j = 0; j < student_nr - i - 1; j++)
{
if (score_table[j][0] < score_table[j + 1][0])
{
float num;
num = score_table[j][0];
score_table[j][0] = score_table[j + 1][0];
score_table[j + 1][0] = num;
num = score_table[j][1];
score_table[j][1] = score_table[j + 1][1];
score_table[j + 1][1] = num;
num = score_table[j][2];
score_table[j][2] = score_table[j + 1][2];
score_table[j + 1][2] = num;
string str;
str = name_array[j];
name_array[j] = name_array[j + 1];
name_array[j + 1] = str;
};
}
}
cout << "课程名称:A" << endl;
for (int i = 0; i < student_nr; i++)
{
cout << "姓名:" << name_array[i] << " 成绩:" << score_table[i][0] << endl;
}
}
if (course_name == "B")
{
for (int i = 0; i < student_nr - 1; i++)
{
for (int j = 0; j < student_nr - i - 1; j++)
{
if (score_table[j][1] < score_table[j + 1][1])
{
float num;
num = score_table[j][0];
score_table[j][0] = score_table[j + 1][0];
score_table[j + 1][0] = num;
num = score_table[j][1];
score_table[j][1] = score_table[j + 1][1];
score_table[j + 1][1] = num;
num = score_table[j][2];
score_table[j][2] = score_table[j + 1][2];
score_table[j + 1][2] = num;
string str;
str = name_array[j];
name_array[j] = name_array[j + 1];
name_array[j + 1] = str;
};
}
}
cout << "课程名称:B" << endl;
for (int i = 0; i < student_nr; i++)
{
cout << "姓名:" << name_array[i] << " 成绩:" << score_table[i][1] << endl;
}
}
if (course_name == "C")
{
for (int i = 0; i < student_nr - 1; i++)
{
for (int j = 0; j < student_nr - i - 1; j++)
{
if (score_table[j][2] < score_table[j + 1][2])
{
float num;
num = score_table[j][0];
score_table[j][0] = score_table[j + 1][0];
score_table[j + 1][0] = num;
num = score_table[j][1];
score_table[j][1] = score_table[j + 1][1];
score_table[j + 1][1] = num;
num = score_table[j][2];
score_table[j][2] = score_table[j + 1][2];
score_table[j + 1][2] = num;
string str;
str = name_array[j];
name_array[j] = name_array[j + 1];
name_array[j + 1] = str;
};
}
}
cout << "课程名称:C" << endl;
for (int i = 0; i < student_nr; i++)
{
cout << "姓名:" << name_array[i] << " 成绩:" << score_table[i][2] << endl;
}
}
}
202130310027