【正文】
/*如果當(dāng)前文件不存在,提示打開文件失敗 */ if (fp == NULL) { printf(\n Open file %s fail!End with any key.\n,DataFile)。 perror(Open file fail)。南昌航空大學(xué)科技學(xué)院學(xué)士學(xué)位論文25 getch()。 exit(1)。 } /*如果成功打開或創(chuàng)建文件,則提示輸入商品序號(hào)、名稱、價(jià)格要素等相關(guān)信息*/ printf(input product infotmation is 0 means input is end.\n)。 printf(Number is not exceed 9 figures,Name is not exceed 20 characters,range of grade:~\n)。 /*循環(huán)從鍵盤上讀取顧客輸入的序號(hào)、名稱、價(jià)格要素等相關(guān)信息*/ while(count = SIZE) { /*輸入序號(hào),如為 0 則停止輸入*/ printf(\n input 39。number =039。 means end input.\n)。 printf(number=)。 scanf(%ld,amp。)。 if ( == 0 ) break。 /*提示輸入商品名稱 */ printf(name=)。 scanf(%s,)。 /*提示輸入商品商品價(jià)格 */ printf(price=)。 scanf(%f,amp。)。 /*提示輸入商品折扣 */ printf(discount=)。 scanf(%f,amp。)。 /*用公式自動(dòng)計(jì)算會(huì)員價(jià) */ =*。 printf(\n)。 /*如遇無法寫入文件的異常,則加以提示 */ if(fwrite(amp。TmpS,sizeof(product),1,fp)!=1) { printf(\nwrite file %s fail!End with any key\n,DataFile)。 perror(Write file fail )。 getch()。 exit(1)。 } count++。 }南昌航空大學(xué)科技學(xué)院學(xué)士學(xué)位論文26 /*如果輸入的數(shù)據(jù)量超過最大允許的范圍,則提示數(shù)據(jù)不能錄入*/ if (countSIZE) printf(\nsorry,number of data can not exceed%d\n,SIZE)。 fclose(fp)。 /*====在屏幕上顯示文件內(nèi)容====*/ /* clrscr()。*/ printf(The data you input is store successful %s in file.\n,DataFile)。 printf(Content as follow:\n)。 fp=fopen(DataFile,rb)。 if (fp == NULL) { printf(\nOpen file%sfail!End with any key \n,DataFile)。 perror(Open file fail)。 getch()。 exit(1)。 } printf(\nNumber\tName\tprice\tdiscount\tmemberprice\n)。 while(fread(amp。TmpS,sizeof(product),1,fp) != (int)NULL) { printf(\n%ld\t%s\t%\t%\t\t%\n,,T)。 } fclose(fp)。}(4)void AddRecord()功能:增加商品信息記錄。根據(jù)顧客輸入的文件名打開商品價(jià)格信息文件,進(jìn)行添加記錄操作,如沒有文件存在,則創(chuàng)建。將顧客輸入的若干條商品信息記錄追加存入該文件并保存。輸入商品序號(hào)為 0,結(jié)束輸入。最后列表顯示文件內(nèi)容。輸入:(1)要追加商品信息記錄的文件名。(2)若干條商品信息記錄。(3)輸入序號(hào)為 0,結(jié)束輸入。 處理:(1)接受顧客輸入的文件名、打開文件,當(dāng)文件不存在時(shí),創(chuàng)建文件。(2)提示顧客逐條輸入商品信息記錄。(3)獲取顧客輸入的商品信息,通過價(jià)格、折扣用公式計(jì)算會(huì)員價(jià)。南昌航空大學(xué)科技學(xué)院學(xué)士學(xué)位論文27(4)將完整的商品價(jià)格記錄追加存入文件。(5)循環(huán)判斷顧客輸入的商品序號(hào),如為 0則結(jié)束輸入。(6)讀取商品信息文件,列表顯示商品信息。輸出:列表顯示顧客追加商品信息記錄后的文件內(nèi)容。程序清單:/*:添加商品信息記錄*/include void AddRecord(){ FILE *fp = NULL。 /*定義指向文件的指針*/ product TmpS。 /*定義進(jìn)行操作時(shí)的臨時(shí)結(jié)構(gòu)體變量 */ char DataFile[40] = 。 /*存儲(chǔ)商品信息的文件名*/ int count = 1。 /*計(jì)算可輸入數(shù)據(jù)的最大范圍*/ /*====輸入要添加商品信息的文件名====*/ printf(\n please input the product information file name to add record:)。 printf(\n Notice:Name of file can39。t exceed 8 can39。t exceed 3 characters,part of exceed will be discarded.\n)。 gets(DataFile)。 /*如顧客沒有輸入,則循環(huán)提示顧客輸入 */ while(*DataFile == (39。\039。)) { printf(\n please input new file name to store data,end with enter.)。 printf(\n Notice:Name of file can39。t exceed 8 characters,suffix can39。t exceed 3 of exceed will be discarded.\n)。 gets(DataFile)。 } fp = fopen(DataFile,a+)。/*a+:當(dāng)文件存在時(shí),追加,當(dāng)文件不存在時(shí),創(chuàng)建*/ /*如果當(dāng)前文件不存在,提示打開文件失敗 */ if (fp == NULL) { printf(\n Open file %s fail!End with any key.\n,DataFile)。 perror(Open file fail)。 getch()。 exit(1)。 } /*如果成功打開或創(chuàng)建文件,則提示輸入商品序號(hào)、名稱、價(jià)格要素等相關(guān)信息*/ printf(input number,name and is 0 means input is end.\n)。 printf(Number is not exceed 9 figures,Name is not exceed 20 characters,range of grade:~\n)。 /*循環(huán)從鍵盤上讀取顧客輸入的序號(hào)、名稱、價(jià)格要素等相關(guān)信息*/南昌航空大學(xué)科技學(xué)院學(xué)士學(xué)位論文28 while(count = SIZE) { /*輸入序號(hào),如為 0 則停止輸入*/ printf(\n input 39。number =039。 means end input.\n)。 printf(number=)。 scanf(%ld,amp。)。 if ( == 0 ) break。 /*提示輸入商品名稱 */ printf(name=)。 scanf(%s,)。 /*提示輸入商品價(jià)格 */ printf(price=)。 scanf(%f,amp。)。 /*提示輸入商品折扣 */ printf(discount=)。 scanf(%f,amp。)。 /*用公式自動(dòng)計(jì)算商品會(huì)員價(jià) */ =*。 printf(\n)。 /*如遇無法寫入文件的異常,則加以提示 */ if(fwrite(amp。TmpS,sizeof(product),1,fp)!=1) { printf(\nwrite file %s fail!End with any key\n,DataFile)。 perror(Write file fail )。 getch()。 exit(1)。 } count++。 } /*如果輸入的數(shù)據(jù)量超過最大允許的范圍,則提示數(shù)據(jù)不能錄入*/ if (countSIZE) printf(\nsorry,number of data can not exceed%d\n,SIZE)。 fclose(fp)。/*====在屏幕上顯示文件內(nèi)容====*/ /* clrscr()。*/ printf(The data you input is store successful %s in file.\n,DataFile)。 printf(Content as follow:\n)。南昌航空大學(xué)科技學(xué)院學(xué)士學(xué)位論文29 fp=fopen(DataFile,rb)。 if (fp == NULL) { printf(\nOpen file%sfail!End with any key \n,DataFile)。 perror(Open file fail)。 getch()。 exit(1)。 } printf(\nNumber\tName\tprice\tdiscountmemberprice\n)。 while(fread(amp。TmpS,sizeof(product),1,fp) != (int)NULL) { printf(\n%ld\t%s\t%\t%\t%\n,,Tm)。 } fclose(fp)。}(5)void DelRecord()功能:刪除。根據(jù)顧客輸入的文件名打開商品價(jià)格信息文件,進(jìn)行刪除記錄操作,如沒有文件存在,這提示打開文件失敗。輸入:(1)要?jiǎng)h除商品價(jià)格信息記錄的文件名。(2)要?jiǎng)h除商品的商品號(hào)碼。(3)是否繼續(xù)刪除的選項(xiàng)字母。處理:(1)接收顧客輸入的文件名、用追加方式打開文件,當(dāng)文件不存在時(shí),提示錯(cuò)誤。(2)提示顧客輸入要?jiǎng)h除商品的商品序號(hào)。(3)將文件中對(duì)應(yīng)的商品記錄刪除。(4)將進(jìn)行刪除操作后的文件進(jìn)行保存。(5)讀取文件,列表顯示文件內(nèi)容。程序清單:* :刪除商品信息記錄*/include void DelRecord() {/南昌航空大學(xué)科技學(xué)院學(xué)士學(xué)位論文30 int i,j,k。 long delnum。 /*存放顧客輸入的要?jiǎng)h除商品序號(hào)*/ product TmpS。 /*定義進(jìn)行操作時(shí)的臨時(shí)結(jié)構(gòu)體變量*/ product s[SIZE]。/*SIZE,在 頭文件中定義的常量,值為 100 */ int recNumber。 /*原文件中的記錄數(shù)*/ char DataFile[40] = ,next。 /*DataFile 存儲(chǔ)商品信息的文件名,next 為是否進(jìn)行下一次刪除操作的選項(xiàng)*/ FILE *fp。/*====fp 指針指向存儲(chǔ)數(shù)據(jù)的文件名 ====*/ printf(\nplease input the name of production information file.\n)。 gets(DataFile)。