#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#define FILENAME "test.txt"/*文件名*/
struct Cmenu
{
int num;/*菜品编号*/
char fname[10]; /*菜系名称*/
char name[10];/*菜名*/
char introduce[1000];/*菜的介绍*/
int binprice;/*菜的原始价格*/
float zhekou;/*若有折扣卡的折扣*/
};
typedef struct Link
{
struct Cmenu data;
struct Link *next;
};
struct Link *head;
struct Link *enter1()/*初始化型录入菜的品种*/
{
int n=1,a=1;
struct Link *p,*q;
struct Link *head=NULL;
FILE *fp;
fp=fopen("D:\\text.txt","w+");
q=NULL;
if(fp==NULL)
{
printf("error,can't open the file");
exit(1);
}
p=NULL;
q=NULL;
p=(struct Link *)malloc(sizeof(struct Cmenu));
if(p==NULL)
{
printf("NO MEMORY");
exit(0);
}
printf("输入菜品编号\n");
scanf("%d",&(p->data.num));
getchar();
printf("输入菜名\n");
gets(p->data.name);
printf("输入菜系\n");
gets(p->data.fname);
printf("输入该菜介绍\n");
gets(p->data.introduce);
printf("输入菜的初始价格\n");
scanf("%d",&p->data.binprice);
getchar();
printf("若客人有折扣单,输入该菜的折扣\n");
scanf("%f",&p->data.zhekou);
getchar();
while(a==1)
{
/*if(n==1)
{
head=p;
q=p;
}
else*/
q=p;
p=(struct Link *)malloc(sizeof(struct Cmenu));
q->next=p;
if(p==NULL)
{
printf("NO MEMORY");
exit(0);
}
system("cls");
printf("输入菜品编号\n");
scanf("%d",&p->data.num);
getchar();
printf("输入菜名\n");
gets(p->data.name);
printf("输入菜系\n");
gets(p->data.fname);
printf("输入该菜介绍\n");
gets(p->data.introduce);
printf("输入菜的初始价格\n");
scanf("%d",&p->data.binprice);
getchar();
printf("若客人有折扣单,输入该菜的折扣\n");
scanf("%f",&p->data.zhekou);
getchar();
//p=(struct Link *)malloc(sizeof(struct Cmenu));
// p=q->next;
printf("按0结束输入,按1继续输入\n");
printf("是否继续输入\n");
scanf("%d",&a);
if(a==0)
{
p->next=NULL;
}
n++;
}
fwrite(&p,sizeof(struct Cmenu),1,fp);
//free(p);
fclose(fp);
return head;
}
struct Link * enter2()/*补充型录入菜的品种*/
{
FILE* fp;
float a1;
char ch;
struct Link *p,*q;
struct Link *head=NULL;
fp=fopen("D:\\text.txt","aw+");
if(fp==NULL)
{
printf("error,can't open the file");
exit(1);
}
p=head;
fread(p,sizeof(struct Cmenu),1,fp);
while(p!=NULL)
{
p++;
}
printf("请问想添加菜名吗?Y/y:是
N/n:否");
scanf("%s",&ch);
getchar();
system("cls");
while((ch=='Y')||(ch=='y'))
{
p=(struct Link*)malloc(sizeof(struct Cmenu));
printf("输入菜品编号\n");
scanf("%d",&p->data.num);
getchar();
printf("输入菜名\n");
gets(p->data.name);
printf("输入菜系\n");
gets(p->data.fname);
printf("输入该菜介绍\n");
gets(p->data.introduce);
printf("输入菜的初始价格\n");
scanf("%d",&(p->data.binprice));
printf("若客人有折扣单,输入该菜的折扣\n");
scanf("%f",&a1);
getchar();
q=p;
p=(struct Link *)malloc(sizeof(struct Cmenu));
q->next=p;
printf("请问想添加菜名吗?Y/y:是
N/n:否");
scanf("%s",&ch);
system("cls");
}
p->next=NULL;
fwrite(p,sizeof(struct Cmenu),1,fp);
fclose(fp);
return head;
}
void menu1()/*总菜单*/
{
printf("
欢迎使用\n");
printf("------------------------点菜系统-----------------------\n");
printf("
请选择:\n");
printf("
1录入(餐厅老板使用)\t\t2使用(客户使用)\n");
printf("-------------------------------------------------------\n");
}
void menu2()/*菜品录入的菜单*/
{
printf("
欢迎使用\n");
printf("------------------------点菜系统------------------------\n");
printf("请选择:\n");
printf("
1始化录入\t\t\t\t2补充录入\n");
printf("
3修改菜品\t\t\t\t4删除菜品\n");
printf("
5返回上一页\n");
printf("--------------------------------------------------------\n");
}
void menu3()/*顾客点菜菜单1*/
{
printf("------------------------点菜系统------------------------\n");
printf("请选择:\n");
printf("
1点菜\t\t\t\t\t2结账\n");
printf("--------------------------------------------------------\n");
}
void menu4()/*顾客点菜菜单2*/
{
printf("------------------------点菜系统------------------------\n");
printf("请选择:\n");
printf("
1菜谱一览表\t\t\t\t\t2菜名查找\n");
printf("--------------------------------------------------------\n");
}
void xiugai(struct Link *head)/*餐馆修改菜单中的菜*/
{
FILE* fp;
//int n ;
// n=sizeof(struct Cmenu);
char buf[sizeof(struct Cmenu)];
// char *buf;
int flag=1;
int a;
struct Link *p,*q;
//struct Link *head=NULL;
fp=fopen("D:\\text.txt","aw+");
//p=head;
if(fp==NULL)
{
printf("error,can't open the file");
exit(1);
}
p=head;
fseek(fp,0,SEEK_SET);
fread(buf,sizeof(struct Cmenu),1,fp);
p=buf;
q=p;
while(p!=NULL)
{
printf("编号\t菜系\t菜名\t菜品介绍\t价格\t折扣后价格\n");
printf("%2d\t\t%5s\t\t%5s\t\t%5s\t\t%2f\t\t%2f",p->data.num,p->data.fname,p->data.name,p->data.introduce,p->data.binprice,p->data.zhekou);
p=p->next;
}
printf("请输入需要修改的菜品编号:");
scanf("%d",&a);
while(flag==1)
{
// p=head;
if(a==p->data.num)
{
printf("请输入修改方案:\n");
printf("输入菜品编号\n");
scanf("%d",&p->data.num);
getchar();
printf("输入菜名\n");
scanf("%s",p->data.name);
getchar();
printf("输入菜系\n");
scanf("%s",p->data.fname);
getchar();
printf("输入该菜介绍\n");
scanf("%s",p->data.introduce);
getchar();
printf("输入菜的初始价格\n");
scanf("%f",&p->data.binprice);
getchar();
printf("若客人有折扣单,输入该菜的折扣\n");
scanf("%f",&p->data.zhekou);
getchar();
flag=0;
}
else
{
p=p->next;
}
}
fwrite(p,sizeof(struct Cmenu),1,fp);
fclose(fp);
free(p);
}
void shanchu()
/*删除菜单中的菜*/
{
FILE* fp;
int flag=1;
int a;
struct Link *p,*q;
struct Link *head;
fp=fopen("D:\\text.txt","aw+");
if(fp==NULL)
{
printf("error,can't open the file");
exit(1);
}
p=head;
fread(p,sizeof(struct Cmenu),1,fp);
while(p!=NULL)
{
printf("编号\t\t菜系\t\t菜名\t\t菜品介绍\t\t价格\t\t折扣后价格\n");
printf("%2d\t\t%5s\t\t%5s\t\t%5s\t\t%2d\t\t%2f",p->data.num,p->data.fname,p->data.name,p->data.introduce,p->data.binprice,p->data.zhekou);
p=p->next;
}
printf("请输入需要删除的菜品编号:");
scanf("%d",&a);
q=p;
while(a!=p->data.num&&p!=NULL)
{
q=p;
p=p->next;
}
if(a==p->data.num)
{
if(p==head)
{
head=p->next;
}
else
{
q->next=p->next;
}
free(p);
}
}
void main()
{
int a,b,c;
menu1();
scanf("%d",&a);
system("cls");
b=1;
switch (a)
{
case 1:
menu2();
scanf("%d",&c);
switch (c)
{
case 1:
system("cls");
enter1();
break;
case 2:
system("cls");
enter2();
break;
case 3:
system("cls");
printf("%d",head);
xiugai(head);
break;
case 4:
system("cls");
shanchu();
break;
case 5:exit(0);
break;
default:
system("cls");
printf("error");
menu2();
}
case 2:
system("cls");
use(head);
break;
default:
system("cls");
printf("输入错误,请重新选择:");
//menu1();
}
printf("是否退出?
1:继续
0:退出 ");
scanf("%d",&b);
if(b)
exit(0);