程序有点小问题,求大神修改下
//学生的成绩管理系统#include "iostream.h"
#include "malloc.h"
#include <fstream.h>
#include "conio.h"
#include "stdlib.h"
#include "string.h"
#define LEN sizeof(struct student)
struct student
{
int num;
int math;
int chinese;
int english;
char name[20];
float aver;
float sum;
struct student *next;
};
void type();
void insert(struct student *stu);
void look(struct student *stu);
void lookall();
void query_by_name();
void query_by_num();
void order_by_ave();
void del();
void save(); /*声明保存函数*/
struct student* open(); /*声明打开函数*/
int swap(struct student *p,struct student *q);
struct student *head=NULL;
int total=0;
void main()
{
system("color 70");
int choice;
int flag;
while(1)
{
cout<<"\n\t\t\t欢迎来到学生管理系统\n"<<endl;
cout<<"\n\t\t\t--------------------\n"<<endl;
cout<<"\t\t\t1:键入学生的信息\n"<<endl;
cout<<"\t\t\t2:查看录入学生的信息\n"<<endl;
cout<<"\t\t\t3:按姓名查看\n"<<endl;
cout<<"\t\t\t4:按学号查看\n"<<endl;
cout<<"\t\t\t5:按平均成绩排序\n"<<endl;
cout<<"\t\t\t6:删除学生的信息\n"<<endl;
cout<<"\t\t\t7:保存\n"<<endl;
cout<<"\t\t\t8:打开文件\n"<<endl;
cout<<"\t\t\t0:退出\n"<<endl;
cout<<"\t\t请输入你的选择:"<<endl;
cin>>choice;
switch(choice)
{
case 1:type();break;
case 2:lookall();break;
case 3:query_by_name();break;
case 4:query_by_num();break;
case 5:order_by_ave();break;
case 6:del();break;
case 7:save();break;
case 8:open();break;
case 0:flag=0;break;
default :cout<<"错误!"<<endl;
}
if(flag) break;
}
}
void type()
{
system("CLS");
struct student *p;
p=(struct student *)malloc(LEN);
cout<<"姓名:"<<endl;
cin>>p->name;
cout<<"学号:"<<endl;
cin>>p->num;
cout<<"数学成绩:"<<endl;
cin>>p->math;
cout<<"外语成绩:"<<endl;
cin>>p->english;
cout<<"语文成绩:"<<endl;
cin>>p->chinese;
p->sum=float(p->chinese+p->english+p->math);
p->aver=p->sum/3;
insert(p);
cout<<"按任意键返回主菜单"<<endl;
getch();
save();
}
void look(struct student *stu)
{
struct student *p;
p=stu;
cout<<"姓名:"<<p->name<<" "<<"学号:"<<p->num<<" "<<
"数学:"<<p->math<<" "<<"外语:"<<p->english<<" "
<<"语文:"<<p->chinese<<" "<<"总分:"<<p->sum<<" "
<<"平均分:"<<p->aver<<endl;
}
void lookall()
{ system("CLS");
struct student *p;
p=head;
if(p==NULL)cout<<"无记录"<<endl;
else
while(p)
{
look(p);
p=p->next;
}
cout<<"按任意键返回主菜单"<<endl;
getch();
}
void insert(struct student *stu)
{
struct student *p,*q1;
p=stu;
q1=head;
if(q1=NULL)
{
head=p;p->next=NULL;
}
else
{
q1=p;p->next=NULL;
p=q1;
}
total++;
}
void query_by_name()
{system("CLS");
struct student *p;
char na[20];
p=head;
cout<<"请输入查找的姓名:"<<endl;
cin>>na;
if(p==NULL)cout<<"无记录"<<endl;
else
while(p)
{ if(!strcmp(p->name,na))look(p);
else p=p->next;
}cout<<"按任意键返回主菜单"<<endl;
getch();
}
void query_by_num()
{system("CLS");
struct student *p;
int i;
p=head;
cout<<"请输入查找的学号:"<<endl;
cin>>i;
if(p==NULL)cout<<"无记录"<<endl;
else
while(p);
{
if(p->num==i)look(p);
else p=p->next;
}cout<<"按任意键返回主菜单"<<endl;
getch();
}
void order_by_ave()
{
system("CLS");
struct student *p,*q;
p=head;
if(p==NULL)cout<<"无记录"<<endl;
else
{
if(total=1)lookall();
if(p->next!=NULL)
{
q=p->next;
if(q->aver<p->aver)swap(p,q);
p=p->next;
}
else lookall();
}
getch();
int i;
cout<<"是否保存?(1:yes or 2:no)"<<endl;
cin>>i;
if(i==1) save();
else cout<<"退出"<<endl;
getch();
}
int swap(struct student *p,struct student *q)
{
struct student *m;
m=p;
p=q;
q=m;
return 0;
}
void del()
{system("CLS");
struct student *p,*q;
int i;
p=head;
cout<<"删除的学号是:"<<endl;
cin>>i;
if(p->next==NULL)cout<<"无记录"<<endl;
else
while((p->num!=i)&&(p->next!=NULL))
{q=p;
p=p->next;
}
if(p->num==i)
{
q->next=p->next;
cout<<"删除的学生是:"<<endl;
look(p);
total--;
}
else cout<<"没有找到该学生!"<<endl;
cout<<"按任意键返回主菜单"<<endl;
getch();
}
void save() /*建立保存文件函数*/
{ struct student *p;
p=head;
system("CLS");
ofstream out("data.txt",ios::out);
cout<<total<<endl;
while(p!=NULL)
{
out<<p->aver<<"\t"<<p->chinese<<"\t"<<p->english<<"\t"<<p->math<<
"\t"<<p->name<<"\n"<<p->num<<"\n"<<p->sum<<endl;
p=p->next;
}
cout<<"按任意键返回主菜单"<<endl;
getch();
}
struct student* open() /*定义打开文件函数*/
{
system("CLS");
int i=0;
struct student *p1,*p2,*st;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
ifstream in("data.txt",ios::out);
in>>i;
if(i==0){cout<<"data.txt文件的数据为空请先输入数据"<<endl;return 0;}
else
{
for(;i>0;i--)
{p1=(struct student *)malloc(LEN);
in>>st->aver>>st->chinese>>st->english>>
st->math>>st->name>>st->num>>st->sum;
strcpy(p1->name,st->name);
p1->aver=st->aver;
p1->chinese=st->chinese;
p1->english=st->english;
p1->math=st->math;
p1->num=st->num;
p1->sum=st->sum;
if(total==0)head=p1;
else p2->next=p1;
p2=p1;
total++;
look(p1);
}
cout<<"数据成功读取完毕"<<endl;
p2->next=NULL;
return (head);
}
}