#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Insert 1 //增加订单
#define Query 2 //查找订单
#define Update 3 //修改订单
#define Delete 4 //删除订单
#define List 5 //列出所有订单
#define Quit 0 //退出系统
FILE *fp;
struct Node
{
char name[20];
char foodname[20];
int sum;
char time[20];
int number;
struct Node *next;
};
struct Node *pointer,*tail; //pointer指向第一订餐的学生,tail指向最近一次订餐的学生
int last=0; //当前订餐总人数
void showMenu();
void doChoice(int);
int getChoice();
void doQuit();
void doInsert();
void doUpdate();
void doQuery();
void doDelete();
void doListAll();
void init();
int main(int argc,char *argv[])
{
int choice; //用户选择功能
init(); //从文件中初始化订餐信息
while(1)
{
showMenu(); //显示主菜单
choice=getChoice(); //获取用户选择
doChoice(choice);
system("pause>nul") ; //执行用户选择
}
return 0;
}
void init()
{
int i=0;
struct Node *p;
pointer=tail=NULL;
if((fp=fopen("order.dat","rb"))==NULL)
{
last=0;//当前没有订餐数据
}
else
{
while(!feof(fp))
{
if((p=(struct Node *)malloc(sizeof(struct Node)))==NULL)
{
printf("系统内存故障,退出程序");
exit(0);
}
if(fread(p,sizeof(struct Node),1,fp)==1)
{
if(i==0)
{
pointer=tail=p; //第一个订餐学生
}
i++;
tail->next=p;
tail=p;
}
}
tail->next=NULL;
last=i;
}
fclose(fp);
}
void save() //保存订单信息到文件pointer.txt中
{
struct Node *p=pointer;
if((fp=fopen("order.txt","wb"))==NULL)
{
printf("不能打开订单信息,请检查");
exit(0);
}
while(p!=NULL)
{
if(fwrite(p,sizeof(struct Node),1,fp)==1)
{
p=p->next;
}
}
fclose(fp);
}
void showMenu()
{
system("cls");
printf("**********Student Food-Order System*********\n");
printf("\t\t1. 点外卖\n");
printf("\t\t2. 查找订单\n");
printf("\t\t3. 修改订单\n");
printf("\t\t4. 删除订单\n");
printf("\t\t5. 查看所有订单\n");
printf("\t\t6. 退出系统\n");
printf("************************\n");
printf("请输入你的选择(0---6):\n");
}
void doQuit()
{
printf("\n欢迎使用本系统,再见!");
exit(0);
}
void doInsert()
{
struct Node *p;
if((p=(struct Node *)malloc(sizeof(struct Node)))==NULL)
{
printf("该店点餐人员爆满,请稍后进入");
return ;
}
printf("请输入编号:");
scanf("%d",&(p->number));
printf("请输入姓名:");
scanf("%s",p->name);
printf("请输入菜名:");
scanf("%s",p->foodname);
printf("请输入人数:");
scanf("%d",&(p->sum));
printf("请输入用餐时间:");
scanf("%s",p->time);
printf("点餐成功\n");
if(tail!=NULL)
{
tail->next=p;
tail=tail->next;
}
else
{
{
for(p=pointer;p->next!=NULL;p=p->next)
p->next=p;
}
tail->next=NULL;
last++;
save();
}
}
void doUpdate()
{
int number;
int i;
int find=0;
struct Node *p=pointer;
printf("请输入要修改的订单号码:");
scanf("%d",&number);
while((p!=NULL)&&(!find))
{ if(p->number==number)
{
find=1;
printf("你要找的订单信息如下:\n");
printf("*******************************");
printf("姓名:%s\t",p->name);
printf("菜名:%s\t",p->foodname);
printf("人数:%d\t",p->sum);
printf("用餐时间%s\t",p->time);
printf("*******************************");
printf("请输入修改后的信息:\n");
printf("姓名:\n");
scanf("%s",p->name);
printf("菜名:\n");
scanf("%s",p->foodname);
printf("人数:\n");
scanf("%d",&(p->sum));
printf("请输入用餐时间:\n");
scanf("%s",p->time);
}
p=p->next;
}
if(!find)
{
printf("无法找到该订单,请重新下单\n");
}
else
{
printf("该订单修改成功\n");
save();
}
p=NULL;
}
void doListAll()
{
struct Node *p=pointer;
printf("该店目前有%d人在线\n",last);
while(p!=NULL)
{
printf("*******************************\n");
printf("姓名:%s\t",p->name);
printf("菜名:%s\t",p->foodname);
printf("人数:%d\t",p->sum);
printf("用餐时间%s\n",p->time);
p=p->next;
}
printf("***************************\n");
}
void doQuery()
{
int number;
int i;
int find=0;
struct Node *p=pointer;
printf("处理查询.....\n");
printf("请输入订单号码:");
scanf("%d",&number);
while((p!=NULL)&&(!find))
{ if(p->number==number)
{
find=1;
printf("你要找的订单信息如下:\n");
printf("*******************************\n");
printf("姓名:%s\t",p->name);
printf("菜名:%s\t",p->foodname);
printf("人数:%d\t",p->sum);
printf("用餐时间%s\t",p->time);
}
p=p->next;
}
if(!find)
{
printf("没有此订单\n");
}
}
void doDelete()
{
int number;
int i;
int find=0;
int index;
struct Node *p=pointer;
struct Node *pre=pointer;
printf("处理删除.....\n");
printf("请输入订单编号:\n");
scanf("%d",&number);
while((p!=NULL)&&(!find))
{
if(p->number==number)
{
find=1;
index=i;
break;
}
pre=p;
p=p->next;
}
if(!find)
{
printf("没有该编号订单\n");
}
else
{
if(p==pre)
{
pointer=NULL;
last=0;
}
else
{
pre->next=p->next;
last--;
printf("订单已删除\n");
save();
}
}
}
int getChoice()
{
int choice;
scanf("%d",&choice);
return choice;
}
void doChoice(int choice)
{
switch(choice)
{
case Insert:
doInsert();
break;
case Query:
doQuery();
break;
case Update:
doUpdate();
break;
case Delete:
doDelete();
break;
case List:
doListAll();
break;
case Quit:
doQuit();
}
}