3-2对象数组排序

 202130310075


上机3-实验目的

理解类和对象的概念,掌握声明类和定义对象的方法。

掌握构造函数和析构函数的实现方法。

初步掌握使用类和对象编制C++程序。

一、实验内容

创建一个Score类,完成以下功能:

连续输入多位学生的float成绩(成绩=科目A 成绩+科目B成绩+科目C成绩)。

学生数目可以由用户自定义(默认为2个,最多为100个。提示:使用变量。)。

显示每位同学的每科成绩和平均分。

显示每门科目的平均成绩。

对每门成绩进行排序由高到低显示;

对整个项目(所有源代码)进行打包。

画流程图。

二、实验代码

 在进行具体代码实现之前,首先确定各类的数据成员和成员函数,并根据需求适当描述 流程图。

 

1.Student 类 


class Student {
public:
	Student() {};
	Student(string str)
	{
		name = str;
	}
	void SetName(); string GetName();
	void SetScoreA(); float GetScoreA();
	void SetScoreB(); float GetScoreB();
	void SetScoreC(); float GetScoreC();
private:
	string name;
	float score_A;
	float score_B;
	float score_C;
};

 

2.StudentScore类

class StudentScore 
{
public:

	StudentScore()
	{
		student_nr = 2;
	}
	StudentScore(int student_count)
	{
		student_nr = student_count;
	}

	void InputStudentNameAndScore();//输入学生姓名和成绩
	void ShowStudentNameAndScore();//显示学生姓名和成绩
	void ShowStudentAvgScore(int student_id);//显示学生的平均成绩
	void ShowCourseAvgScore(string course_name);//显示课程的平均成绩
	void OrderScoreByCourse(string course_name);//根据课程成绩进行排序

private:
	Student  score_table[100], score_sorting[100]; 
	int student_nr;
};

 

 3.main函数

int main()
	{
		StudentScore x;
		x.InputStudentNameAndScore(); 
		x.ShowStudentNameAndScore();
		x.ShowStudentAvgScore(1); //“1”为 学生id,用于类数组
		x.ShowCourseAvgScore("A"); 
		x.OrderScoreByCourse("B");
		system("pause");
		return 0;
	}


三、部分成员函数具体实现

    将某一课程的成绩按照降序排序:

1:利用switch 语句进行选择 具体的课程名称。

2:主要是利用的 score_sorting 数组作为 temp 的 作用,寄存新排序后各成员的具体信息。

void StudentScore::OrderScoreByCourse(string course_name)
{
	cout << "OrderScoreByCourse:" << course_name << endl;
	int n;
	if (course_name == "A")
		n = 1;
	if (course_name == "B")
		n = 2;
	if (course_name == "C")
		n = 3;
	switch (n)
	{ case 1:
		for (int i = 0; i < student_nr; i++)
	{

		for (int j = i + 1; j < student_nr; j++)
		{
			if (score_table[i].GetScoreA() <= score_table[j].GetScoreA())
			{
				score_sorting[i] = score_table[j];
				score_sorting[i + 1] = score_table[i];
			}
		}

	}
		break;
	case 2:for (int i = 0; i < student_nr; i++)
	{

		for (int j = i + 1; j < student_nr; j++)
		{
			if (score_table[i].GetScoreB() <= score_table[j].GetScoreB())
			{
				score_sorting[i] = score_table[j];
				score_sorting[i + 1] = score_table[i];
			}
		}
	}break;
	case 3:for (int i = 0; i < student_nr; i++)
	{

		for (int j = i + 1; j < student_nr; j++)
		{
			if (score_table[i].GetScoreC() <= score_table[j].GetScoreC())
			{
				score_sorting[i] = score_table[j];
				score_sorting[i + 1] = score_table[i];
			}
		}

	}break;
	default:
		cout << "请重新输入课程名称" << endl;
		break;
	}
	cout << "该课程成绩降序排列为:" << endl;
	for (int i = 0; i < student_nr; i++)
	{
		cout << "第" << i + 1 << "名:" << score_sorting[i].GetName() << endl;
	}
} 

四:运行结果

 

总结

以上就是今天要讲的内容,本文简单介绍了两个类之间的使用,熟悉了各类的数据成员和成员函数,解决了如上提出的问题。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值