64,654
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
/*
菜单食物的类
*/
typedef struct _food
{
char name[128];//菜名
char kind[128];//分类
int price;//价格
}Food;
void setName(Food* f, const char* n)
{
assert(f && n);
strcpy(f->name, n);
}
const char* getName(Food* f)
{
assert(f);
return f->name;
}
void setKind(Food* f, const char* k)
{
assert(f && k);
strcpy(f->kind, k);
}
const char* getKind(Food* f)
{
assert(f);
return f->kind;
}
int getPrice(Food* f)
{
assert(f);
return f->price;
}
void setPrice(Food* f, int p)
{
f->price = p;
}
const char* numberToString (int x, char* buf)
{
return _itoa(x, buf, 10);
}
Food foods[100];
int FoodsOfnumbr =0;
/*
读取磁盘文件里面的菜单信息
*/
int readFood()
{
int i;
FILE* p = fopen("d://food.txt", "r");
if (!p)
{
return 0;
}
for (i = 0; !feof(p); i++)
{
fscanf(p, "%s\t%d\t%s\n", &foods[i].name, &foods[i].price, &foods[i].kind);
FoodsOfnumbr++;
}
fclose(p);
return 1;
}
/*
把菜单信息写入磁盘文件
*/
int writeFood()
{
int i;
FILE* p = fopen("d://food.txt", "w+");
if (p)
{
for (i = 0; i < FoodsOfnumbr; i++)
{
fprintf(p, "%s\t%d\t%s\n", foods[i].name, foods[i].price, foods[i].kind);
}
fclose(p);
return 1;
}
else
{
return 0;
}
}
/*
输出菜单
*/
void printFoods()
{
int i;
if(FoodsOfnumbr==0)
{
printf("暂时还没有任何菜式\n");
return ;
}
printf("菜单如下:\n");
printf("菜单号\t菜名\t价格\t分类\n");
for (i = 0; i < FoodsOfnumbr; i++)
{
printf("%d\t%s\t%d\t%s\n", i, foods[i].name, foods[i].price, foods[i].kind);
}
}
/*
修改菜式
*/
void changeFood(int p)
{
int action;
char name[128] = {0};
char kind[128] = {0};
int price;
if(p < 0 || p >= FoodsOfnumbr)
{
printf("没有菜单号为%d的菜\n", p);
return;
}
printf("当前菜式的信息:\n") ;
printf("编号\t菜名\t价格\t分类\n");
printf("%d\t%s\t%d\t%s\t", p, foods[p].name, foods[p].price, foods[p].kind);
printf("请选择:\n");
printf("1.修改菜名\n");
printf("2.修改价格\n");
printf("3.修改分类\n");
scanf("%d", &action);
switch(action)
{
case 1:printf("请输入新菜名 :");
scanf("%s", name);
setName(&foods[p], name); break;
case 2:printf("请输入它的价格 :");
scanf("%d", &price);
setPrice(&foods[p], price); break;
case 3:printf("请输入它的分类 :");
scanf("%s", kind);
setKind(&foods[p], kind); break;
default:printf("输入错误\n");
}
}
/*
删除菜式
*/
void deleteFood(int p)
{
int i;
if( p < 0 || p >= FoodsOfnumbr)
{
printf("没有菜单号为%d的菜\n", p);
return;
}
for (i = p; i < FoodsOfnumbr; i++)
{
foods[i] = foods[i+1];
}
FoodsOfnumbr--;
printf("删除成功!\n");
}
/*
增加菜式
*/
void addFood()
{
int i;
char n[128] = {0};
char k[128] = {0};
int p;
int word;
printf("输入新的菜式的信息: \n");
do
{
printf("菜名 : ");
scanf("%s", n);
word = 0 ;
for(i = 0 ; i < FoodsOfnumbr; i++)
{
if(strcmp(n, foods[i].name) == 0)
{
word = 1;
printf("已经有%s这个菜了!请重新输入!\n", n);
break;
}
}
}while(word);
printf("价格 :");
scanf("%d", &p);
printf("分类 :");
scanf("%s", k);
setName(&foods[FoodsOfnumbr], n);
setKind(&foods[FoodsOfnumbr], k);
setPrice(&foods[FoodsOfnumbr], p);
FoodsOfnumbr++;
}
void showDish()
{
int action = 0;
int pos = 0;
do
{
printFoods();
printf("\n\n");
printf( "1.修改菜单\n");
printf("2.删除菜式\n");
printf("3.添加菜式\n");
printf("4.退出\n");
scanf("%d", &action);
switch (action)
{
case 1:
printf("输入要修改的菜 :");
scanf("%d", &pos);
changeFood(pos);
break;
case 2:
printf("输入要删除的菜 :");
scanf("%d", &pos);
deleteFood(pos);
break;
case 3:
addFood();
break;
case 4: return;
default:
printf("非法操作!");
}
}
while (1);
}
typedef struct _client
{
int money;
int rpMoney;
int b[100];
int number;
}client;
void resetclient(client* c)
{
c->money = 0;
c->number = 0;
c->rpMoney = 0;
memset(c->b, 0, sizeof(c->b));
}
void add(client* c)
{
int i;
int p ;
printf("请出入你想点的菜单号:");
scanf("%d", &p);
if (p > FoodsOfnumbr - 1)
{
printf("没有这个菜\n");
return;
}
for(i=0; i < c->number; i++)
{
if(p==c->b[i])
{
printf("您已经点过这个菜了!\n");
return ;
}
}
c->b[c->number++] = p ;
c->money += foods[p].price;
}
void deletefood(client* c)
{
int p ;
int i;
if(c->money == 0)
{
printf("您还没有点菜\n");
return;
}
for(i = 0; i < c->number; i++)
{
printf("%d\t%s\t%d\n", c->b[i], foods[c->b[i]].name, foods[c->b[i]].price);
}
printf("共 %d 元\n", c->money);
printf("请出入你想撤销的菜单号:");
scanf("%d", &p);
for( i = 0; i <= c->number; i++)
{
if(c->b[i] == p)
{
c->b[i] = c->b[c->number--] ;
c->money -= foods[p].price;
}
}
if(i > c->number) printf("没有点这个菜\n");
}
void show(client* c)
{
int i;
if(c->money == 0) return ;
printf("您已点:\n");
for(i=0; i < c->number; i++)
{
printf("%s\t%d元\n", foods[c->b[i]].name, foods[c->b[i]].price);
}
printf("共 %d 元\n", c->money);
}
void showclient(client* c)
{
int pos;
printFoods();
printf("\n\n");
do
{
printf("请选择:\n");
printf("1.点菜\n");
printf("2.取消\n");
printf("3.结账\n");
printf("4.退出\n");
scanf("%d", &pos);
switch(pos)
{
case 1: add(c);break;
case 2: deletefood(c);break;
case 3: printf("你共消费了 %d 元钱。\n欢迎您下次光临!\n", c->money); return ;
case 4 : return ;
default:
printf("非法操作!\n");
}
show(c);
}
while(1);
}
int main()
{
client c;
int i;
resetclient(&c);
memset(foods, 0, sizeof(foods));
readFood();
while(1)
{
printf(" ***************************欢迎使用点餐管理系统!****************************\n");
printf("------------------------------1.用户点餐-------------------------------------\n");
printf("------------------------------2.菜单管理-------------------------------------\n");
printf("------------------------------3.退出-----------------------------------------\n");
scanf("%d", &i);
switch(i)
{
case 1:showclient(&c);break;
case 2:showDish();break;
case 3:writeFood();system("pause");return 0;
default: printf("输入错误,请重新选择!\n");break;
}
}
return 0;
}
你自己的测试一下,有啥错的地方自己改改就好那是个空文件,用来保存录入的信息的 你把你的 “d://food.txt” 贴上来一份
你把你的 “d://food.txt” 贴上来一份