c++的一道程序题
发布网友
发布时间:2023-10-10 19:15
我来回答
共1个回答
热心网友
时间:2024-12-04 00:41
#include<iomanip>
using namespace std;
class Person
{
protected:
string name;
int age;
public:
Person()
{
;
}
Person(string p_name)
{
age=0;
name=p_name;
}
Person (string p_name, int p_age)
{
name=p_name;
age=p_age;
}
};
class Student:protected Person
{
int ID;
float cpp_count;
public:
float cpp_score;
float cpp_grade;
Student(string p_name,int id,int p_age,float score,float count):Person(p_name)
{
ID=id;
cpp_score=score;
cpp_count=count;
cpp_grade=cpp_score * 0.9 + cpp_count * 2;
}
Student():Person()
{
ID=0;
cpp_score=0;
cpp_count=0;
cpp_grade=cpp_score * 0.9 + cpp_count * 2;
}
void output()
{
cout<<ID<<" "<<name<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<cpp_grade<<endl;
}
};
class Teacher:protected Person
{
int ID;
Student s[100];
int count;
float average;
float sum;
public:
Teacher(string p_name,int id,int p_age):Person(p_name,p_age)
{
ID=id;
count=0;
name=p_name;
sum=0;
}
void add(Student s1)
{
s[count]=s1;
count++;
sum+=s1.cpp_grade;
}
void calculate()
{
average=sum/count;
}
void output()
{
int i;
cout<<ID<<" "<<name<<" "<<count<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<average<<endl;
for(i=0;i<count;i++)
{
s[i].output();
}
}
};
int main()
{
string name;
int age,id;
float score,count;
cin>>name>>id>>age;
Teacher teacher(name,id,age);
cin>>name;
for(;name[0]!='0';)
{
cin>>id>>age>>score>>count;
Student student(name,id,age,score,count);
teacher.add(student);
cin>>name;
}
teacher.calculate();
teacher.output();
return 0;
}