//mm.h頭文件
#包括
#包括
# define mu _ num _ max 100//假設有100種菜品。
# defineLENsizeof(structMenuInfo)
structMenuInfo
{
intID
char menu name[20];
浮動價格;
} Menu[Menu _ NUM _ MAX];
/*基礎數據維護*/
voidAddMenu()
{
FILE * fp
intmenu _ num
Printf("妳要加幾道菜?:");
scanf("%d ",& ampmenu _ num);
for(inti = 0;我{
printf(" ");//addedthisline
Printf("請輸入ID:");
scanf("%d ",& amp菜單[i]。ID);
Printf("請輸入菜名:");
scanf("%s ",菜單[i]。MenuName);
Printf("請輸入[%s]的價格:",菜單[i]。菜單);
菜單[i]。價格= 0.0f//initialfloatprice
scanf("%f ",& amp菜單[i]。價格);
fflush(stdin);
}
if((fp=fopen("MenuInfo.dat "," ab"))==NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
退出(1);
}
for(intj = 0;j{
if(fwrite(& amp;Menu[j],LEN,1,fp)!= 1)//寫入數據
printf("Errorwritingfile。");
}
fclose(FP);//closefilepoint
}
void()
{
FILE * fp
printf(" ID name price ");//列標題
if((fp=fopen("MenuInfo.dat "," Rb ")= = NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
退出(1);
}
inti = 0;
做
{
fseek(fp,i*LEN,SEEK _ SET);//movefileheadlocation
if(fread(& amp;Menu[i],LEN,1,FP))//readdatasavetostructurevariable
{
Printf ("%d% 5s% 5.1f元",菜單[i]。ID,菜單[i]。菜單[i]。價格);
我;
}
}while(!feof(FP));
fclose(FP);
}
void()
{
FILE * fp
intMenuID
intto delete =-1;
inti = 0;
Printf("請輸入要刪除的菜的ID:");
scanf("%d ",& ampMenuID);
/* loadorreloadthefilendcheckthatrecord with atidexists */
if((fp=fopen("MenuInfo.dat "," Rb ")= = NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
退出(1);
}
做
{
fseek(fp,i*LEN,SEEK _ SET);//movefileheadlocation
if(fread(& amp;Menu[i],LEN,1,fp))
{
如果(菜單[i]。ID = = MenuID)to delete = I;
我;
}
}while(!feof(FP));
fclose(FP);
if(todelete==-1)
{
printf(" AmenuwiththatIDdoesn ' texist ");
}
其他
{
/* writerecordsbacktofileexcluding onetobedeleted */
if((fp=fopen("MenuInfo.dat "," WB ")= = NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
退出(1);
}
for(intj = 0;j
{
if(j==todelete)繼續;/* skipercordtobedeleted */
if(fwrite(& amp;Menu[j],LEN,1,fp)!= 1)//寫入數據
printf("Errorwritingfile。");
}
fclose(FP);//closefilepoint
}
}
voidFindMenu()
{
FILE * fp
intMenuID
boolfind _ mark = false
Printf("請輸入妳要找的菜的名稱ID:");
scanf("%d ",& ampMenuID);
printf(" ID name price ");//列標題
if((fp=fopen("MenuInfo.dat "," Rb ")= = NULL)//openbinaryfile
{
printf(" Can ' to penfile ");
退出(1);
}
inti = 0;
做
{
fseek(fp,i*LEN,SEEK _ SET);//movefileheadlocation
弗雷德(& ampMenu[i],LEN,1,FP);//readdatasavetostructurevariable
如果(菜單[i]。ID==MenuID)
{
Printf ("%d% 5s% 5.1f元",菜單[i]。ID,菜單[i]。菜單[i]。價格);
find _ mark = true
打破;
}
我;
}while(!feof(FP));
如果(!Find_mark)printf("尊敬的顧客,我們餐廳沒有您想要的。妳可以試試我們的特色菜。”);
fclose(FP);
}
/*基礎數據已經維護*/
//sc.cpp主文件
#包括
#包括
#包括“mm.h”
voidmain(無效)
{
//AddMenu();
//();
//find menu();
}