表彰优秀学生(多态)

1、题目大意:

学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。

主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。

GroupA类是普通生,有2门课程的成绩(均为不超过100的非负整数);

GroupB类是特招运动员,有2门课程的成绩(均为不超过100的非负整数),1次运动会的表现分,表现分有:A、B、C、D共4等。

GroupC类是学科专长生,有5门课程的成绩(均为不超过100的非负整数)。

表彰人员至少符合以下3个条件中的一个:

(1)2门课程平均分在普通生和特招运动员中,名列第一者。

a.该平均分称为获奖线。

b.存在成绩并列时,则全部表彰,例如某次考试有2人并列第1,则他们全部表彰。

(2)5门课程平均分达到或超过获奖线90%的学科专长生,给予表彰。

(3)2门课程平均分达到或超过获奖线70%的特招运动员,如果其运动会表现分为A,给予表彰。

输入格式:每个测试用例占一行,第一项为类型,1为普通生,2为特招运动员,3为学科专长生, 输入0表示输入的结束。第二项是学号,第三项是姓名。对于普通生来说,共输入5项,第4、5项是课程成绩。对于特招运动员来说,共输入6项,第4、5项是课程成绩,第6项是运动会表现。对于学科专长生来说,共输入8项,第4、5、6、7、8项是课程成绩。

输出时,打印要表彰的学生的学号和姓名。(输出顺序与要表彰学生的输入前后次序一致)

函数接口定义:
以Student为基类,构建GroupA, GroupB和GroupC三个类

2、代码如下:

#include<iostream>
#include<string>
using namespace std;
class Student
{
	protected:
		string number;
		string name;
	public:
		static double max;
		Student(string num,string n)
		{
			number=num;
			name=n;
		}
		virtual double aver()=0;
		virtual void display()=0;

};
double Student::max=0;
class GroupA:public Student
{
	private:
		int score1;
		int score2;
	public:

		GroupA(string num,string n,int a,int b):Student(num,n)
		{
			score1=a;
			score2=b;
			if(aver()>Student::max)
			{
				Student::max=aver();
			}
		}
		virtual double aver()
		{
			return (score1+score2)/2;
		}
		virtual void display()
		{
			if(aver()==Student::max)
			{
				cout<<number<<" "<<name<<endl;
			}

		}
};
class GroupB:public Student
{
	private:
		int score1;
		int score2;
		char s;
	public:

		GroupB(string num,string n,int a,int b,char  c):Student(num,n)
		{
			score1=a;
			score2=b;
			s=c;
			if(aver()>Student::max)
			{
				Student::max=aver();
			}
		}
		virtual double aver()
		{
			return (score1+score2)/2;
		}
		virtual void display()
		{
			if(aver()==Student::max)
			{
				cout<<number<<" "<<name<<endl;
			}

			else  if(aver()>=Student::max*0.7&&s=='A')
			{
				cout<<number<<" "<<name<<endl;
			}
		}

};
class GroupC:public Student
{
	private:
		int score1;
		int score2;
		int score3;
		int score4;
		int score5;
	public:
		GroupC(string num,string n,int a1,int a2,int a3,int a4,int a5):Student(num,n)
		{
			score1=a1;
			score2=a2;
			score3=a3;
			score4=a4;
			score5=a5;
		}
		virtual double aver()
		{
			return (score1+score2+score3+score4+score5)/5;
		}
		virtual void display()
		{
			if(aver()>=Student::max*0.9)
			{
				cout<<number<<" "<<name<<endl;
			}

		}
};

int main()
{
	const int Size=50;
	string num, name;
	int i,ty,s1,s2,s3,s4,s5;
	char gs;
	Student *pS[Size];
	int count=0;
	for(i=0; i<Size; i++)
	{
		cin>>ty;
		if(ty==0) break;
		cin>>num>>name>>s1>>s2;
		switch(ty)
		{
			case 1:
				pS[count++]=new GroupA(num, name, s1, s2);
				break;
			case 2:
				cin>>gs;
				pS[count++]=new GroupB(num, name, s1,s2, gs);
				break;
			case 3:
				cin>>s3>>s4>>s5;
				pS[count++]=new GroupC(num, name, s1,s2,s3,s4,s5);
				break;
		}
	}
	for(i=0; i<count; i++)
	{
		pS[i]->display();
		delete pS[i];
	}
	return 0;
}
posted @ 2016-07-16 15:11  laixl  阅读(3994)  评论(0编辑  收藏  举报