-
表彰优秀学生(多态)
2020-05-14 11:50:20学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。 GroupA...学期结束,班主任决定表彰一批学生,已知该班学生数在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 019AAA 93 97 94 82 80
0
输出样例:
009 BBB
007 CC
012 CCCC我的代码:
class Student{ protected: int s1,s2,s3,s4,s5; char s; string name,num; public: static double max; Student(){ s1=s2=s3=s4=s5=0; name=num=""; } virtual void display()=0; }; double Student::max=0; class GroupA:public Student{ public: GroupA(string nu,string na,int S1,int S2){ num=nu; name=na; s1=S1; s2=S2; if((s1+s2)>max) max=s1+s2; } void display(){ if((s1+s2)==max) cout<<num<<" "<<name<<endl; } }; class GroupB:public Student{ public: GroupB(string nu,string na,int S1,int S2,char S){ num=nu; name=na; s1=S1; s2=S2; s=S; if((s1+s2)>max) max=s1+s2; } void display(){ if((s1+s2)>=max*0.7&&s=='A'||(s1+s2)>=max) cout<<num<<" "<<name<<endl; } }; class GroupC:public Student{ public: GroupC(string nu,string na,int S1,int S2,int S3,int S4,int S5){ num=nu; name=na; s1=S1; s2=S2; s3=S3; s4=S4; s5=S5; } void display(){ if((s1+s2+s3+s4+s5)/5.0>=max/2.0*0.9) cout<<num<<" "<<name<<endl; } };
-
6-1 表彰优秀学生(多态)
2020-05-22 21:02:26学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。 GroupA...学期结束,班主任决定表彰一批学生,已知该班学生数在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答案:
class Student{ public: Student(string cnum,string cname,int cs1,int cs2){ num=cnum; name=cname; s1=cs1; s2=cs2; } virtual void display()=0;//定义纯虚函数 protected: string name,num; int s1,s2; //获奖线 }; static float maxAvg=0; class GroupA:public Student{//他只需要与最高的分数进行比较 public : GroupA(string cnum,string cname,int cs1,int cs2):Student(cnum,cname,cs1,cs2){//只调用父类的构造函数 float avg=(s1+s2)/2.0; if(avg>maxAvg)maxAvg=avg; } void display(){ if((s1+s2)/2.0==maxAvg) cout<<num<<" "<<name<<endl; }}; class GroupB:public Student{ public : GroupB(string cnum,string cname,int cs1,int cs2,char cbxf):Student(cnum,cname,cs1,cs2){ bxf=cbxf; //要进行判断 他的创建是否影响到groupA float avg=(s1+s2)/2.0; if(avg>maxAvg)maxAvg=avg; } void display(){ if((s1+s2)/2.0==maxAvg||(s1+s2)/2.0>=maxAvg*0.7&&bxf=='A'){ cout<<num<<" "<<name<<endl; } } private: char bxf; }; class GroupC:public Student{ private: int s3,s4,s5; public : GroupC(string cnum,string cname,int cs1,int cs2,int cs3,int cs4,int cs5):Student(cnum,cname,cs1,cs2){ s3=cs3; s4=cs4; s5=cs5; //这里他没有权利决定获奖线 } void display(){ float avg=(s1+s2+s3+s4+s5)/5.0; if(avg>=0.9*maxAvg){ cout <<num<<" "<<name<<endl; } } };
首先建立题目所说的类:
需要我们总结那些是派生类中共有的属性把它归纳到基类中去。然后看主函数中所用到的没有被定义的函数在类中补全
主要是不同参数的构造函数/和注意构建一个纯虚函数。注意的问题:
static最好写在类外,他在类外的时候可以直接赋值 -
PTA 习题:6-1 表彰优秀学生(多态)C++
2020-05-15 17:05:256-1 表彰优秀学生(多态) (15分) 学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立...6-1 表彰优秀学生(多态) (15分)
学期结束,班主任决定表彰一批学生,已知该班学生数在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三个类
输入样例:
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代码:
static double max1 = 0; class Student { protected: string name; string id; public: Student(string i,string name):id(i),name(name){} virtual void display() = 0; virtual double avg() = 0; }; class GroupA : public Student { public: GroupA(string i,string name,int s1,int s2):Student(i,name),score1(s1),score2(s2){ if(this->avg()>max1)max1=this->avg(); } double avg(){ return (score1+score2)/2; } void display(){ if (this->avg()==max1)cout<<id<<" "<<name<<endl; } private: int score1, score2; }; class GroupB : public Student {//特招运动员 public: GroupB(string i,string n,int s1,int s2,char l):Student(i,n),score1(s1),score2(s2),level(l){ if (this->avg()>max1)max1=avg(); } double avg(){ return (score1+score2)/2; } void display(){ if (this->avg()==max1)cout<<id<<" "<<name<<endl; else if(this->avg()>=0.7*max1&&this->level=='A')cout<<id<<" "<<name<<endl; } private: int score1, score2;//两门课成绩 char level;//运动会表现分 A B C D }; class GroupC : public Student {//学科专长生 public: GroupC(string i,string n,int s1,int s2,int s3,int s4,int s5):Student(i,n),score1(s1),score2(s2),score3(s3),score4(s4),score5(s5){} double avg(){ return (score1+score2+score3+score4+score5)/5; } void display(){ if (this->avg()>=0.9*max1)cout<<id<<" "<<name<<endl; } private: int score1, score2, score3, score4, score5;//五门课成绩 };
Over
GL&HF
-
PTA 表彰优秀学生(多态) (20 分)
2019-06-02 20:37:22学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。 ...学期结束,班主任决定表彰一批学生,已知该班学生数在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
实现代码如下:
class Student{ protected: int s1,s2,s3,s4,s5; string name,num; char gs; public: static float max; Student(){ s1=s2=s3=s4=s5=0; num=""; name=""; } virtual void display()=0; }; float Student::max=0; class GroupA:public Student{ public: GroupA(string nu,string na,int S1,int S2){ name=na; num=nu; s1=S1; s2=S2; if((s1+s2)>max) max=s1+s2; } void display(){ if((s1+s2)==max) cout<<num<<" "<<name<<endl; } }; class GroupB:public Student{ public: GroupB(string nu,string na,int S1,int S2,char Gs){ name=na; num=nu; s1=S1; s2=S2; gs=Gs; if((s1+s2)>max) max=s1+s2; } void display(){ if((s1+s2)>=max*0.7&&gs=='A'||(s1+s2)>=max) cout<<num<<" "<<name<<endl; } }; class GroupC:public Student{ public: GroupC(string nu,string na,int S1,int S2,int S3,int S4,int S5){ name=na; num=nu; s1=S1; s2=S2; s3=S3; s4=S4; s5=S5; } void display(){ if((s1+s2+s3+s4+s5)/5.0>=max/2.0*0.9) cout<<num<<" "<<name<<endl; } };
-
6-1 表彰优秀学生(多态) (20分)
2020-04-26 19:40:11学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。 GroupA... -
6-1 表彰优秀学生(多态) (15分)
2020-06-08 10:56:56学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。 GroupA... -
PTA 6-1 表彰优秀学生(多态) (15分)
2020-05-08 13:34:42学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。 GroupA... -
PTA---类的多态性---表彰优秀学生(多态) (20分)
2020-04-26 23:59:22PTA—表彰优秀学生(多态) (20分) 要求 学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应... -
PTA:表彰优秀学生(c++,多态)
2020-06-11 21:11:27学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。 GroupA... -
C++虚函数与多态——表彰优秀学生
2018-11-28 21:29:41学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。 ... -
PTA 12
2020-06-03 12:03:036-1 表彰优秀学生(多态) (15分) 学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立... -
LHL PTA 实验12. 期末综合测试
2020-06-02 13:21:376-1 表彰优秀学生(多态) (15分) 学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。 主函数根据输入的信息,相应建立...