[C++/PTA] 表彰优秀学生(多态)
题目要求
学期结束,班主任决定表彰一批学生,已知该班学生数在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三个类
裁判测试程序样例:
#include<iostream>
#include <string>
using namespace std;
/* 请在这里填写答案 */
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;
}
输入样例:
1 001 AAAA 96 80
2 009 BBB 82 75 A
1 007 CC 100 99
3 012 CCCC 97 95 90 99 93
1 003 DDD 62 50
1 022 ABCE 78 92
2 010 FFF 45 40 A
3 019 AAA 93 97 94 82 80
0
输出样例:
009 BBB
007 CC
012 CCCC
解题思路
定义三个类 GroupA 、GroupB 和 GroupC 继承自基类Student,再重载虚函数display()实现输出符合要求的学生信息。
-
在 GroupA 和 GroupB 中,定义两门课程的成绩s1、s2
-
在 GroupC 中,定义五门课程的成绩s1、s2、s3、s4、s5
-
在三个类中定义构造函数,并在构造函数中计算最高分max。
-
在GroupA和GroupB中,使用display()函数中判断是否符合条件1,符合则输出学生信息;
-
在GroupB中,增加了判断是否符合条件3的语句。
-
在GroupC中,使用display()函数判断是否符合条件2,符合则输出学生信息。
代码
// 基类
class Student {
protected:
string name; // 姓名
string num; // 学号
char gs; // 运动会表现分的等级,仅GroupB使用
public:
static float max; // 静态成员变量,记录最高分
Student(){
num="";
name="";
};
virtual void display()=0; // 纯虚函数,子类必须重载
};
float Student::max=0; // 初始化静态成员变量
// 普通生,继承自Student
class GroupA:public Student{
protected:
int s1; // 第一门课程成绩
int s2; // 第二门课程成绩
public:
GroupA(string name,string num,int s1,int s2)
{
this->name=name;
this->num=num;
this->s1=s1;
this->s2=s2;
if((s1+s2)>max) // 更新最高分
max=s1+s2;
}
void display(){
if((s1+s2)==max) // 如果成绩达到最高分,输出学生信息
cout<<name<<" "<<num<<endl;
}
};
// 特招运动员,继承自Student
class GroupB:public Student{
protected:
int s1; // 第一门课程成绩
int s2; // 第二门课程成绩
public:
GroupB(string name,string num,int s1,int s2,char gs){
this->name=name;
this->num=num;
this->s1=s1;
this->s2=s2;
this->gs=gs;
if((s1+s2)>max) // 更新最高分
{
max=s1+s2;
}
}
void display(){
// 如果成绩达到最高分70%以上,并且运动会表现等级为A,或者成绩达到最高分,输出学生信息
if((s1+s2)>=max*0.7&&gs=='A'||(s1+s2)>=max)
cout<<name<<" "<<num<<endl;
}
};
// 学科专长生,继承自Student
class GroupC:public Student{
protected:
int s1; // 第一门课程成绩
int s2; // 第二门课程成绩
int s3; // 第三门课程成绩
int s4; // 第四门课程成绩
int s5; // 第五门课程成绩
public:
GroupC(string name,string num,int s1,int s2,int s3,int s4,int s5){
this->name=name;
this->num=num;
this->s1=s1;
this->s2=s2;
this->s3=s3;
this->s4=s4;
this->s5=s5;
}
void display(){
// 如果平均成绩达到获奖线90%以上,输出学生信息
if((s1+s2+s3+s4+s5)/5.0>=max/2.0*0.9)
cout<<name<<" "<<num<<endl;
}
};
整体代码如下:
#include<iostream>
#include <string>
using namespace std;
// 基类
class Student {
protected:
string name; // 姓名
string num; // 学号
char gs; // 运动会表现分的等级,仅GroupB使用
public:
static float max; // 静态成员变量,记录最高分
Student(){
num="";
name="";
};
virtual void display()=0; // 纯虚函数,子类必须重载
};
float Student::max=0; // 初始化静态成员变量
// 普通生,继承自Student
class GroupA:public Student{
protected:
int s1; // 第一门课程成绩
int s2; // 第二门课程成绩
public:
GroupA(string name,string num,int s1,int s2)
{
this->name=name;
this->num=num;
this->s1=s1;
this->s2=s2;
if((s1+s2)>max) // 更新最高分
max=s1+s2;
}
void display(){
if((s1+s2)==max) // 如果成绩达到最高分,输出学生信息
cout<<name<<" "<<num<<endl;
}
};
// 特招运动员,继承自Student
class GroupB:public Student{
protected:
int s1; // 第一门课程成绩
int s2; // 第二门课程成绩
public:
GroupB(string name,string num,int s1,int s2,char gs){
this->name=name;
this->num=num;
this->s1=s1;
this->s2=s2;
this->gs=gs;
if((s1+s2)>max) // 更新最高分
{
max=s1+s2;
}
}
void display(){
// 如果成绩达到最高分70%以上,并且运动会表现等级为A,或者成绩达到最高分,输出学生信息
if((s1+s2)>=max*0.7&&gs=='A'||(s1+s2)>=max)
cout<<name<<" "<<num<<endl;
}
};
// 学科专长生,继承自Student
class GroupC:public Student{
protected:
int s1; // 第一门课程成绩
int s2; // 第二门课程成绩
int s3; // 第三门课程成绩
int s4; // 第四门课程成绩
int s5; // 第五门课程成绩
public:
GroupC(string name,string num,int s1,int s2,int s3,int s4,int s5){
this->name=name;
this->num=num;
this->s1=s1;
this->s2=s2;
this->s3=s3;
this->s4=s4;
this->s5=s5;
}
void display(){
// 如果平均成绩达到获奖线90%以上,输出学生信息
if((s1+s2+s3+s4+s5)/5.0>=max/2.0*0.9)
cout<<name<<" "<<num<<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; // 如果输入0,结束输入
cin>>num>>name; // 输入学号和姓名
if(ty == 1) {
cin >> s1 >> s2; // 普通生输入两门课程成绩
pS[count++]=new GroupA(num, name, s1, s2); // 创建GroupA对象
} else if (ty == 2) {
cin >> s1 >> s2 >> gs; // 特招运动员输入两门课程成绩和运动会表现等级
pS[count++]=new GroupB(num, name, s1,s2, gs); // 创建GroupB对象
} else if (ty == 3) {
cin>>s1>>s2>>s3>>s4>>s5; // 学科专长生输入五门课程成绩
pS[count++]=new GroupC(num, name, s1,s2,s3,s4,s5); // 创建GroupC对象
}
}
// 遍历基类指针数组,调用display函数输出符合要求的学生信息
for(i=0;i<count;i++) {
pS[i]->display();
delete pS[i];
}
return 0;
}
总结
该题考察继承
、虚函数
、静态成员变量
、动态内存分配和指针
等知识点,同时也考察了基本的算法思路,如遍历
和条件判断
等。
我是秋说,我们下次见。
本文作者:秋说
本文链接:https://www.cnblogs.com/qiushuo/p/17481197.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步