分享一下
//def.h#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
class Teacher
{
public:
char *Getc();
virtual void Input();
virtual void Output();
virtual void DL();
private:
char a[10];
char c[20];
char d[8];
char b[15];
friend class Postdocteral;
};
class College
{
public:
char *Gets();
void Input();
void Output();
void Output2();
void Outpute();
void Outputf();
void Outputg();
void OutputT();
float Ave();
int Total();
void DL();
friend class System;
private:
char m[10];
char s[10];
char t[2];
char n[8];
int e,f,g;
};
class Master
{
public:
char *GetName();
virtual void Input();
virtual void Output();
virtual void DL();
private:
char Name[10];
char Number[10];
char n[8];
char t[3];
char GuideTeacher[20];
char Study[80];
friend class Postdocteral;
};
class Postdocteral:virtual public Teacher,Master//博士后
{
public:
void Input();
void Output();
char *GetName();
void DL();
};
class System
{
private:
Teacher A[10];
College B[20];
Master C[10];
Postdocteral D[10];
static int j1,j2,j3,j4;
void infor1();
void infor2();
void infor3();
void infor4();
void outfor1();
void outfor2();
void outfor3();
void outfor4();
void outfor5();
void Search1(int h,char ch[20]);
void Interface2();
void Interface3();
void Delete1(int h,char ch[20]);
public:
System(){};
void In_information();
void Search();
void Out_information();
void Interface1();
void Delete();
};
//carry out.cpp
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
#include<iomanip.h>
#include"def.h"
char fileName[]="company.txt";//输出文件
char *Teacher::Getc(){return c;} //教师
char *College::Gets(){return s;} //学生
char *Master::GetName(){return Name;} //研究生
char *Postdocteral::GetName(){return Name;} //博士后
///////////////////////////////////////////////////输入输出实现
void Teacher::Input()
{ cout<<"\t\t老师编号:";
cin>>a;
cout<<"\t\t 姓名:";
cin>>c;
cout<<"\t\t 职称:";
cin>>d;
cout<<"\t\t 教研室数据:";
cin>>b;
}
void Teacher::Output()
{ cout<<"\t\t老师编号:"<<a<<endl;
cout<<"\t\t 姓名:"<<c<<endl;
cout<<"\t\t 职称:"<<d<<endl;
cout<<"\t\t 教研室数据:"<<b<<endl;
}
void College::Input()
{ cout<<"\t\t大学生编号:";
cin>>m;
cout<<"\t\t 姓名:";
cin>>s;
cout<<"\t\t 班号:";
cin>>n;
cout<<"\t\t 性别:";
cin>>t;
cout<<"\t\t 英语:";
cin>>e;
if (e<0||e>100) {cout<<"成绩输入错误"<<endl;cout<<"\t\t英语:";cin>>t;}
cout<<"\t\t高等数学:";
cin>>f;
if (f<0||f>100) {cout<<"成绩输入错误"<<endl;cout<<"\t\t高等数学:";cin>>t;}
cout<<"\t\t数据结构:";
cin>>g;
if (g<0||g>100) {cout<<"成绩输入错误"<<endl;cout<<"\t\t数据结构:";cin>>t;}
}
void College::Output()
{ cout<<"\t\t大学生编号:"<<m<<endl;
cout<<"\t\t 姓名:"<<s<<endl;
cout<<"\t\t 班号:"<<n<<endl;
cout<<"\t\t 性别:"<<t<<endl;
cout<<"\t\t 英语:"<<e<<endl;
cout<<"\t\t 高等数学:"<<f<<endl;
cout<<"\t\t 数据结构:"<<g<<endl;
}
void College::Output2()
{ cout<<"\t编号:"<<m<<"\t"<<"姓名:"<<s<<"\t"<<"平均分:"<<setprecision(3)<<Ave()<<"\t"<<"总分:"<<Total()<<endl;}
float College::Ave()
{return float(e+f+g)/3;}
int College::Total()
{return e+f+g;}
void College::Outpute()
{ cout<<"\t编号:"<<m<<"\t"<<"姓名:"<<s<<"\t"<<"英语:"<<e<<endl;}
void College::Outputf()
{ cout<<"\t编号:"<<m<<"\t"<<"姓名:"<<s<<"\t"<<"高等数学:"<<f<<endl;}
void College::Outputg()
{ cout<<"\t编号:"<<m<<"\t"<<"姓名:"<<s<<"\t"<<"数据结构:"<<g<<endl;}
void College::OutputT()
{ cout<<"\t编号:"<<m<<"\t"<<"姓名:"<<s<<"\t"<<"总分:"<<Total()<<endl;}
void Master::Input()
{ cout<<"\t\t研究生编号:";
cin>>Number;
cout<<"\t\t 姓名:";
cin>>Name;
cout<<"\t\t 班号:";
cin>>n;
cout<<"\t\t 性别:";
cin>>t;
cout<<"\t\t 指导教师:";
cin>>GuideTeacher;
cout<<"\t\t 研究方法:";
cin>>Study;
}
void Master::Output()
{ cout<<"\t\t研究生编号:"<<Number<<endl;
cout<<"\t\t 姓名:"<<Name<<endl;
cout<<"\t\t 班号:"<<n<<endl;
cout<<"\t\t 性别:"<<t<<endl;
cout<<"\t\t 指导教师:"<<GuideTeacher<<endl;
cout<<"\t\t 研究方法:"<<Study<<endl;
}
void Postdocteral::Input()
{ cout<<"\t\t博士后编号:";
cin>>Number;
cout<<"\t\t 姓名:";
cin>>Name;
cout<<"\t\t 班号:";
cin>>n;
cout<<"\t\t 性别:";
cin>>t;
cout<<"\t\t 职称:";
cin>>d;
cout<<"\t\t 教研室数据:";
cin>>b;
cout<<"\t\t 指导教师:";
cin>>GuideTeacher;
cout<<"\t\t 研究方法:";
cin>>Study;
}
void Postdocteral::Output()
{ cout<<"\t\t博士后编号:"<<Number<<endl;
cout<<"\t\t 姓名:"<<Name<<endl;
cout<<"\t\t 班号:"<<n<<endl;
cout<<"\t\t 性别:"<<t<<endl;
cout<<"\t\t 职称:"<<d<<endl;
cout<<"\t\t 教研室数据:"<<b<<endl;
cout<<"\t\t 指导教师:"<<GuideTeacher<<endl;
cout<<"\t\t 研究方法:"<<Study<<endl;
}
//////////////////////////////////////////////写入文件的人数
int System::j1=0;
int System::j2=0;
int System::j3=0;
int System::j4=0;
void System::In_information()
{
int rev1;
int again=1;
char t;
while(again)
{
cout<<"当前目录——基本信息输入"<<endl;
Interface2();
cin>>rev1;
switch(rev1)
{
case 1:
infor1();
break;
case 2:
infor2();
break;
case 3:
infor3();
break;
case 4:
infor4();
break;
case 5:
Interface1();
break;
default:
cout<<"\t\t\t 对不起、没有此类人员!"<<endl;
continue;
}
cout<<"\t\t\t 信息储存成功!"<<endl;
cout<<"\t\t\t 是否继续输入(y/n)?";
cin>>t;
cout<<endl;
if(!(t=='Y'||t=='y'))
again=0;
}
Interface1();
}
////////////////////////////////////////////////////菜单类
//主菜单
void System::Interface1()
{
int rev;
cout<<"\a";
cout<<"\n\n\n";
cout<<"\t\t+++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<"\t\t+ 总目录 +"<<endl;
cout<<"\t\t+ +"<<endl;
cout<<"\t\t+ 一 基本信息输入 +"<<endl;
cout<<"\t\t+ 二 查询信息并显示 +"<<endl;
cout<<"\t\t+ 三 删除信息 +"<<endl;
cout<<"\t\t+ 四 学生三门课程情况 +"<<endl;
cout<<"\t\t+ 五 退出 +"<<endl;
cout<<"\t\t+ +"<<endl;
cout<<"\t\t+++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<" 请输入您的选择(1~5):";
cin>>rev;
switch(rev)
{
case 1:
In_information();
break;
case 2:
Search();
break;
case 3:
Delete();
break;
case 4:
Out_information();
break;
case 5:
cout<<endl<<endl;
cout<<"\t\t\t 谢谢您使用本系统,下次再见!"<<endl;
cout<<" 作者 :孙盛博 王骏 刘凯宇 姚鹏"<<endl;
system("pause");
}
}
void System::Out_information()
{int rev1;
int again=1;
char t;
while(again)
{
Interface3();
cin>>rev1;
switch(rev1)
{
case 1:
outfor1();
break;
case 2:
outfor2();
break;
case 3:
outfor3();
break;
case 4:
outfor4();
break;
case 5:
outfor5();
break;
case 6:
Interface1();
break;
default:
cout<<"\t\t\t 对不起、没有此选项!"<<endl;
continue;
}
cout<<"\t\t\t 是否继续查看(y/n)?";
cin>>t;
cout<<endl;
if(!(t=='Y'||t=='y'))
again=0;
}
Interface1();
}
//功能一 二 三 的子菜单
void System::Interface2()
{
cout<<"\t 1.教师类 "<<endl;
cout<<"\t 2.大学生类 "<<endl;
cout<<"\t 3.研究生类 "<<endl;
cout<<"\t 4.博士生类 "<<endl;
cout<<"\t 5.退出 "<<endl;
cout<<"\t 请输入大学师生类别(1~5): ";
}
//功能四的子菜单
void System::Interface3()
{
cout<<"\n\n\n";
cout<<"当前目录——学生三门课程情况"<<endl;
cout<<" 1.学生总分、平均分查询 "<<endl;
cout<<" 2.英语成绩排序 "<<endl;
cout<<" 3.高等数学成绩排序 "<<endl;
cout<<" 4.数据结构成绩排序 "<<endl;
cout<<" 5.总分排序 "<<endl;
cout<<" 6.返回 "<<endl;
cout<<" 请输入处理方式(1~6): ";
}
/////////////////////////////////////////////////////////////输入输出等功能的实现 分为4个类别
void System::infor1()
{
fstream datafile(fileName,ios::in|ios::out|ios::binary);
datafile.seekp(0,ios::end);
A[j1].Input();
datafile.write((char *)&A[j1],sizeof(class Teacher));
datafile.close();
j1++;
}
void System::infor2()
{
fstream datafile(fileName,ios::in|ios::out|ios::binary);
datafile.seekp(0,ios::end);
B[j2].Input();
datafile.write((char *)&B[j2],sizeof(class College));
datafile.close();
j2++;
}
void System::infor3()
{
fstream datafile(fileName,ios::in|ios::out|ios::binary);
datafile.seekp(0,ios::end);
C[j3].Input();
datafile.write((char *)&C[j3],sizeof(class Master));
datafile.close();
j3++;
}
void System::infor4()
{
fstream datafile(fileName,ios::in|ios::out|ios::binary);
datafile.seekp(0,ios::end);
D[j3].Input();
datafile.write((char *)&D[j3],sizeof(class Postdocteral));
datafile.close();
j3++;
}
/////////////////////////////////////////////////////////////////查询功能的实现
void System::Search1(int h,char ch[20])
{
int s=0,found=0;
switch(h)
{
case 1:
while(s<j1)
{
if(strcmp(ch,A[s].Getc())==0)
{
A[s].Output();
cout<<"\t\t\t————————————————"<<endl;
found=1;break;
}
s++;
}
break;
case 2:
while(s<j2)
{
if(strcmp(ch,B[s].Gets())==0)
{
B[s].Output();
cout<<"\t\t\t————————————————"<<endl;
found=1;break;
}
s++;
}
break;
case 3:
while(s<j3)
{
if(strcmp(ch,C[s].GetName())==0)
{
C[s].Output();
cout<<"\t\t\t————————————————"<<endl;
found=1;break;
}
s++;
}
break;
case 4:
while(s<j4)
{
if(strcmp(ch,D[s].GetName())==0)
{
D[s].Output();
cout<<"\t\t\t————————————————"<<endl;
found=1;break;
}
s++;
}
break;
}
if(found==0)
cout<<"\n\n\t\t 对不起,该类别中并没有此人!"<<endl;
}
void System::Search()
{
int rev;
char name[20];
int again=1;
char t;
while(again)
{
cout<<"当前目录——查询信息并显示 "<<endl;
Interface2();
cin>>rev;
if(rev==5) break;
cout<<"\t\t\t请输入您要查询的人的姓名:";
cin>>name;
Search1(rev,name);
cout<<"\t\t\t是否继续查询(y/n)?";
cin>>t;
cout<<endl;
if(!(t=='Y'||t=='y'))
again=0;
}
Interface1();
}
void System::Delete1(int h,char ch[20])
{
int s=0,found=0;
switch(h)
{
case 1:
while(s<j1)
{
if(strcmp(ch,A[s].Getc())==0)
{
char t;
A[s].Output();
cout<<"\t\t\t是否确定删除?(y/n)"<<endl;
cin>>t;
if(t=='Y'||t=='y')
{A[s].DL();
fstream datafile(fileName,ios::in|ios::out|ios::binary);
datafile.seekp(0,ios::end);
datafile.write((char *)&A[s],sizeof(class Postdocteral));
datafile.close();
cout<<"\t\t\t删除成功!"<<endl;
cout<<"\t\t\t*****************"<<endl;
}
else Interface1();
found=1;break;
}
s++;
}
break;
case 2:
while(s<j2)
{
if(strcmp(ch,B[s].Gets())==0)
{
char t;
B[s].Output();
cout<<"\t\t\t是否确定删除?(y/n)"<<endl;
cin>>t;
if(t=='Y'||t=='y')
{B[s].DL();
fstream datafile(fileName,ios::in|ios::out|ios::binary);
datafile.seekp(0,ios::end);
datafile.write((char *)&B[s],sizeof(class Postdocteral));
datafile.close();
cout<<"\t\t\t删除成功!"<<endl;
cout<<"\t\t\t*****************"<<endl;
}
else Interface1();
found=1;break;
}
s++;
}
break;
case 3:
while(s<j3)
{
if(strcmp(ch,C[s].GetName())==0)
{
char t;
C[s].Output();
cout<<"\t\t\t是否确定删除?(y/n)"<<endl;
cin>>t;
if(t=='Y'||t=='y')
{C[s].DL();
fstream datafile(fileName,ios::in|ios::out|ios::binary);
datafile.seekp(0,ios::end);
datafile.write((char *)&C[s],sizeof(class Postdocteral));
datafile.close();
cout<<"\t\t\t删除成功!"<<endl;
cout<<"\t\t\t*****************"<<endl;
}
else Interface1();
found=1;break;
}
s++;
}
break;
case 4:
while(s<j4)
{
if(strcmp(ch,D[s].GetName())==0)
{
char t;
D[s].Output();
cout<<"\t\t\t是否确定删除?(y/n)";
cin>>t;
if(t=='Y'||t=='y')
{D[s].DL();
fstream datafile(fileName,ios::in|ios::out|ios::binary);
datafile.seekp(0,ios::end);
datafile.write((char *)&D[s],sizeof(class Postdocteral));
datafile.close();
cout<<"\t\t\t删除成功!"<<endl;
cout<<"\t\t\t*****************"<<endl;
}
else Interface1();
found=1;break;
}
s++;
}
break;
}
if(found==0)
cout<<"\n\n\t\t 对不起,该类别中并没有此人!"<<endl;
}
/////////////////////////////////////////////////////删除实现
void Teacher::DL()
{ strcpy(a,"");
strcpy(c,"");
strcpy(d,"");
strcpy(b,"");
}
void College::DL()
{ strcpy(m,"");
strcpy(s,"");
strcpy(t,"");
strcpy(n,"");
}
void Master::DL()
{ strcpy(Name,"");
strcpy(Name,"");
strcpy(n,"");
strcpy(t,"");
strcpy(GuideTeacher,"");
strcpy(Study,"");
}
void Postdocteral::DL()
{ strcpy(Name,"");
strcpy(Name,"");
strcpy(n,"");
strcpy(t,"");
strcpy(d,"");
strcpy(b,"");
strcpy(GuideTeacher,"");
strcpy(Study,"");
}
void System::Delete()
{
int rev;
char name[20];
int again=1;
char t;
while(again)
{
cout<<"当前目录——删除信息"<<endl;
Interface2();
cin>>rev;
if(rev==5) break;
cout<<"\t\t\t请输入您要删除的人的姓名:";
cin>>name;
Delete1(rev,name);
cout<<"\t\t\t是否继续删除(y/n)?";
cin>>t;
cout<<endl;
if(!(t=='Y'||t=='y'))
again=0;
}
Interface1();
}
//////////////////////////////////////////////////////输出排序
//计算总分
void System::outfor1()
{ int i;
cout<<"\n\n\n";
for (i=0;i<j2;i++)
B[i].Output2();
}
//英语成绩排序
void System::outfor2()
{
int i,j;
College temp;
for (i=0;i<j2-1;i++)
for (j=0;j<j2-i-1;j++)
if (B[j].e<B[j+1].e)
{ strcpy(temp.m,B[j].m);
strcpy(temp.s,B[j].s);
strcpy(temp.t,B[j].t);
strcpy(temp.n,B[j].n);
temp.e=B[j].e;
temp.g=B[j].g;
temp.f=B[j].f;
strcpy(B[j].m,B[j+1].m);
strcpy(B[j].s,B[j+1].s);
strcpy(B[j].t,B[j+1].t);
strcpy(B[j].n,B[j+1].n);
B[j].e=B[j+1].e;
B[j].g=B[j+1].g;
B[j].f=B[j+1].f;
strcpy(B[j+1].m,temp.m);
strcpy(B[j+1].s,temp.s);
strcpy(B[j+1].t,temp.t);
strcpy(B[j+1].n,temp.n);
B[j+1].e=temp.e;
B[j+1].g=temp.g;
B[j+1].f=temp.f;
}
for (i=0;i<j2;i++)
B[i].Outpute();
}
//高等数学成绩排序
void System::outfor3()
{int i,j;
College temp;
for (i=0;i<j2-1;i++)
for (j=0;j<j2-i-1;j++)
if (B[j].f<B[j+1].f)
{ strcpy(temp.m,B[j].m);
strcpy(temp.s,B[j].s);
strcpy(temp.t,B[j].t);
strcpy(temp.n,B[j].n);
temp.e=B[j].e;
temp.g=B[j].g;
temp.f=B[j].f;
strcpy(B[j].m,B[j+1].m);
strcpy(B[j].s,B[j+1].s);
strcpy(B[j].t,B[j+1].t);
strcpy(B[j].n,B[j+1].n);
B[j].e=B[j+1].e;
B[j].g=B[j+1].g;
B[j].f=B[j+1].f;
strcpy(B[j+1].m,temp.m);
strcpy(B[j+1].s,temp.s);
strcpy(B[j+1].t,temp.t);
strcpy(B[j+1].n,temp.n);
B[j+1].e=temp.e;
B[j+1].g=temp.g;
B[j+1].f=temp.f;
}
for (i=0;i<j2;i++)
B[i].Outputf();
}
//数据结构成绩排序
void System::outfor4()
{int i,j;
College temp;
for (i=0;i<j2-1;i++)
for (j=0;j<j2-i-1;j++)
if (B[j].g<B[j+1].g)
{ strcpy(temp.m,B[j].m);
strcpy(temp.s,B[j].s);
strcpy(temp.t,B[j].t);
strcpy(temp.n,B[j].n);
temp.e=B[j].e;
temp.g=B[j].g;
temp.f=B[j].f;
strcpy(B[j].m,B[j+1].m);
strcpy(B[j].s,B[j+1].s);
strcpy(B[j].t,B[j+1].t);
strcpy(B[j].n,B[j+1].n);
B[j].e=B[j+1].e;
B[j].g=B[j+1].g;
B[j].f=B[j+1].f;
strcpy(B[j+1].m,temp.m);
strcpy(B[j+1].s,temp.s);
strcpy(B[j+1].t,temp.t);
strcpy(B[j+1].n,temp.n);
B[j+1].e=temp.e;
B[j+1].g=temp.g;
B[j+1].f=temp.f;
}
for (i=0;i<j2;i++)
B[i].Outputg();
}
//总分排序
void System::outfor5()
{int i,j;
College temp;
for (i=0;i<j2-1;i++)
for (j=0;j<j2-i-1;j++)
if (B[j].Total()<B[j+1].Total())
{ strcpy(temp.m,B[j].m);
strcpy(temp.s,B[j].s);
strcpy(temp.t,B[j].t);
strcpy(temp.n,B[j].n);
temp.e=B[j].e;
temp.g=B[j].g;
temp.f=B[j].f;
strcpy(B[j].m,B[j+1].m);
strcpy(B[j].s,B[j+1].s);
strcpy(B[j].t,B[j+1].t);
strcpy(B[j].n,B[j+1].n);
B[j].e=B[j+1].e;
B[j].g=B[j+1].g;
B[j].f=B[j+1].f;
strcpy(B[j+1].m,temp.m);
strcpy(B[j+1].s,temp.s);
strcpy(B[j+1].t,temp.t);
strcpy(B[j+1].n,temp.n);
B[j+1].e=temp.e;
B[j+1].g=temp.g;
B[j+1].f=temp.f;
}
for (i=0;i<j2;i++)
B[i].OutputT();
}
void main()
{
cout<<endl;
cout<<" ------------欢迎使用《大学师生管理程序》------------"<<endl;
System s;
s.Interface1();
}