?? c++.txt
字號:
//高校學(xué)籍管理系統(tǒng)
//頭文件及類
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
class student
{
protected:
int number;
char name[20];
char sex[6];
char place[20];
char nation[6];
char birth[20];
char party[10];
char id[20];
float score[3];
public:
student *next;
student(){ }
~student(){ }
char* getname(){ return name; }
int getnumber(){ return number;}
double getscore(int i) { return score[i];}
float getg(){ return (score[0]+score[1]+score[2]); }
void input()
{
int e=1;
cout<<"\t\t\t按提示輸入:"<<endl;
cout<<"\t\t輸入編號: ";
cin>>number;
cout<<"\t\t輸入姓名: ";
cin>>name;
do
{
cout<<"\t\t輸入性別: ";
cin>>sex;
if(strcmp(sex,"男")==0 || strcmp(sex,"女")==0)
{
cout<<"\t\t輸入籍貫: ";
cin>>place;
cout<<"\t\t輸入民族: ";
cin>>nation;
cout<<"\t\t輸入生日: ";
cin>>birth;
cout<<"\t\t輸入政治面貌: ";
cin>>party;
cout<<"\t\t輸入身份證號: ";
cin>>id;
cout<<"\t\t輸入數(shù)學(xué)分?jǐn)?shù): ";
cin>>score[0];
cout<<"\t\t輸入英語分?jǐn)?shù): ";
cin>>score[1];
cout<<"\t\t輸入計算機分?jǐn)?shù): ";
cin>>score[2];
e=0;
}
else
{
cout<<"\t\t\t無此類型性別!重新輸入!"<<endl;
e=1;
}
}while(e);
return ;
}
void input(ifstream & is)
{
is>>number>>name>>sex>>place>>nation>>birth>>party>>id
>>score[0]>>score[1]>>score[2];
is.get();
}
void output()
{
cout<<"學(xué)生基本信息如下:"<<endl;
cout<<"編號:"<<number
<<" 姓名:"<<name
<<" 性別:"<<sex
<<" 籍貫:"<<place
<<" 民族:"<<nation
<<" 生日:"<<birth
<<" 政治面貌:"<<party<<endl
<<"身份證號:"<<id
<<" 數(shù)學(xué):"<<score[0]
<<" 英語:"<<score[1]
<<" 計算機:"<<score[2]
<<" 總分:"<<getg()<<endl<<endl;
}
void output(ofstream & os)
{
os<<setw(6)<<number
<<setw(15)<<name
<<setw(6)<<sex
<<setw(20)<<place
<<setw(6)<<nation
<<setw(20)<<birth
<<setw(20)<<party
<<setw(20)<<id
<<setw(6)<<score[0]
<<setw(6)<<score[1]
<<setw(6)<<score[2]<<endl;
}
};
class school
{
public:
school(){ head=new student; head->next=NULL; key=0; }
~school(){ delete head; }
void input();
void mend();
void del();
int find(student **p,int num,char *pn="^");
void found();
void show();
void count();
void save();
void begin();
void clear();
char mainmenu();
int getkey(){ return key;}
void setkey(int k){ key=k; }
private:
student *head;
int key;
};
//錄入函數(shù)
void school::input()
{
student *p,*p2=NULL;
p=head;
int n;
while(p->next)
p=p->next;
while(n)
{
p2=new student;
p2->input();
p->next=p2;
p2->next=NULL;
p=p->next;
school::setkey(1);
cout<<"\t\t\t按1繼續(xù),按0返回 : ";
cin>>n;
}
}
//子查找函數(shù)
int school::find(student **p1,int num,char *pn)
{
student *p;
p=head;
while(p->next)
{
(*p1)=p;
if( (p->next)->getnumber()==num||!strcmp( (p->next)->getname(),pn ) )
return 1;
p=p->next;
}
return 0;
}
//查找函數(shù)
void school::found()
{
student *p;
int num=-1,n=9;
char name[20]="^";
do
{
cout<<"\t\t1:按編號查找,2:按姓名查找: ";
cin>>n;
}while(n<1||n>2);
if(n= =1)
{
cout<<"\t\t\t輸入編號: ";
cin>>num;
}
if(n= =2)
{
cout<<"\t\t\t輸入姓名: ";
cin>>name;
}
if(!find(&p,num,name) )
{
cout<<"\t\t找不到你要查找的內(nèi)容!"<<endl;
return;
}
(p->next)->output();
}
//刪除函數(shù)
void school::del()
{
student *p,*p2;
int num;
cout<<"\t\t\t輸入編號: ";
cin>>num;
if( !find(&p,num,"^") )
{
cout<<"\t\t找不到你要刪除的內(nèi)容!"<<endl;
return;
}
(p->next)->output();
p2=p->next;
p->next=p2->next;
delete p2;
school::setkey(1);
}
//顯示函數(shù)
void school::show()
{
student *p;
p=head;
while(p->next)
{
(p->next)->output();
p=p->next;
}
}
//修改函數(shù)
void school::mend()
{
student *p;
int num=-1,n;
char name[20]="^";
do
{
cout<<"\t\t1:按編號修改,2:按姓名修改: ";
cin>>n;
}while(n<1||n>2);
if(n==1)
{
cout<<"\t\t\t輸入編號: ";
cin>>num;
}
if(n==2)
{
cout<<"\t\t\t輸入姓名: ";
cin>>name;
}
if( !find(&p,num,name) )
{
cout<<"\t\t找不到你要修改的內(nèi)容!"<<endl;
return;
}
(p->next)->output();
(p->next)->input();
school::setkey(1);
}
//保存函數(shù)
void school::save()
{
student *p;
p=head;
ofstream os("student.txt",ios::out);
if (school::getkey()==1)
{
while(p->next)
{
(p->next)->output(os);
p=p->next;
}
}
cout<<"\t\t\t文件已保存! "<<endl;
school::setkey(0);
}
//初始化函數(shù)
void school::begin()
{
student *p,*p2;
p=head;
clear();
long t;
ifstream is("student.txt",ios::in);
if(!is)
{
ofstream os("student.txt",ios::out);
os.close();
return ;
}
int num=-1;
while(1)
{
num=-1;
t=is.tellg();
is>>num;
is.seekg(t);
if(num<0)
{
is.close();
return;
}
p2=new student;
p2->input(is);
p->next=p2;
p2->next=NULL;
p=p->next;
}
}
//清空函數(shù)
void school::clear()
{
student *p,*p2;
p=head->next;
while( p )
{
p2=p;
p=p->next;
delete p2;
}
}
//統(tǒng)計函數(shù)
void school::count()
{
student *p;
p=head;
int n=0;
double g[3]={0,0,0};
float j[3]={0,0,0};
while(p->next)
{
p=p->next;
n++;
for(int i=0;i<3;i++)
{
g[i]=g[i]+( p->getscore(i) );
(p->getscore(i) )>=60? j[i]++ : 0 ;
}
}
cout<<"\t\t\b\b\b\b數(shù)學(xué)總分:"<<g[0]<<" 平均分:"<<g[0]/n
<<" 及格率:"<<j[0]/n<<endl<<"\t\t\b\b\b\b英語總分:"<<g[1]
<<" 平均分:"<<g[1]/n<<" 及格率:"<<j[1]/n<<endl
<<"\t\t\b\b\b\b計算機總分: "<<g[2]<<" 平均分: "<<g[2]/n
<<" 及格率:"<<j[2]/n<<endl;
}
//主選菜單函數(shù)
char school::mainmenu()
{
char n[6];
cout<<"\n\n ☆☆☆☆歡迎進入高校學(xué)籍管理系統(tǒng)☆☆☆☆"<<endl<<endl;
cout<<" 作者:計算機0403班 王德軍 "<<endl<<endl
<<"* * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl
<<"* * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl
<<"* * 1: 錄入學(xué)生信息 * *"<<endl
<<"* * 2: 顯示學(xué)生信息 * *"<<endl
<<"* * 3: 查找學(xué)生信息 * *"<<endl
<<"* * 4: 刪除學(xué)生信息 * *"<<endl
<<"* * 5: 修改學(xué)生信息 * *"<<endl
<<"* * 6: 統(tǒng)計學(xué)生成績 * *"<<endl
<<"* * 7: 保存學(xué)生信息 * *"<<endl
<<"* * 0: 退出系統(tǒng) * *"<<endl
<<"* * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl
<<"* * * * * * * * * * * * * * * * * * * * * * * * * * * *"<<endl
<<" 請選擇:";
cin>>n;
return n[0];
}
//主函數(shù)
void main()
{
school pp;
int k=1;
char n;
pp.begin();
while(k==1)
{
n=pp.mainmenu();
switch(n)
{
case '1':pp.input(); break;
case '2':pp.show(); break;
case '3':pp.found(); break;
case '4':pp.del(); break;
case '5':pp.mend(); break;
case '6':pp.count(); break;
case '7':pp.save(); break;
case '0':
if(pp.getkey()==1)
{
cout<<"\t\t\t是否保存? 1 : 保存 0:不保存 : ";
cin>>k;
if(k==1)
pp.save();
}
pp.clear();
k=0;
break;
}
}
}
//源程序結(jié)束
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -