#include<iostream>
#include<string>
using namespace std;
class studentsscore {
public:
studentsscore()
{
students_nr = 2;
}
studentsscore(int students_count)
{
students_nr = students_count;
}
~studentsscore()
{
cout << "Press any key to continue" << endl;
}
void input_students_name_and_score()
{
for (int students_id=0; students_id < students_nr; students_id++)
{
cout << "请输入学生姓名:";
cin >> name[students_id];
cout << "请输入科目a成绩:";
cin >> score[students_id][0];
cout << "请输入科目b成绩:";
cin >> score[students_id][1];
cout << "请输入科目c成绩:";
cin >> score[students_id][2];
}
}
void show_students_name_and_score()
{
for (int students_id=0; students_id < students_nr; students_id++)
{
cout << "姓名:" << name[students_id] << " 科目a成绩:" << score[students_id][0]
<< " 科目b成绩:" << score[students_id][1] << " 科目c成绩:" << score[students_id]
[2]<<"\n";
}
}
void show_students_avgscore(int student_id)
{
float students_avgscore = 0;
cout << "请输入学生姓名:" << name[student_id] << endl;
students_avgscore = (score[student_id][0] + score[student_id][1] + score[student_id][2]);
students_avgscore = students_avgscore / 3;
cout << "平均成绩: " << students_avgscore<<"\n";
}
void show_course_avgscore(string course_name)
{
cout << "请输入课程名称:" <<course_name << endl;
float course_avgscore = 0;
if (course_name == "a")
{
for(int i = 0; i < students_nr; i++)
{
course_avgscore = score[i][0]+course_avgscore;
}
course_avgscore = course_avgscore / students_nr;
cout <<"课程的平均分为:"<< course_avgscore << "\n";
}
if (course_name == "b")
{
for (int i = 0; i < students_nr; i++)
{
course_avgscore = score[i][1] + course_avgscore;
}
course_avgscore = course_avgscore / students_nr;
cout << "课程的平均分为:" << course_avgscore << "\n";
}
if (course_name == "c")
{
for (int i = 0; i < students_nr; i++)
{
course_avgscore = score[i][2] + course_avgscore;
}
course_avgscore = course_avgscore / students_nr;
cout << "课程的平均分为:" << course_avgscore << "\n";
}
}
void order_score_by_course(string course_name)
{
cout << "请输入课程名称:"<<course_name<<"\n";
if (course_name == "a")
{
for (int i = 0; i < students_nr; i++)
{
cout << "姓名: " << name[i] << " 成绩:" << score[i][0] << " ";
}
}
else if (course_name == "b")
{
for (int i = 0; i < students_nr; i++)
{
cout << "姓名:" << name[i] << " 成绩:" << score[i][1]<<" ";
}
}
else if (course_name == "c")
{
for (int i = 0; i < students_nr; i++)
{
cout << "姓名:" << name[i] << " 成绩:" << score[i][2] << " ";
}
}
}
private:
float score[100][3];
string name[100];
int students_nr;
};
int main()
{
studentsscore x;
x.input_students_name_and_score();
x.show_students_name_and_score();
x.show_students_avgscore(1);
x.show_course_avgscore("a");
x.order_score_by_course("b");
return 0;
}
主函数内容:
定义一个变量x;
输入学生名称和成绩;
输出学生名称和成绩;
输出某一个学生的平均成绩;
输出某一科目的平均成绩;
输出所有学生的某一科目的成绩;