1、include 〈iostream。h〉
#include 〈iomanip。h〉
#include
2、定义一个学生信息的结构体
{
char name[20]; //用来存放姓名的
char sex[20]; //用来存放性别的
char yuanxi[20];//用来存放院系的
long int id; //用来存放学号的
int score[4]; //用来存放分数的
int total; //用来存放总分数的
struct student *next;
};
vector
3、formation() ; //析构函数。 student *creat();//建立链表函数。 void output(student *head); int count(student *head);//定义函数count()统计考生总数 student *insert(student*head);//指针函数*insert()用来添加考生信息。 student *cancel(student *head,long int num);//指针函数*cancel()用来删除考生信息. student *find(student *head,long int
4、num); //指针函数*find()用来查找考生信息。 void inorder(student *head);//定义inorder()函数将考生的总分从大到小排列并输出 void average( student *head);//求学生成绩的平均分的函数 void save(student *head);//保存函数 student *Read();//读取函数 private: student *p1,*p2,*p3,*head,st; }; Information::Information() //构造函数 { cout〈〈” *
5、*****************************************************************************\n"; cout<〈” ————-—-———---——————-————<〈欢迎您使用学生信息管理系统〉〉—-———-——-—————-————-—-—-\n”; cout〈〈" ******************************************************************************\n\n”; } Information::~Information() //构
6、造函数 { cout<〈” ******************************************************************************\n”; cout<〈" —----—————---—-—----————<〈谢谢您使用学生信息管理系统〉>-——-—————-————-——-----—-\n”; cout<〈” ******************************************************************************\n”; } student *Inform
7、ation::creat(void) //定义一个指向struct student的结构体指针函数*creat()用来增加学生信息. { char ch[20];n=0; //用来存放姓名的 p1=p2=(student *)malloc(LEN);//调用malloc()函数用来开辟一个新的存储单元 cout〈〈” —————-—--—-—-<〈请建立学生考试信息表,在姓名处输入 ! 结束输入.〉〉—————-—---————"<〈endl; cout〈<”姓名:"; cin>〉ch; head=NULL; //给指针head赋初值 while (
8、strcmp(ch,”!")!=0) //调用字符比较函数strcmp()用来判断是否继续输入 { char str[10]; int flag=0; p1=(student *)malloc(LEN);//调用malloc()函数用来开辟一个新的存储单元 strcpy(p1—>name,ch); //将循环结构前面输入的姓名复制到结构体名为p1的数组name中 cout<<” 性别:"; cin〉〉p1-〉sex; cout<〈"院系:" ; cin〉〉str; cout<〈” 学号(9位):”; do{ cin〉〉str;
9、 if(atol(str)〉999999999 || atol(str)〈1) cout〈〈"对不起,请正确输入!\n”; else { p1->id=atol(str); flag=1; } }while(flag==0); flag=0; cout〈<” 语文成绩:”; do{ cin〉>str; if(atoi(str)〉100 || atoi(str)〈1) cout〈〈”对不起,请输入1—100之间的数字!!\n"; else { p1->score[0]=atoi(str); flag=1;
10、 } }while(flag==0); flag=0; cout<〈” 外语成绩:”; do{ cin>〉str; if(atoi(str)〉100 || atoi(str)〈1) cout〈〈”对不起,请输入1—100之间的数字!!\n"; else { p1-〉score[1]=atoi(str); flag=1;} }while(flag==0); flag=0; cout〈〈” 数学成绩:”; do{ cin〉〉str; if(atoi(str)>100 || atoi(str)〈1) cou
11、t<〈”对不起,请输入1—100之间的数字!!\n”; else { p1—>score[2]=atoi(str); flag=1;} }while(flag==0); flag=0; cout〈〈” C++成绩:”; do{ cin>〉str; if(atoi(str)〉100 || atoi(str)〈1) cout<<”对不起,请输入1—100之间的数字!!\n"; else { p1—〉score[3]=atoi(str); flag=1;} }while(flag==0); flag=0; p1—>t
12、otal=p1—〉score[0]+p1—〉score[1]+p1—>score[2]+p1—〉score[3];//计算总分 if(n==0)head=p1;//如果是输入第一组学生考试信息就将指针p1赋给指针head else p2—>next=p1;//否则将p1赋给p2所指结构体的next指针 p2=p1;//将指针p1赋给指针p2 n++; //将n的值加1 cout〈<” 姓名:”; cin>〉ch;//将输入的姓名存放到字符数组ch中 } p2-〉next=NULL;//将p2所指结构体的next指针重新赋空值 return (hea
13、d);//将输入的第一组学生考试信息返回 } void Information::output(student *head) //定义output()函数将学生的信息从头指针所指内容开始输出 { if(head==NULL) cout〈<” 这是一个空表,请先输入考生成绩。\n”; else{ cout〈<"--—-------———-————-——-——--—---———-——————-—--———————-—-—-————-—---—-——---———---—\n”; cout〈〈" *学生成绩信息表*\n”; cout〈〈"-——-—--—-—-—
14、—--————--——-—----—-—--—-——-—-—————-———-—-———-—-———————--—-—————-——\n”;
cout〈〈”学号 姓 名 性别 院系 语文 英语 数学 C++ 平均成绩 总分\n";
cout〈〈”-—-———--—————-----————-——-—--—--———---———————--—--————-————-——————-————————-———\n”;
p1=head;//将头指针赋给p
do
{
cout〈 15、etw(9)〈〈p1-〉name
<〈setw(8)〈〈p1—>sex
< 16、n”;
p1=p1->next;//将下一组学生信息的next指针赋给p
}while(p1!=NULL);//若指针p非空则继续,目的是把所有的学生信息都传给指针p然后输出.
}
}
//统计学生人数的函数
int Information::count(struct student *head)//定义函数count()统计考生总数
{
if(head==NULL) //若指针head为空返回值为0
return(0);
else return(1+count(head—>next));//函数的递归调用
}
//插入学生的成绩信息的函数 17、
student *Information::insert( student *head) //插入新结点定义一个指向struct student的结构体指针函数*insert()用来添加考生信息。
{
char str[10];
int flag=0;
cout<〈"\t--—-—--——-—————-<〈请输入新增学生成绩信息〉〉-——-———-—-—--—-—\n"〈〈endl;
p1=(student *)malloc(LEN); //使p1指向插入的新结点
cout〈<” 姓名:”;
cin〉〉p1—〉name; //将输入的姓名存放到结构体 18、名为p1的数组name中
cout<〈" 性别:”;
cin>>p1—>sex;
cout〈〈” 学号(9位):”;
do{
cin>〉str;
if(atol(str)〉99999999 || atol(str)<1)
cout<〈”对不起,请请正确输入!!!\n”;
else
{p1—>id=atol(str); flag=1; }
}while(flag==0);
flag=0;
cout〈<" 语文成绩:”;
do{
cin〉>str;
if(atoi(str)〉100 || atoi(str)〈 19、1)
cout〈<"对不起,请输入1-100之间的数字!!\n”;
else
{ p1-〉score[0]=atoi(str); flag=1;}
}while(flag==0);
flag=0;
cout<〈" 外语成绩:”;
do{
cin〉〉str;
if(atoi(str)〉100 || atoi(str)〈1)
cout〈<”对不起,请输入1—100之间的数字!!\n”;
else
{ p1-〉score[1]=atoi(str); flag=1;}
}while(flag==0);
flag=0; 20、
cout〈〈” 数学成绩:”;
do{
cin>〉str;
if(atoi(str)>100 || atoi(str)〈1)
cout<<”对不起,请输入1—100之间的数字!!\n";
else
{ p1-〉score[2]=atoi(str); flag=1;}
}while(flag==0);
flag=0;
cout<〈" C++成绩:";
do{
cin>>str;
if(atoi(str)〉100 || atoi(str)〈1)
cout<〈”对不起,请输入1-100之间的数字!!\n”;
e 21、lse
{ p1-〉score[3]=atoi(str); flag=1;}
}while(flag==0);
flag=0;
p1-〉total=p1—>score[0]+p1-〉score[1]+p1—〉score[2]+p1->score[3];//计算总分
p2=head;//将头指针赋给p2
if(head==NULL) //若没调用次函数以前的头指针head为空
{
head=p1;p1—>next=NULL;//则将p1赋给头指针head并将p1所指结构体成员指针next赋空值
}
else
{
while((p1 22、〉id〉p2—〉id)&&(p2—〉next!=NULL))
{
p3=p2;//p3指向原p2指向的结点
p2=p2—〉next;
}//p2后移一个结点
if(p1—〉id〈=p2—〉id)
{
if(head==p2)
{
p1—>next=head;
head=p1;
} //插入到第一个结点之前
else
{
p3—〉next=p1;
p1—>next=p2;
} //插入到p3所指结点之后
}
else
{
p2—〉next=p1;
p1-〉next=NUL 23、L;
} //插入到尾结点之后
}
n++;//将学生人数加1
cout〈〈"\t你输入的学生信息已经成功插入"< 24、head;//否则将头指针赋给p1
while(num!=p1-〉id&&p1-〉next!=NULL)//寻找要删除的结点当p1所指的学号不是输入的学号并且p1所指的next指针不为空
{
p2=p1;
p1=p1—〉next; //p2指向原p1指向的结点p1后移一个结点
}
if(num==p1->id)//如果输入的学生准考证号是p1所指的学号结点找到后删除
{
if(p1==head) head=p1-〉next;//如果head指针和p1指针相等则将下一个结点赋给指针head
else
p2—〉next=p1—>next;/ 25、/否则将p1所指结点赋给p2所指结点将要删除的学生信息跳过去
cout<〈” 删除学号为”〈 26、先输入考生成绩。\n”;
return(head);
}
else
{
p1=head;//否则将头指针赋给p1
while(num!=p1—〉id&&p1—>next!=NULL)
//寻找结点当p1所指的学号不是输入的学生学号并且p1所指的next指针不为空
{
p1=p1—>next; //p1后移一个结点
}
if(num==p1—>id)//如果要查找的学号是p1所指的学号
{
cout〈<”—-———-—-—---——----——--—-——--—-——-—--———-—-----———————————--—-—- 27、————-—--——--—\n”;
cout<<"学号 姓名 性别 院系 语文 英语 数学 C++ 平均分 总分 \n";
cout<〈"———————-—-———-————--——-—-——-——-—-—-----—-——--————-————-—————---—----—----—-———\n”;
cout〈 28、10)〈 29、inorder(student *head) //定义inorder()函数将考生的总分从大到小排列并输出
{
int i,k,m=0,j;
student *p[20];//定义一个指向struct student的结构体指针数组p
if(head!=NULL)//如果头指针是空则继续
{ m=count(head);
cout〈〈"——-———————-———-———-—-——————-——-—-—-——--—-—--———-———-—-—---————-————-—-————--——\n”;
cout<<”学生成绩统计表\n";
cout〈<”—- 30、———————————--——————————-———————-——-—---—-————-—————--————-—--—-—---—-——-—-—-\n”;
cout〈<”学号 姓 名 性别 院系 语文 英语 数学 C++ 平均分 总分 名次\n”;
cout<<”——-—-——-———-———-———-——--—-—-—-—-—---——-—-——-—-—————-——---—-——--—--———-——---—-—\n”;
p1=head;
for(k=0;k 31、}
for(k=0;k〈m-1;k++) //选择排序法
for(j=k+1;j〈m;j++)
if(p[k]->total name
〈 32、1—>score[2]
<〈setw(9)〈 33、0,arg3=0,arg4=0;
if(head==NULL)//如果头指针是空则继续
{
cout<〈” 这是一个空表,请先输入学生成绩.\n";
}
else
{
m=count(head);
p1=head;
for(k=0;k 34、
cout<〈"全班单科成绩平均分\n";
cout〈<”-----—--———--—-—-—-—-—--—--———--——-——————--—————————----——--————--————--——---—\n”;
cout<<” 语文平均分:”〈〈setw(7)〈〈arg1
〈〈” 英语平均分:”〈〈setw(7)〈〈arg2
<〈" 数学平均分:"<〈setw(7)< 35、———-—-—-—--———-—-—--—-—————————--————\n";
}
}
void Information::save(student *head) //保存函数。
{
ofstream out("data。txt”,ios::out);
out〈 36、core[1]〈〈"\t”
〈〈head->score[2]〈〈”\t"
〈〈head—〉score[3]<〈”\t”
〈 37、空,请先输入数据。”〈〈endl; return 0;}
else {
cout<〈” …………………………………………………………………………………………"〈〈endl;
for(;i>0;i—-)
{ p1=(student *)malloc(LEN);
cin>>st。name〉>st。id>〉st。sex
〉〉st。score[0]〉〉st.score[1]>>st。score[2]>>st。score[3]
〉>st。total;
strcpy(p1—〉name,st.name);
p1—>id=st.id;
strcpy(p1 38、—〉sex,st。sex);
p1—〉score[0]=st。score[0];
p1—〉score[1]=st。score[1];
p1—〉score[2]=st.score[2];
p1—〉score[3]=st.score[3];
p1->total=st.total;
if(n==0)head=p1;//如果是输入第一组学生考试信息就将指针p1赋给指针head
else p2—〉next=p1;//否则将p1赋给p2所指结构体的next指针
p2=p1;//将指针p1赋给指针p2
n++; //将n的值加1
cout〈〈" " 39、〈〈p1-〉name<〈"\t" //显示读入数据
〈〈p1—〉id<〈"\t”〈<”\t"
<〈p1->sex<〈”\t”
〈〈p1—〉score[0]<<”\t"
〈 40、 return (head);
}
}
int main() //主函数.
{
system("color 4f”);//改变登陆界面颜色
Information person;
student *head=NULL;
char str[5];
int flag=0;
int choice;
long int i;
char admin[20];
char pass[30];
cout<〈”请输入账号\n”;
cin〉>admin;
cout<<”请输入密码\n”;
cin〉〉pass;
if(strcmp( 41、admin,”1111”)==0&&strcmp(pass,"222”)==0)
{
do{
cout<<” ******************************************************************************”< 42、 ”< 43、
cout〈〈”┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"〈〈endl;
cout<〈”请输入您的选择(1-—7):( )\b\b";
cin>>str;
if(atoi(str)>7 || atoi(str)〈1)
cout〈<”对不起,请输入1—7这几个数字!!\n”;
else
{
choice=atoi(str);
switch(choice)
{
case 1:
head=person.creat();
break;
case 2:
person。outpu 44、t(head);
break;
case 3:
person。inorder(head);
person.average(head);
cout〈〈" 学生人数为:”〈 45、}
}while(flag==0);
flag=0;
person.find(head,i);
break;
case 5:
head=person.insert(head);
person。output(head);
break;
case 6:
cout〈〈" 请输入要删除的学号(9位):”;
do{
cin>〉str;
if(atol(str)>999999999 || atol(str)〈1)
cout〈〈”对不起,请输入正确输入!!!h\n”;
else
{i=atol(str); flag 46、1; }
}while(flag==0);
flag=0;
head=person.cancel(head,i);
person.output(head);
break;
case 7:
person。save(head);
cout<<”文件已保存!可以安全退出!!!"〈






