1027. 师生信息管理

给出下面的一个基类框架

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年论文总数。
提示:应用虚函数实现多态

输入样例
1 19 -1 -1 -1 -1 -1
1 125 78 66 -1 95 88
2 68 3 0 7
2 52 0 0 0
1 6999 32 95 100 88 74
0

输出样例
19 5
125 1 81.8
68 10
52 0
6999 0 77.8


时间限制

1000 ms
内存限制
65536 kB
代码长度限制
8192 B
判题程序
Standard
来源
多态


#include<iostream>
#include<iomanip>
using namespace std;
class Person
{
  protected:
    int NO;//编号
  public:
    virtual void display()=0;//输出相关信息
};
class Student:public Person
{
  float s[5];
  int num;
  float sum;
  public:
    Student(){cin>>NO;sum=0;num=0;for(int i=0;i<5;i++){cin>>s[i];if(s[i]!=-1)sum+=s[i];else num++;
    }
    }
    void display();
};
void Student::display()
{
  cout<<NO<<" "<<num;
  if(num!=5)
    cout<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<sum/(5-num);
  cout<<endl;
}
class Teacher:public Person
{
  int s[3];
  int num;
  public:
    Teacher(){cin>>NO;num=0;for(int i=0;i<3;i++) {cin>>s[i];num+=s[i];
    }  }
    void display();
};
void Teacher:: display()
{
  cout<<NO<<" "<<setiosflags(ios::dec)<<num<<endl;
}
int main()
{
  int type,i=0;
  Person *pp[10];
  cin>>type;
  while(type!=0)
  {
    if(type==1)
    {
      pp[i]=new Student;
      pp[i++]->display();
    }
    else
    {
      pp[i]=new Teacher;
      pp[i++]->display();
    }
    cin>>type;
  }
  return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include "string.h" int N,i; FILE *fp; struct student {char num[10]; char name[8]; char sex[5]; int age; char addr[15]; int score; }stu[100]; void input() /*输入学生数据的资料*/ {printf("Input the student data %d:\n",i+1); /*输入一个学生的数据*/ printf("NO.:"); scanf("%s",stu[i].num); /*输入号数*/ printf("name:"); scanf("%s",stu[i].name); /*姓名*/ printf("sex:"); scanf("%s",stu[i].sex); /*性别*/ printf("age:"); scanf("%d",&stu[i].age); /*年龄*/ printf("address:"); scanf("%s",stu[i].addr); /*家庭地址*/ printf("score:"); scanf("%d",&stu[i].score); /*分数*/ printf("\n"); } void add() /*在ouru的文件后添加学生数据*/ { if((fp=fopen("ouru","ab"))==NULL) /*以读写的二进制形式打开ouru文件*/ {printf("Cann't open the file\n"); return;} printf("How many data do you want:"); /*输入需要添加几个学生的数据和具体的资料*/ scanf("%d",&N); for(i=0;i<N;i++) /*向ouru文件中加入添加学生的数据的个数*/ input(); for(i=0;i<N;i++) if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("File error\n"); fclose(fp); } void save() /*保存学生的数据到ouru文件中*/ { int i; if((fp=fopen("ouru","wb"))==NULL) /*打开ouru文件*/ {printf("Cann't open the file\n"); return;} for(i=0;i<N;i++) /*把学生的数据写入到ouru 文件中*/ if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("File write error\n"); fclose(fp); } void insert() /*在ouru文件中插入学生的数据*/ {char positions[10]; int a,b; if((fp=fopen("ouru","r+"))==NULL) /*以读写打开ouru文件*/ {printf("Can not open file!"); return;} for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++); /*读取文件并计算文件中有多少个学生的数据*/ printf("Which position do you want to insert:"); scanf("%s",positions); for(a=0;a<i;a++) /*确定插入的位置*/ if(strcmp(positions,stu[a].num)==0)break; a=a+1; fseek(fp,sizeof(struct student)*(a+1),0); /*将指针指向要插入位置的后一位*/ for(b=a;b<i;b++) fwrite(&stu[b],sizeof(struct student),1,fp); /*将插入后的学生数据向后移动一位*/ i=a; input(); fseek(fp,sizeof(struct student)*a,0); /*将指针指向要插入的位置*/ fwrite(&stu[a],sizeof(struct student),1,fp); /*将插入的数据保存到ouru文件里*/ fclose(fp);} void change() /*修改ouru文件中的某个学生的数据*/ { char positions[10]; if((fp=fopen("ouru","rb+"))==NULL) /*以读写打开二进制ouru文件*/ {printf("Can not open the file\n"); return;} printf("What data do you want to modify:"); /*输入要修改学生的号码*/ scanf("%s",positions); for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++) /*找出要修改的数据*/ if(strcmp(stu[i].num,positions)==0) {fseek(fp,sizeof(struct student)*i,0); /*将指针指向要修改的资料*/ input(); fwrite(&stu[i],sizeof(struct student),1,fp); /*将修改的数据保存*/ break;} fclose(fp); } void del() /*删除ouru中的某个学生的数据*/ {int a,b,f; char positions[10]; if((fp=fopen("ouru","rb+"))==NULL) /*以读写打开ouru文件*/ {printf("Can not open file!"); return;} printf("Input the deleted number:"); /*输入要删除的学生号码*/ scanf("%s",positions); for(i=0;fread(&stu[i],sizeof(struct student),1,fp)!=0;i++); /*查找文件中有多少个学生的数据*/ f=i; for(a=0;a<i;a++) /*找出要删除的学生的数据并把后面的数据全都向前移一位*/ {if(strcmp(positions,stu[a].num)==0) { for(b=a;b<i;b++) {strcpy(stu[b].num,stu[b+1].num); strcpy(stu[b].name,stu[b+1].name); strcpy(stu[b].sex,stu[b+1].sex); stu[b].age=stu[b+1].age; strcpy(stu[b].addr,stu[b+1].addr); stu[b].score=stu[b+1].score;} f=f-1; break;} } fclose(fp); fopen("ouru","w"); /*将删除后的结果保存到ouru文件里*/ for(a=0;a<f;a++) fwrite(&stu[a],sizeof(struct student),1,fp); fclose(fp);} void examine() /*查看ouru文件中的资料*/ {int i; if((fp=fopen("ouru","r"))==NULL) /*以只读的方式打开ouru文件*/ {printf("Cannot open the file\n"); return;} printf("\n"); printf("num name sex age addr score\n"); /*读取各个学生数据的同时把它们显示出来*/ for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++) {printf("%-10s%-10s%-8s%-9d%-17s%d\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].addr,stu[i].score);} printf("\n"); fclose(fp); } main() {int choose; /*显示主界面*/ printf("********************************************************************************\n"); printf("--------------------------------------------------------------------------------\n\n"); printf(" Welcome to the student system\n"); printf(" Producer:ouru\tRegistration number:03011123\n"); printf("\n\n--------------------------------------------------------------------------------\n"); printf("********************************************************************************\n"); printf("Please the choice:\n"); printf("\t\t\t1. Input the student data\n"); printf("\t\t\t2. Add the new studetn data\n"); printf("\t\t\t3. Change a data\n"); printf("\t\t\t4. Insert a new data\n"); printf("\t\t\t5. Delete the student data\n"); printf("\t\t\t6. Manifestation student data\n"); printf("\t\t\t7. Main menu\n"); printf("\t\t\t8. exit\n\n\n"); scanf("%d",&choose); /*选择想要操作的选项*/ while(choose!=8) /*当选择8的时候不再循环并退出*/ { switch(choose) {case 1:printf("Please input the student total:"); /*当choose为下面的数时执行相应的功能*/ scanf("%d",&N); for(i=0;i<N;i++) input(); save();break; case 2:add();break; case 3:change();break; case 4:insert();break; case 5:del();break; case 6:examine();break; case 7:printf("\n\n\nPlease the choice:\n"); printf("\t\t\t1. Input the student data\n"); printf("\t\t\t2. Add the new studetn data\n"); printf("\t\t\t3. Change a data\n"); printf("\t\t\t4. Insert a new data\n"); printf("\t\t\t5. Delete the student data\n"); printf("\t\t\t6. Manifestation student data\n"); printf("\t\t\t7. Main menu\n"); printf("\t\t\t8. exit\n\n\n");break; default :printf("error\n");} scanf("%d",&choose); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值