小型订餐系统(已修改,但仍旧有问题。)
思想:链表。有insert函数 del函数 print 函数search函数 分别用来插入数据,删除数据,输出数据,和寻找数据。其中insert函数调用fill函数返回head值,采用的是按时间升序排列。在主函数中采用switch语句,实现循环
运行时,就是有问题,求大神花点时间来看一下。(每个功能都用一下。)
#include<iostream.h>
#include<string.h>
struct guest
{
int num;
char name[6];
int member;
char time[7];
guest *next;
};
void main()
{
void insert(guest *head);
void del(guest *head,int num);
guest *search(guest *head,int num);
void print(guest *head);
void empty(guest *head);
guest *head=NULL,*p;
int num,n;
cout<<"=================="<<endl;
cout<<'1'<<"==="<<"insert"<<endl;
cout<<'2'<<"==="<<"del"<<endl;
cout<<'3'<<"==="<<"search"<<endl;
cout<<'4'<<"==="<<"print"<<endl;
cout<<'5'<<"==="<<"exit"<<endl;
cout<<"=================="<<endl;
cout<<"what do you want to do:";
cin>>n;
while(n!=5)
{
switch(n)
{
case(1):insert(head);break;
case(2):cout<<"请输入要删除的号码:";cin>>num;del(head,num);break;
case(3):cout<<"请输入要寻找的号码:";
cin>>num;
p=search(head,num);
cout<<p->num<<endl;
cout<<p->name;
cout<<p->member<<endl;
cout<<p->time<<endl;
break;
case(4):print(head);break;
}
cout<<"what do you want to do:";
cin>>n;
}
}
guest *fill(guest *head,guest *p)
{
guest *p1,*p2;
if(head==NULL)
{
head=p;
p->next=NULL;
return (head);
}
p1=head;
while(strcmp(p->time,p1->time)>0)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p->time,p1->time)<=0)
{
p->next=p1;
if(p1==head) head=p;
else p2->next=p;
}
else
{
p1->next=p;
p->next=NULL;
}
return(head);
}
void insert(guest *head)
{
guest *fill(guest *head,guest *p);
guest *p1;
int num;
cout<<"input the num:";
cin>>num;
while(num!=0)
{
p1=new guest;
p1->num=num;
cout<<"name:";
cin>>p1->name;
cout<<"member:";
cin>>p1->member;
cout<<"time:";
cin>>p1->time;
head=fill(head,p1);
cout<<"input the num:";
cin>>num;
}
}
void del(guest *head,int num)
{
guest *p1,*p2;
if(head==NULL)
cout<<"还没有顾客订餐";
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(head==p1) head=p1->next;
else
p2->next=p1->next;
delete p1;
cout<<"已经删除";
}
else
cout<<"无此订号";
}
guest *search(guest *head,int num)
{
guest *p;
p=head;
while(p->num!=num&&p->next!=NULL)
p=p->next;
if(p->num==num)
return p;
else
return NULL;
}
void print(guest *head)
{
guest *p;
p=head;
while(p!=NULL)
{
cout<<p->num<<endl;
cout<<p->name<<endl;;
cout<<p->member<<endl;
cout<<p->time<<endl;
p=p->next;
}
}
void empty(guest *head)
{
guest *p;
while(head!=NULL)
{
p=head;
delete p;
head=head->next;
}
}