#include<iostream>
#include<cstring>
#include<cassert>
#include<cstdio>
using namespace std;
struct student
{
char name[20];
char num[20];
int math;
int english;
int py;
int sum;
int average;
};
struct node
{
struct student data;
struct node*next;
};
struct node*head;
struct node* createhead()
{
struct node*headnode=(struct node*)malloc(sizeof(struct node));
assert(headnode);//判断是否申请内存成功
headnode->next=NULL;
return headnode;
}
struct node* createnode(struct student data)
{
struct node*newnode=(struct node*)malloc(sizeof(struct node));
assert(newnode);//判断是否申请内存成功
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void insertnode(struct node*headnode,struct student data)//headnote只需要,createnode函数只用调用一次 ,后面只需传入头节点就行
{
struct node*newnode=createnode(data);
newnode->next=headnode->next;
headnode->next=newnode;
}
void printlist(struct node*headnode)
{
struct node*passnode=headnode->next;
cout<<"姓名"<<"\t" <<"编号"<<"\t" <<"数学"<<"\t" <<"英语"<<"\t"<<"py"<<"\t"<<"总分"<<"\t"<<"平均分"<<"\t"<<endl;
while(passnode!=NULL)
{
cout<<passnode->data.name<<"\t"<<passnode->data.num<<"\t"<<passnode->data.math<<"\t"<<passnode->data.english<<"\t"<<passnode->data.py<<"\t"<<passnode->data.sum<<"\t"<<passnode->data.average<<endl;
passnode=passnode->next;
}
}
void makemenu()
{
cout<<"---------链式学生管理系统----------"<<endl;
cout<<"1.录入功能"<<endl;
cout<<"2.浏览功能"<<endl;
cout<<"3.查找功能"<<endl;
cout<<"4.修改功能"<<endl;
cout<<"5.删除功能"<<endl;
cout<<"6.退出系统"<<endl;
cout<<"-----------------------------------"<<endl;
}
void deletedata(struct node*headnode,const char*name)
{
struct node*prenode=headnode;
struct node*curnode=headnode->next;
while(curnode!=NULL&&strcmp(curnode->data.name,name))
{
prenode=curnode;
curnode=prenode->next;
}
if(curnode=NULL)
{
cout<<"删除失败,没有找到指定数据"<<endl;
}
else
{
prenode->next=curnode->next;
free(curnode);
}
}
struct node*searchdata(struct node*headnode,const char*name)
{
struct node*pmove=headnode->next;
while(pmove!=NULL&&strcmp(pmove->data.name,name))
{
pmove=pmove->next;
}
return pmove;
}
void keydown()
{int m;
cin>>m;
struct student temp;
struct node*result;
switch(m)
{
case 1:cout<<"----【录入模块】----"<<endl;
cout<<"请输入学生信息:"<<endl;
cout<<"姓名"<<"\t" <<"编号"<<"\t" <<"数学"<<"\t" <<"英语"<<"\t"<<"py"<<endl;
cin>>temp.name>>temp.num>>temp.math>>temp.english>>temp.py;
temp.sum=temp.math+temp.english+temp.py;
temp.average=temp.sum/3;
insertnode(head,temp);
break;
case 2:cout<<"----【浏览模块】----"<<endl;
printlist(head);
break;
case 3:cout<<"----【查找功能】----"<<endl;
cout<<"请输入要查找的名字:";
cin>>temp.name;
searchdata(head,temp.name);
result=searchdata(head,temp.name);
if(result==NULL)
{
cout<<"未找到结果";
}
else
{
cout<<"姓名"<<"\t" <<"编号"<<"\t" <<"数学"<<"\t" <<"英语"<<"\t"<<"py"<<"\t"<<"总分"<<"\t"<<"平均分"<<endl;
cout<<result->data.name<<"\t"
<<result->data.num<<"\t"
<<result->data.math<<"\t"
<<result->data.english<<"\t"
<<result->data.py<<"\t"
<<result->data.sum<<"\t"
<<result->data.average<<"\t";
}
break;
case 4:cout<<"----【修改功能】----"<<endl;
cout<<"请输入要修改的名字:";
cin>>temp.name;
result=searchdata(head,temp.name);
if(result==NULL)
{
cout<<"未找到结果";
}
else
{cout<<"输入新同学信息:"<<endl ;
cout<<"姓名"<<"\t" <<"编号"<<"\t" <<"数学"<<"\t" <<"英语"<<"\t"<<"py"<<endl;
cin>>result->data.name>> result->data.num>> result->data.math>> result->data.english>>result->data.py;
result->data.sum=result->data.math+result->data.english+result->data.py;
result->data.average=result->data.sum/3;
}
break;
case 5:cout<<"----【删除功能】----"<<endl;
cout<<"请输入要删除的学生姓名:";
cin>>temp.name ;
deletedata(head,temp.name);
break;
case 6:cout<<"----【退出系统】----"<<endl;
exit(0);
break;
default:
cout<<"输入错误,请重新输入!"<<endl;
break;
}
}
int main()
{ head=createhead();
while(1)
{
makemenu();
keydown();
system("pause");
system("cls");
}
return 0;
}
这个程序如果输入的不是数字而是字母,按任意键就会一直循环执行default,system("pause")和system("cls")的功能,且无法输入任何数据,这个要怎么修改。