#include<iostream>
#include<string>
using namespace std;
class StudentScore {
public:
StudentScore()
{
student_nr = 2;
}
StudentScore(int student_count)
{
student_nr = student_count;
}
void InputStudentNameAndScore()
{
int i ,j=0;
for (j; j < student_nr; j++)
{
cout << "请输入学生姓名" << endl;
cin >> name_array[j];
for (i=0; i < 3; i++)
{
if (i == 0)
{
cout << "请输入科目A成绩" << endl;
cin >> score_table[j][i];
}
if (i == 1)
{
cout << "请输入科目B成绩" << endl;
cin >> score_table[j][i];
}
if (i == 2)
{
cout << "请输入科目C成绩" << endl;
cin >> score_table[j][i];
}
}
}
}
void ShowStudentNameAndScore()
{
int i, j;
for (j = 0; j < student_nr; j++)
{
cout << "姓名:" << name_array[j] <<" ";
for (i = 0; i < 3; i++)
{
if (i == 0)
{
cout << "科目A成绩:" << score_table[j][i] << " ";
}
if (i == 1)
{
cout << "科目B成绩:" << score_table[j][i] << " ";
}
if (i == 2)
{
cout << "科目C成绩:" << score_table[j][i] << endl;
}
}
}
}
void ShowStudentAvgScore(string student_id)
{
int i, j;
float avg = 0, sum = 0;
cout << "姓名:" << student_id << " ";
for (j = 0; j < student_nr; j++)
{
if (name_array[j] == student_id)
{
for (i = 0; i < 3; i++)
{
sum += score_table[j][i];
avg = sum / 3;
}
cout << "平均成绩:" << avg << endl;
}
}
}
void ShowCourseAvgScore(string course_name)
{
int i, j;
float course_sum=0, course_avg=0;
cout << "课程名称:" << course_name << " ";
if (course_name == "A")
{
for (j = 0; j < student_nr; j++)
{
course_sum += score_table[j][0];
course_avg = course_sum / student_nr;
}
cout << "平均成绩:" << course_avg << endl;
}
if (course_name == "C")
{
for (j = 0; j < student_nr; j++)
{
course_sum += score_table[j][2];
course_avg = course_sum / student_nr;
}
cout << "平均成绩:" << course_avg << endl;
}
if (course_name == "B")
{
for (j = 0; j < student_nr; j++)
{
course_sum += score_table[j][1];
course_avg = course_sum / student_nr;
}
cout << "平均成绩:" << course_avg << endl;
}
}
void OrderScoreByCourse(string course_name)
{
int i, j;
float temp=0;
string temp_name;
cout << "课程名称:" << course_name <<endl;
if (course_name == "A")
{
for (i = student_nr; i > 0; i--)
{
for (j = 0; j <i; j++)
{
if (score_table[j][0] < score_table[j + 1][0])
{
temp = score_table[j + 1][0];
score_table[j+1][0] = score_table[j][0];
score_table[j][0] = temp;
temp_name = name_array[j];
name_array[j] = name_array[j + 1];
name_array[j + 1] = temp_name;
}
}
}
for (j = 0; j < student_nr; j++)
{
cout << "姓名:" << name_array[j] <<" " << "成绩:" << score_table[j][0] << endl;
}
}
if (course_name == "B")
{
for (i = student_nr; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (score_table[j][1] < score_table[j + 1][1])
{
temp = score_table[j + 1][1];
score_table[j + 1][1] = score_table[j][1];
score_table[j][1] = temp;
temp_name = name_array[j];
name_array[j] = name_array[j + 1];
name_array[j + 1] = temp_name;
}
}
}
for (j = 0; j < student_nr; j++)
{
cout << "姓名:" << name_array[j] << "成绩:" << score_table[j][1];
}
}
if (course_name == "C")
{
for (i = student_nr; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (score_table[j][2] < score_table[j + 1][2])
{
temp = score_table[j + 1][2];
score_table[j + 1][2] = score_table[j][2];
score_table[j][2] = temp;
temp_name = name_array[j];
name_array[j] = name_array[j + 1];
name_array[j + 1] = temp_name;
}
}
}
for (j = 0; j < student_nr; j++)
{
cout << "姓名:" << name_array[j] << "成绩:" << score_table[j][2];
}
}
}
private:
float score_table[100][3];
string name_array[100];
int student_nr;
};
int main()
{
StudentScore x;
x.InputStudentNameAndScore();
x.ShowStudentNameAndScore();
x.ShowStudentAvgScore("徐元");
x.ShowCourseAvgScore("A");
x.OrderScoreByCourse("B");
return 0;
}