//202130312034
//main.cpp
#include<iostream>
#include<string>
using namespace std;
class StudentScore {
public:
StudentScore()//
{
student_nr = 2;
}
StudentScore(int student_count)
student_nr = student_count;
}
void InputStudentNameAndScore()
{
for (int student_id = 0; student_id < student_nr; student_id++)
{
cout << "请输入学生姓名:" << endl;
cin >> name_array[student_id];
cout << "请输入科目A成绩:" << endl;
cin >> score_table[student_id][1];
cout << "请输入科目B成绩:" << endl;
cin >> score_table[student_id][2];
cout << "请输入科目C成绩:" << endl;
cin >> score_table[student_id][3];
}
}
void ShowStudentNameAndScore()
{
for (int student_id = 0; student_id < student_nr; student_id++)//
{
cout << "姓名:" << name_array[student_id] << "科目A成绩:" << score_table[student_id][1] << "科目B成绩:" << score_table[student_id][2]
<< "科目C成绩:" << score_table[student_id][3] << endl;
}
}
void ShowStdentAvgScore(int student_id)
{
float average;
average = (score_table[student_id][1] + score_table[student_id][2] + score_table[student_id][3]) / 3;
cout << average << endl;
}
void ShowCourseAvgScore(string course_name)
{
if (course_name == "A")
{
float avg_A = 0;
for (int student_id = 0; student_id < student_nr; student_id++)
{
avg_A = avg_A + score_table[student_id][1];
}
avg_A = avg_A / student_nr;
cout << "A科目平均成绩:" << endl;
cout << avg_A << endl;
}
if (course_name == "B")
{
float avg_B = 0;
for (int student_id = 0; student_id < student_nr; student_id++)
{
avg_B = avg_B + score_table[student_id][2];
}
avg_B = avg_B / student_nr;
cout << "B科目平均成绩:" << endl;
cout << avg_B << endl;
}
if (course_name == "C")
{
float avg_C = 0;
for (int student_id = 0; student_id < student_nr; student_id++)
{
avg_C = avg_C + score_table[student_id][3];
}
avg_C = avg_C / student_nr;
cout << "C科目平均成绩:" << endl;
cout << avg_C << endl;
}
}
void OrderScoreByCourse(string course_name)
{
if (course_name == "A")
{
int ta = 0;
for (int student_id = 0; student_id < student_nr - 1; student_id++)
{
for (int j = 0; j < student_nr - 1 - j; j++)
{
if (score_table[j][1] < score_table[j + 1][1])
{
ta = score_table[j][1];
score_table[j][1] = score_table[j + 1][1];
score_table[j + 1][1] = ta;
}
}
}
cout << "排序为:" << endl;
for (int student_id = 0; student_id < student_nr; student_id++)
cout << score_table[student_id][1] << endl;
}
if (course_name == "B")
{
int ta = 0;
for (int student_id = 0; student_id < student_nr - 1; student_id++)
{
for (int j = 0; j < student_nr - 1 - j; j++)
{
if (score_table[j][2] < score_table[j + 1][2])
{
ta = score_table[j][2];
score_table[j][2] = score_table[j + 1][2];
score_table[j + 1][2] = ta;
}
}
}
cout << "排序为:" << endl;
for (int student_id = 0; student_id < student_nr; student_id++)
cout << score_table[student_id][2] << endl;
}
if (course_name == "C")
{
int ta = 0;
for (int student_id = 0; student_id < student_nr - 1; student_id++)
{
for (int j = 0; j < student_nr - 1 - j; j++)
{
if (score_table[j][3] < score_table[j + 1][3])
{
ta = score_table[j][3];
score_table[j][3] = score_table[j + 1][3];
score_table[j + 1][3] = ta;
}
}
}
cout << "排序为:" << endl;
for (int student_id = 0; student_id < student_nr; student_id++)
cout << score_table[student_id][3] << endl;
}
}
private:
float score_table[100][3], score_sorting[100];
string name_array[100], name_sorting[100];
int student_nr;
};
int main()
{
StudentScore x(3);//
x.InputStudentNameAndScore();
x.ShowStudentNameAndScore();
x.ShowStdentAvgScore(1);
x.ShowCourseAvgScore("A");
x.ShowCourseAvgScore("B");
x.ShowCourseAvgScore("C");
x.OrderScoreByCourse("A");
x.OrderScoreByCourse("B");
x.OrderScoreByCourse("C");
return 0;
流程框图