完成“学生cpp成绩计算”之后,修改Person和Student类,各自增加两个无参构造函数。
仍以Person类为基础,建立一个派生类Teacher,增加以下成员数据:
int ID;//教师工号
Student stu[100];//学生数组
int count;//学生数目,最多不超过100
float cpp_average;//班级cpp平均分
增加以下成员函数:
Teacher类的参数化构造函数
void Add (Student & stu1)//在学生数组中增加一个学生记录
void average();//计算当前班级cpp平均成绩cpp_average
void print()//输出当前班级学生的信息
//其中学生记录中cpp_score和cpp_grade输出保留一位小数
//当前班级cpp_average输出保留一位小数;
//输出格式如下:
//第一行:教师工号 教师姓名 班级学生数 cpp_average
//第二行至第count+1行每一行输出一个学生的信息,每一行格式
// 学生学号 学生姓名 cpp_grade
//cpp_grade保留一位小数
生成上述类并编写主函数,根据输入的教师基本信息,建立一个教师对象,根据输入的每一条学生基本信息,建立一个学生对象,计算学生cpp总评成绩并且加入学生数组中,由教师对象计算班级cpp平均成绩,并输出班级学生的全部信息。
输入格式: 测试输入包含一个测试用例,该测试用例的第一行输入教师的基本信息(教师姓名 教师工号 年龄),第二行开始输入班级内学生信息,每个学生基本信息占一行(学生姓名 学号 年龄 cpp成绩 cpp考勤),最多不超过100行,当读入0时输入结束。
输入样例:
Mike 901 30
Bob 10001 18 75.9 4
Anna 10003 19 87.0 5
0
输出样例:
901 Michale 2 82.3
10001 Bob 76.3
10003 Anna 88.3
1 #include<string>
2 #include<iomanip>
3 #include <iostream>
4
5 using namespace std;
6 class Person
7 {
8 public:
9 string name;
10 int age;
11 Person() {}
12 Person(string p_name, int p_age)
13 {
14 name = p_name;
15 age = p_age;
16 };
17 void display() { cout << name << ":" << age << endl; }
18 };
19 class Student :public Person
20 {
21 public:
22 int ID;
23 float cpp_score;
24 float cpp_count;
25 float cpp_grade;
26 Student() {}
27 Student(string Name, int id, float a, float b)
28 {
29 name = Name;
30 ID = id;
31 cpp_score = a;
32 cpp_count = b;
33 }
34 void print()
35 {
36 cpp_grade = cpp_grade = cpp_score * 0.9 + cpp_count * 2;
37 cout << ID << " " << name << " " << setiosflags(ios::fixed) << setprecision(1) << cpp_grade << endl;
38 }
39
40 };
41 class Teacher:public Person
42 {
43 public:
44 int i = 0;
45 float aver = 0;
46 int ID;//教师工号
47 Student stu[100];//学生数组
48 int count;//学生数目,最多不超过100
49 float cpp_average;//班级cpp平均分
50 Teacher(string n, int a, int b)
51 {
52 name = n;
53 Teacher::ID = a;
54 age = b;
55 }
56 void Add(Student& stu1)
57 {
58 stu[i].name = stu1.name;
59 stu[i].ID = stu1.ID;
60 stu[i].age = stu1.age;
61 stu[i].cpp_score = stu1.cpp_score;
62 stu[i].cpp_count = stu1.cpp_count;
63 i++;
64 }
65 void average()
66 {
67
68 float sum = 0;
69 for (int j = 0; j < i; j++)
70 {
71 sum = sum+stu[j].cpp_score * 0.9 +stu[j].cpp_count * 2;
72 }
73 aver = sum / i;
74 }
75 void print()
76 {
77 cout << this->ID << " " << this->name << " " << i << " " <<setiosflags(ios::fixed) << setprecision(1)<< aver << endl;
78 for (int j = 0; j < i; j++)
79 {
80 stu[j].print();
81 }
82 }
83 };
84 int main()
85 {
86 string name1;
87 int ID1;
88 int age1;
89 cin >> name1 >> ID1 >> age1;
90 Teacher t(name1, ID1, age1);
91 int ID;
92 string name;
93 int age;
94 float cpp_score;
95 float cpp_count;
96 cin >> name;
97 while (name != "0")
98 {
99 cin >> ID >> age >> cpp_score >> cpp_count;
100 Student a(name, ID, cpp_score, cpp_count);
101 t.Add(a);
102 cin >> name;
103 }
104 t.average();
105 t.print();
106 return 0;
107
108 }