上机 3.2 Score 排序

#include<iostream>
using namespace std;
class StudentScore {
public:
	StudentScore() {
		student_nr = 2;
	}

	StudentScore(int student_count) {
		student_nr=student_count;
	}
	~StudentScore() {
		cout << "Press any key to continue" << endl;
	}
	void InputStudentNameAndScore();
	void ShowStudentNameAndScore();
	void ShowStudentAvgScore(int student_id);
	void ShowCourseAvgScore(string course_name);
	void OrderScoreByCourse(string course_name);
private:
	int student_nr; //输入学生数
	string name_array[100]; //学生名字
	float score_table[3][100];
};
void StudentScore::InputStudentNameAndScore() {
	for (int i = 0; i < student_nr; i++) {
		cout << "请输入学生姓名:" << endl;
		cin >> name_array[i];
		cout << "请输入科目A的成绩:" << endl;
		cin >> score_table[0][i];
		cout << "请输入科目B的成绩:" << endl;
		cin >> score_table[1][i];
		cout << "请输入科目C的成绩:" << endl;
		cin >> score_table[2][i];
	}
}

void StudentScore::ShowStudentNameAndScore() {
	int i;
	
	for (i = 0; i < student_nr; i++) {
		cout << "姓名:" << name_array[i] << " 科目A成绩:" << score_table[0][i] << " 科目B成绩:" << score_table[1][i] << " 科目C成绩:" << score_table[2][i] << endl;
	}
}
void StudentScore::ShowStudentAvgScore(int student_id) {
	int i;
	float Stu_avg = 0;
	for (i = 0; i < student_nr; i++) {
	Stu_avg = score_table[0][i] / 3 + score_table[1][i] / 3 + score_table[2][i] / 3;
	if(i==student_id)
	cout << "姓名:" << name_array[i] << " 平均成绩:" << Stu_avg << endl;}
}

void StudentScore::ShowCourseAvgScore(string course_name) {
	float ClassA_avg = 0;
	float ClassB_avg = 0;
	float ClassC_avg = 0;
	int i;
	for (i = 0; i < student_nr; i++) {
		ClassA_avg += score_table[0][i] / student_nr;
		ClassB_avg += score_table[1][i] / student_nr;
		ClassC_avg += score_table[2][i] / student_nr;
	}
	if(course_name=="A")
	cout << "课程名称:A" << "平均成绩:" << ClassA_avg << endl;
	else if(course_name=="B")
	cout << "课程名称:B" << "平均成绩:" << ClassB_avg << endl;
	else
	cout << "课程名称:C" << "平均成绩:" << ClassC_avg << endl;
}

void StudentScore::	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];
		}
	}
}
int main() {
	StudentScore x;
	x.InputStudentNameAndScore();
	x.ShowStudentNameAndScore();
	x.ShowStudentAvgScore(1);
	x.ShowCourseAvgScore("A");
	x.OrderScoreByCourse("B");
	return 0;
}

 通过编写程序,实现了输入学生的姓名和成绩之后显示,以及计算学生的平均分数和各个科目的平均分数,按照成绩的降序进行排序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值