#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*-----信息存储区-----*/
struct Caipin{
char cai[10];
int price;
}caipin[7]={ //7种菜品
{"steak",50},{"lamb",30},{"bacon",25},{"chicken",30},
{"vegetable",15},{"meatball",20},{"lobster",100}
};
struct Zhushi{
char zhu[10];
int price;
}zhushi[5]={ //5种主食
{"rice",5},{"cake",3},{"noodles",7},
{"jiaozi",20},{"bun",10}
};
struct Yinliao{
char yin[10];
int price;
}yinliao[5]={ //5种饮料
{"juice",3},{"coca-cola",5},{"tea",20},
{"coffee",15},{"milk",7}
};
struct VIP{ //用链表形式保存会员信息
char name[20];
char tel[20];
struct VIP *next;
};
/*-----函数声明区-----*/
float diancan();
float choice_caipin();
float choice_zhushi();
float choice_yinliao();
int jiesuan(struct VIP *infor,float sum);
struct VIP *check_huiyuan(struct VIP *infor,char *input);
int huiyuan(struct VIP **infor);
void getInput(struct VIP *infor);
void huiyuan_upload(struct VIP **xinxi);
void huiyuan_print(struct VIP *infor);
void huiyuan_delete(struct VIP **infor);
void saveVIP(struct VIP *xinxi);
struct VIP *readVIP();
/*-----主函数区-----*/
int main()
{
struct VIP *infor;
infor = readVIP();
// saveVIP(infor);
printf("欢迎使用XX餐厅前台管理系统\n");
printf("请选择您需要的服务项目:\n");
printf("1\t点餐\n2\t结算\n3\t会员卡管理\n");
int temp=0;
float sum = 0;
while(1){
scanf("%d",&temp);
switch(temp)
{
case 1:sum = diancan();break; //进入点餐界面
case 2:jiesuan(infor,sum);break; //进入结算系统
case 3:huiyuan(&infor);break; //进入会员管理系统
default:printf("输入错误!请重新输入");
}
}
return 0;
}
/*-----点餐函数区-----*/
float diancan()
{
printf("欢迎点餐!\n");
printf("请选择:\n1\t菜品\n2\t主食\n3\t饮料\n0\t返回主菜单\n");
int temp;
float sum = 0;
while(1){
scanf("%d",&temp);
switch(temp)
{
case 1:
sum += choice_caipin();
printf("请选择:\n1\t菜品\n2\t主食\n3\t饮料\n0\t返回主菜单\n");
break; //调用菜品选择函数,并计算总价格
case 2:
sum += choice_zhushi();
printf("请选择:\n1\t菜品\n2\t主食\n3\t饮料\n0\t返回主菜单\n");
break; //调用主食选择函数,并计算总价格
case 3:
sum += choice_yinliao();
printf("请选择:\n1\t菜品\n2\t主食\n3\t饮料\n0\t返回主菜单\n");
break; //调用饮料选择函数,并计算总价格
case 0:printf("1\t点餐\n2\t结算\n3\t会员卡管理\n");return sum; //返回主菜单
default:printf("输入错误!请重新输入");break;
}
}
}
float choice_caipin()
{
printf("请选择菜品:\n");
for(int i=0;i<7;i++){
printf("%d\t%s\t\t%d\n",i+1,caipin[i].cai,caipin[i].price);
}
printf("输入0返回上一级\n");
int choice;
float sum_caipin = 0;
while(1){
scanf("%d",&choice);
if(choice == 0){
break;
}
for (int j=0;j<7;j++){
if(choice == j+1){
printf("%s已选择\n",caipin[j].cai);
sum_caipin += caipin[j].price;
}
}
}
return sum_caipin;
}
float choice_zhushi()
{
printf("请选择主食:\n");
for(int i=0;i<5;i++){
printf("%d\t%s\t\t%d\n",i+1,zhushi[i].zhu,zhushi[i].price);
}
printf("输入0返回上一级\n");
int choice;
float sum_zhushi = 0;
while(1){
scanf("%d",&choice);
if(choice == 0){
break;
}
for (int j=0;j<7;j++){
if(choice == j+1){
printf("%s已选择\n",zhushi[j].zhu);
sum_zhushi += zhushi[j].price;
}
}
}
return sum_zhushi;
}
float choice_yinliao()
{
printf("请选择主食:\n");
for(int i=0;i<5;i++){
printf("%d\t%s\t\t%d\n",i+1,yinliao[i].yin,yinliao[i].price);
}
printf("输入0返回上一级\n");
int choice;
float sum_yinliao = 0;
while(1){
scanf("%d",&choice);
if(choice == 0){
break;
}
for (int j=0;j<7;j++){
if(choice == j+1){
printf("%s已选择\n",yinliao[j].yin);
sum_yinliao += yinliao[j].price;
}
}
}
return sum_yinliao;
}
/*-----结算函数区-----*/
int jiesuan(struct VIP *infor,float sum)
{
printf("您本次消费 %.2f 元",sum);
int temp = 1;
float rate = 0.7;
// struct VIP *infor = NULL;
char input[20];
while(temp == 1){
printf("您是否有会员卡?\n");
printf("1\t有\n0\t没有\n");
scanf("%d",&temp);
if(temp == 0){
break;
}else{
printf("请输入您的手机号码:\n");
scanf("%s",input);
if(check_huiyuan(infor,input) != NULL){
printf("会员卡享受7折优惠\n");
sum *= rate;
break;
}else{
printf("未查询到会员信息!请重新输入\n");
continue;
}
}
}
printf("您应付金额为:%.2f\n",sum);
printf("1\t点餐\n2\t结算\n3\t会员卡管理\n");
return 0;
}
struct VIP *check_huiyuan(struct VIP *infor,char *input) //检查会员信息是否存在
{
struct VIP *xinxi;
xinxi = infor;
while(xinxi != NULL){
if(strcmp(xinxi->tel,input) == 0){
return xinxi;
}
xinxi = xinxi->next;
}
return NULL;
}
/*-----会员管理函数区-----*/
int huiyuan(struct VIP **infor)
{
printf("欢迎进入会员管理系统\n");
printf("请选择您需要的服务项目:\n");
printf("1\t会员信息录入\n2\t会员信息显示\n3\t会员卡注销\n");
printf("输入0返回上一级\n");
int temp = 0,flag = 1;
// struct VIP *infor = NULL;
while(1){
scanf("%d",&temp);
switch(temp)
{
case 1:huiyuan_upload(infor);break; //会员信息录入
case 2:huiyuan_print(*infor) ;break; //会员信息打印
case 3:huiyuan_delete(infor) ;break; //会员信息注销
case 0:printf("1\t点餐\n2\t结算\n3\t会员卡管理\n");return 0;
default:printf("输入错误!请重新输入");continue;
}
}
}
void getInput(struct VIP *infor)
{
printf("请输入姓名:");
scanf("%s",infor->name);
printf("请输入手机号码:");
scanf("%s",infor->tel);
infor->next=NULL;
}
void huiyuan_upload(struct VIP **xinxi)
{
struct VIP *infor,*temp;
infor = (struct VIP *)malloc(sizeof(struct VIP));
if (infor == NULL){
printf("内存分配失败!");
exit(1);
}
getInput(infor);
if(*xinxi != NULL){
temp = *xinxi;
*xinxi = infor;
infor->next = temp;
}
else{
*xinxi = infor;
infor->next = NULL;
}
saveVIP(*xinxi);
}
void saveVIP(struct VIP *xinxi)
{
FILE *fp;
struct VIP *infor = xinxi;
fp = fopen("huiyuan.txt","wb+");
if(fp == NULL){
printf("can't open file!\n");
exit(1);
}
if(infor != NULL){
//fprintf(fp,"%s\t%s\n",infor->name,infor->tel);
rewind(fp);
fwrite(infor,sizeof(struct VIP),1,fp);
while (infor->next!=NULL)
{
fwrite(infor->next,sizeof(struct VIP),1,fp);
infor = infor->next;
}
}
fclose(fp);
}
struct VIP *readVIP() //运行前把文件内容读取到电脑内存
{
struct VIP *p,*q,*infor = (struct VIP *)malloc(sizeof(struct VIP));
p = q = infor;
FILE *fp;
if ( (fp=fopen("huiyuan.txt", "rb+"))==NULL)
{
if ( (fp=fopen("huiyuan.txt", "wb+"))==NULL)
{
printf("无法创建或打开"huiyuan.txt"文件\n");
exit(0);
}
return NULL;
}
else
{
if (fread(infor,sizeof(struct VIP),1,fp)!=0)
{
q= (struct VIP*)malloc(sizeof(struct VIP));
while(fread(q,sizeof(struct VIP),1,fp)!=0)
{
p->next = q;
p = q;
q= (struct VIP*)malloc(sizeof(struct VIP));
}
p->next =NULL ;
}
else
{
infor=NULL;
}
fclose(fp);
return infor;
}
}
void huiyuan_print(struct VIP *infor)
{
struct VIP *xinxi;
int num = 1;
xinxi = infor;
while(xinxi != NULL){
printf("Number %d:",num);
printf(" 姓名:%s",xinxi->name);
printf(" 手机号码:%s\n",xinxi->tel);
xinxi = xinxi->next;
num++;
}
}
void huiyuan_delete(struct VIP **infor)
{
struct VIP *xinxi,*temp;
char input[20];
printf("请输入需要删除的会员的手机号码:\n");
scanf("%s",input);
temp = *infor;
xinxi = temp;
while(xinxi != NULL){
if(strcmp(xinxi->tel,input)==0){
if (xinxi==*infor)
{
*infor=(*infor)->next;
free(xinxi);
break;
}
else
{
temp->next = xinxi->next;
free(xinxi);
break;
}
}
temp = xinxi;
xinxi = xinxi->next;
}
saveVIP(*infor);
}