师生信息管理

给出下面的一个基类框架
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年论文总数。
(PS:未使用 Person *pp[10]; )

 

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 using namespace std;
 5 
 6 class Person
 7 {
 8 protected:
 9     int NO;//编号
10 public:
11     virtual void display()=0;//输出相关信息
12     virtual ~Person(){};
13 };
14 
15 class Student:public Person
16 {
17     int score[5];
18 public:
19     Student(){};
20     ~Student(){};
21     friend istream& operator >> (istream &input,Student &S);  //重载输入流
22     void display();
23 };
24 
25 istream& operator >> (istream &input,Student &S)
26 {
27     input>>S.NO>>S.score[0]>>S.score[1]>>S.score[2]>>S.score[3]>>S.score[4];
28     return input;
29 }
30 
31 void Student::display()
32 {
33     double average(0);
34     int count(0);
35     for(int i(0);i<5;i++)
36     {
37         if(score[i]==-1) count++;
38         else average+=score[i];
39     }
40     average/=(5-count);
41     if(count==5) cout<<NO<<" "<<count<<endl;
42     else cout<<NO<<" "<<count<<" "<<fixed<<setprecision(1)<<average<<endl;
43 }
44 
45 class Teacher:public Person
46 {
47     int num[3];
48 public:
49     Teacher(){};
50     ~Teacher(){};
51     friend istream& operator >> (istream &input,Teacher &T);
52     void display();
53 };
54 
55 istream& operator >> (istream &input,Teacher &T)
56 {
57     input>>T.NO>>T.num[0]>>T.num[1]>>T.num[2];
58     return input;
59 }
60 
61 void Teacher::display()
62 {
63     int number(0);
64     for(int i(0);i<3;i++)
65     {
66         number+=num[i];
67     }
68     cout<<NO<<" "<<number<<endl;
69 }
70 
71 int main()
72 {
73     int flag;
74     while(cin>>flag,flag)
75     {
76         switch(flag)
77         {
78         case 1:
79             {
80                 Student *p=new Student;
81                 cin>>*p;
82                 p->display();
83                 delete p;
84                 break;
85             }
86         case 2:
87             {
88                 Teacher *p=new Teacher;
89                 cin>>*p;
90                 p->display();
91                 delete p;
92                 break;
93             }
94         }
95     }
96     return 0;
97 }

 

 

(使用 Person *pp[10;])

  1 #include<iostream>
  2 #include<string>
  3 #include<iomanip>
  4 using namespace std;
  5 
  6 class Person
  7 {
  8 protected:
  9     int NO;//编号
 10 public:
 11     Person() {};
 12     Person(int N): NO(N) {};
 13     virtual void display() = 0; //输出相关信息
 14     virtual ~Person() {};
 15 };
 16 
 17 class Student: public Person
 18 {
 19     int score[5];
 20 public:
 21     Student() {};
 22     Student(int N, int S1, int S2, int S3, int S4, int S5): Person(N)
 23     {
 24         score[0] = S1, score[1] = S2, score[2] = S3, score[3] = S4, score[4] = S5;
 25     };
 26     ~Student() {};
 27     //friend istream& operator >> (istream &input,Student &S);
 28     void display();
 29 };
 30 /*
 31 istream& operator >> (istream &input,Student &S)
 32 {
 33     input>>S.NO>>S.score[0]>>S.score[1]>>S.score[2]>>S.score[3]>>S.score[4];
 34     return input;
 35 }
 36 */
 37 void Student::display()
 38 {
 39     double average(0);
 40     int count(0);
 41 
 42     for(int i(0); i < 5; i++)
 43     {
 44         if(score[i] == -1) count++;
 45         else average += score[i];
 46     }
 47 
 48     average /= (5 - count);
 49 
 50     if(count == 5) cout << NO << " " << count << endl;
 51     else cout << NO << " " << count << " " << fixed << setprecision(1) << average << endl;
 52 }
 53 
 54 class Teacher: public Person
 55 {
 56     int num[3];
 57 public:
 58     Teacher() {};
 59     Teacher(int N, int N1, int N2, int N3): Person(N)
 60     {
 61         num[0] = N1, num[1] = N2, num[2] = N3;
 62     };
 63     ~Teacher() {};
 64     //friend istream& operator >> (istream &input,Teacher &T);
 65     void display();
 66 };
 67 /*
 68 istream& operator >> (istream &input,Teacher &T)
 69 {
 70     input>>T.NO>>T.num[0]>>T.num[1]>>T.num[2];
 71     return input;
 72 }
 73 */
 74 void Teacher::display()
 75 {
 76     int number(0);
 77 
 78     for(int i(0); i < 3; i++)
 79     {
 80         number += num[i];
 81     }
 82 
 83     cout << NO << " " << number << endl;
 84 }
 85 
 86 int main()
 87 {
 88     int flag, count(0);
 89     Person *pp[10];
 90 
 91     while(cin >> flag, flag)
 92     {
 93         switch(flag)
 94         {
 95             case 1:
 96                 {
 97                     int N, S1, S2, S3, S4, S5;
 98                     cin >> N >> S1 >> S2 >> S3 >> S4 >> S5;
 99                     pp[count++] = new Student(N, S1, S2, S3, S4, S5);
100                     pp[count - 1]->display();
101                     break;
102                 }
103 
104             case 2:
105                 {
106                     int N, N1, N2, N3;
107                     cin >> N >> N1 >> N2 >> N3;
108                     pp[count++] = new Teacher(N, N1, N2, N3);
109                     pp[count - 1]->display();
110                     break;
111                 }
112         }
113     }
114 
115     return 0;
116 }

 

posted @ 2019-04-22 22:19  菜鸟plus  阅读(696)  评论(0编辑  收藏  举报