PTA:7-61 师生信息管理

给出下面的一个基类框架
class Person
{
protected:
int NO;//编号
public:
virtual void display()=0;//输出相关信息
}
以Person为基类,构建出Student、Teacher两个类。
生成上述类并编写主函数,要求主函数中有一个基类Person指针数组,数组元素不超过10个。
Person *pp[10];
主函数根据输入的信息,相应建立Student, Teacher类对象,对于Student给出期末5门课的成绩(为整数,缺考的科目填-1), 对于Teacher则给出近3年,每年发表的论文数量。
输入格式:每个测试用例占一行,第一项为人员类型,1为Student,2为Teacher.接下来为编号(0-9999),接下来Student是5门课程成绩,Teacher是3年的论文数。最后一行为0,表示输入的结束。
要求输出编号,以及Student缺考的科目数和已考科目的平均分(保留1位小数,已考科目数为0时,不输出平均分),Teacher的3年论文总数。
提示:应用虚函数实现多态

输入样例
1 19 -1 -1 -1 -1 -1
1 125 78 66 -1 95 88
2 68 3 0 7
2 52 0 0 0
1 6999 32 95 100 88 74
0

输出样例
19 5
125 1 81.8
68 10
52 0
6999 0 77.8

#include<iostream>
using namespace std;
class Person
{
	protected:
		int NO;//编号
	public:
		virtual void display()=0;//输出相关信息
};
class Student:public Person
{
	int score[5];
	int pd;
	double sum;
	public:
		Student(int n,int a,int b,int c,int d,int e)
		{
			sum=pd=0;
			NO=n;
			int k[5]={a,b,c,d,e};
			for(int i=0;i<5;i++)
			{
				score[i]=k[i];
				if(score[i]==-1)
					pd++;
				else
					sum+=score[i];
			}
				
		}
		virtual void display()
		{
			cout<<NO<<" "<<pd;
			if(pd!=5)
			printf(" %.1f",sum/(5-pd));
			cout<<endl;
		}
};
class Teacher:public Person
{
	int num[3];
	int sum;
	public:
		Teacher(int n,int a,int b,int c)
		{
			NO=n;
			sum=0;
			int k[3]={a,b,c};
			for(int i=0;i<3;i++)
			{
				num[i]=k[i];
				sum+=num[i];
			}
				
		}
		virtual void display()
		{
			cout<<NO<<" "<<sum<<endl;
		}
};
int main()
{
	Person *pp[10];
	int type;
	cin>>type;
	int n,a,b,c,d,e;
	int i=0;
	while(type!=0)
	{
		cin>>n;
		switch(type)
		{
			case 1:
				cin>>a>>b>>c>>d>>e;
				pp[i]=new Student(n,a,b,c,d,e);
				break;
			case 2:
				cin>>a>>b>>c;
				pp[i]=new Teacher(n,a,b,c);
		}
		pp[i]->display();
		i++;
		cin>>type;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值