#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetltemName(char *item, FILE *file);
int GetltemPrice(FILE *file);
void AcceptMoney(int price);
//symbolic constant definitions
#define CAPACITY 10 //max number itemavailable to vend
int main(void)
{
char name[CAPACITY][20]={0};
int price[CAPACITY]={0};
FILE *stockfile;
int i,Nitems,choice;
int listend;
stockfile=fopen("","r");
if(stockfile==NULL)
{
printf("Error initialising not opened");
}
else
{
//read stock from file
listend=0;
Nitems=0;
do
{
price[Nitems]=GetltemPrice(stockfile);
GetltemName(name[Nitems],stockfile);
if(price[Nitems]>0)
{
Nitems++;
}
else
{
listend=1;
}
}
while(Nitems<CAPACITY&&!listend);
//print menu
printf("***Menu***\n");
for(i=0;i<Nitems;i++)
{
printf("\n%d:%20s:%dp",i,name[i],price[i]);
}
//request selection
printf("\n\n");
do
{
printf("Please make your chioce(enter CTRL-C to quit);");
scanf("%d",&choice);
}
while(choice<0||choice>Nitems-1);
printf("%s seleted.\n",name[choice]);
AcceptMoney(price[choice]);
printf("\nEnjoy your %s.\n",name[choice]);
}
fclose(stockfile);
return 0;
}
void GetltemName(char *item,FILE *file)
{
int len;
if(feof(file))
{
item[0]='\0';
}
else
{
fgets(item,20,file);
len=strlen(item);
if(item[len-1]=='\n')
{
item[len-1]='\0';
}
}
}
void AcceptMoney(int price)
{
int money=0,coin,left;
printf("Please pay %d p. 10p,5p,2p,1p accepted. No change given.\n\n",price);贪吃蛇的编程代码
do
{
left=price;
printf("Price: %d, paid: %d, to pay: %d,",price,money,left);
do
{
printf("please enter coin:\n");
scanf("%d",&coin);
}
while(coin!=1&&coin!=2&&coin!=5&&coin!=10);
money=money+coin;
left=price-money;
}
while(left>0);
printf("%d p paid. Thank you.",money);
}
int GetltemPrice(FILE *file)
{ int j;
if(feof(file))
{
j='\0';
}
else
{
fscanf(file,"%d",&j);
}
return j;
}
COMMODITY FILE :
40Mars Bar
40Toffee Crisp
30Kit Kat
25Toblerone
40bounty
50Topic
0
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论