- /*前言:
- 進入管理界面的密碼爲:206;
- 銷售日誌記錄在sale.log中,售貨機中的零鈔數量記錄在change.log中,
- 售貨機中的商品信息(包括狀態(上架或下架,上架記錄爲1,下架記錄爲0),
- 數量,編號和價格)記錄在saleinfo.log中;
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <conio.h>
- void xsjm(void); /*銷售界面函數*/
- void lcjm(void); /*零鈔界面函數*/
- void gljm(void); /*管理界面函數*/
- void InputSale(FILE *fp); /*商品上架函數*/
- void InputChange(FILE *fp2); /*設置零鈔數目函數*/
- void RemoveSale(FILE *fp); /*商品下架函數*/
- void Account(int chos1, int chos2, FILE *fp, FILE *fp2, FILE *fp3); /*商品交易計算函數*/
- void Search(FILE *fp3); /*打印銷售日誌函數*/
- void Stat(FILE *fp2); /*查詢零鈔數目和銷售額函數*/
- void Stat_2(FILE *fp3); /*銷售統計函數*/
- #define MAX 100 /*定義sale.log中最多存儲100條銷售記錄*/
- struct SALE /*定義商品結構*/
- {
- int state; /*自定義1或0,初始化爲0*/
- int amount; /*自定義,初始化爲0*/
- int num; /*1,2,3,4*/
- int price; /*1,2,5,5*/
- }sale[4]={{0,0,1,1},{0,0,2,2},{0,0,3,5},{0,0,4,5}};
- struct TIME
- {
- int month;
- char eng_month[5];
- }times[12]={{1,"Jan"},{2,"Feb"},{3,"Mar"},{4,"Apr"},{5,"May"},{6,"Jun"},
- {7,"Jul"},{8,"Aug"},{9,"Sep"},{10,"Oct"},{11,"Nov"},{12,"Dec"}};
- int change[3]={{0},{0},{0}};
- main()
- {
- int choose, choose1, choose2;
- FILE *fp, *fp2, *fp3;
- xsjm();
- scanf("%d", &choose1);
- while (choose1!=0)
- {
- if (choose1==206)/*如果輸入206則進入管理界面*/
- {
- system("cls");
- gljm();
- scanf("%d", &choose);
- while (choose!=0)
- {
- switch (choose)
- {
- case 1:
- if ((fp=fopen("saleinfo.log","r+"))==NULL)
- {
- printf("open saleinfo.log fail!/n");
- }
- InputSale(fp);
- fclose(fp);
- break;
- case 2:
- if ((fp2=fopen("change.log","r+"))==NULL)
- {
- printf("open change.log fail!/n");
- }
- InputChange(fp2);
- fclose(fp2);
- break;
- case 3:
- if ((fp2=fopen("change.log","r+"))==NULL)
- {
- printf("open change.log fail!/n");
- }
- Stat(fp2);
- printf("Press any key to continue.../n");
- getch();
- fclose(fp2);
- break;
- case 4:
- if ((fp3=fopen("sale.log","r"))==NULL)
- {
- printf("open sale.log fail!/n");
- }
- Search(fp3);
- printf("Press any key to continue.../n");
- getch();
- fclose(fp3);
- break;
- case 5:
- if ((fp=fopen("saleinfo.log","r+"))==NULL)
- {
- printf("open saleinfo.log fail!/n");
- }
- RemoveSale(fp);
- printf("商品下架成功!/n");
- printf("Press any key to continue.../n");
- getch();
- fclose(fp);
- break;
- case 6:
- if ((fp3=fopen("sale.log","r"))==NULL)
- {
- printf("open sale.log fail!/n");
- }
- Stat_2(fp3);
- printf("Press any key to continue.../n");
- getch();
- fclose(fp3);
- break;
- default:
- printf("Input error!Press any key to continue.../n");
- getch();
- }
- system("cls");
- gljm();
- scanf("%d", &choose);
- }
- }
- if (choose1==1||choose1==2||choose1==3||choose1==4)
- {
- system("cls");
- lcjm();
- scanf("%d", &choose2);
- switch (choose2)
- {
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- if ((fp=fopen("saleinfo.log","r+"))==NULL)
- {
- printf("open saleinfo.log fail!/n");
- }
- if ((fp2=fopen("change.log","r+"))==NULL)
- {
- printf("open change.log fail!/n");
- }
- if ((fp3=fopen("sale.log","a+"))==NULL)
- {
- printf("open sale.log fail!/n");
- }
- Account(choose1, choose2, fp, fp2, fp3);
- printf("Press any key to continue...");
- getch();
- fclose(fp);
- fclose(fp2);
- fclose(fp3);
- break;
- default:
- printf("Input error!Press any key to continue.../n");
- getch();
- break;
- }
- }
- system("cls");
- xsjm();
- scanf("%d", &choose1);
- }
- }
- void Stat_2(FILE *fp3)
- {
- int i, j, num[MAX], sort[MAX] , amount[MAX], out_1[MAX], out_5[MAX], year[MAX], date[MAX];
- int lines=0;
- int month_2[MAX];
- int start_year, start_month, start_date, end_year, end_month, end_date;
- int sold_1=0,sold_2=0,sold_3=0,sold_4=0;
- int start=-1, end=-1;
- char week[MAX][5], month[MAX][5], time[MAX][10];
- fseek(fp3, 0, 0);
- for (i=0; !feof(fp3)&&i<MAX; i++)
- {
- fscanf(fp3, "%d/t/t%d/t%d/t%d/t%d/t%s %s %d %s %d/n",&num[i], &sort[i],
- &amount[i], &out_1[i], &out_5[i],&week[i], &month[i],
- &date[i],&time[i], &year[i]);
- lines++;
- }
- for (i=0; i<lines; i++)
- {
- for (j=0; j<12; j++)
- {
- if(strcmp(month[i],times[j].eng_month)==0)
- month_2[i]=times[j].month;
- }
- }
- printf("請輸入統計開始時間:/n例如 2008年3月2日則輸入:2008,03,02/n:");
- scanf("%d,%d,%d", &start_year, &start_month, &start_date);
- printf("請輸入統計結束時間:");
- scanf("%d,%d,%d", &end_year, &end_month, &end_date);
- for (i=0; i<lines; i++)
- {
- if (start_year==year[i]&&start_month==month_2[i]&&start_date==date[i])
- {
- start=i;
- break;
- }
- }
- for (i=0; i<lines; i++)
- {
- if (end_year==year[i]&&end_month==month_2[i]&&end_date==date[i])
- {
- end=i;
- }
- }
- if (start==-1||end==-1||start>end)
- {
- printf("Input error or date not exist!/n");
- return;
- }
- for (i=start; i<=end; i++)
- {
- if (num[i]==1)
- sold_1++;
- if (num[i]==2)
- sold_2++;
- if (num[i]==3)
- sold_3++;
- if (num[i]==4)
- sold_4++;
- }
- printf("商品1賣出數量%d,銷售額爲%d/n", sold_1, sold_1*sale[0].price);
- printf("商品2賣出數量%d,銷售額爲%d/n", sold_2, sold_2*sale[1].price);
- printf("商品3賣出數量%d,銷售額爲%d/n", sold_3, sold_3*sale[2].price);
- printf("商品4賣出數量%d,銷售額爲%d/n", sold_4, sold_4*sale[3].price);
- }
- void Stat(FILE *fp2)
- {
- int i, sum;
- printf("對帳:/n");
- for (i=0; i<3; i++)
- {
- fscanf(fp2, "%5d/n", &change[i]);
- }
- fscanf(fp2, "%5d/n", ∑);
- printf("當前1元數量爲%d,5元數量爲%d,10元數量爲%d./n", change[0],change[1],change[2]);
- printf("銷售總額爲%d/n", sum);
- }
- void Search(FILE *fp3)
- {
- char ch;
- int *p=NULL;
- int i, j, k, choose, num[MAX], sort[MAX] , amount[MAX], out_1[MAX], out_5[MAX], year[MAX];
- int lines=0, TempInt;
- char TempChar[10];
- char week[MAX][5], date[MAX][5], month[MAX][5], time[MAX][10];
- printf("銷售日誌:/n");/*打印銷售日誌*/
- while(!feof(fp3))
- {
- ch=fgetc(fp3);
- putchar(ch);
- }
- p=(int*)malloc(sizeof(int)*MAX);
- fseek(fp3, 0, 0);
- for (i=0; !feof(fp3); i++)
- {
- fscanf(fp3, "%d/t/t%d/t%d/t%d/t%d/t%s %s %s %s %d/n",&num[i], &sort[i],
- &amount[i], &out_1[i], &out_5[i],&week[i], &month[i],
- &date[i],&time[i], &year[i]);
- lines++;
- }
- printf("請選擇:1.用選擇法排序 2.用交換法排序:");
- scanf("%d", &choose);
- while (choose!=1&&choose!=2)
- {
- printf("輸入錯誤!/n");
- printf("請選擇:1.用選擇法排序 2.用交換法排序:");
- scanf("%d", &choose);
- }
- switch (choose)
- {
- case 1:
- for (i=0; i<lines-1; i++)
- {
- k=i;
- for (j=i+1; j<lines; j++)
- {
- if (num[j]<num[k])
- {
- k=j;
- }
- }
- if (k!=i)
- {
- TempInt=num[k];
- num[k]=num[i];
- num[i]=TempInt;
- TempInt=sort[k];
- sort[k]=sort[i];
- sort[i]=TempInt;
- TempInt=amount[k];
- amount[k]=amount[i];
- amount[i]=TempInt;
- TempInt=out_1[k];
- out_1[k]=out_1[i];
- out_1[i]=TempInt;
- TempInt=out_5[k];
- out_5[k]=out_5[i];
- out_5[i]=TempInt;
- strcpy(TempChar,week[k]);
- strcpy(week[k],week[i]);
- strcpy(week[i],TempChar);
- strcpy(TempChar,month[k]);
- strcpy(month[k],month[i]);
- strcpy(month[i],TempChar);
- strcpy(TempChar,date[k]);
- strcpy(date[k],date[i]);
- strcpy(date[i],TempChar);
- strcpy(TempChar,time[k]);
- strcpy(time[k],time[i]);
- strcpy(time[i],TempChar);
- TempInt=year[k];
- year[k]=year[i];
- year[i]=TempInt;
- }
- }
- for (i=0; i<lines; i++)
- {
- printf("%d/t/t%d/t%d/t%d/t%d/t%s %s %s %s %d/n",num[i], sort[i],
- amount[i], out_1[i], out_5[i],week[i], month[i],
- date[i],time[i], year[i]);
- }
- break;
- case 2:
- for (i=0; i<lines-1; i++)
- {
- for (k=i+1; k<lines; k++)
- {
- if (num[k]<num[i])
- {
- TempInt=num[k];
- num[k]=num[i];
- num[i]=TempInt;
- TempInt=sort[k];
- sort[k]=sort[i];
- sort[i]=TempInt;
- TempInt=amount[k];
- amount[k]=amount[i];
- amount[i]=TempInt;
- TempInt=out_1[k];
- out_1[k]=out_1[i];
- out_1[i]=TempInt;
- TempInt=out_5[k];
- out_5[k]=out_5[i];
- out_5[i]=TempInt;
- strcpy(TempChar,week[k]);
- strcpy(week[k],week[i]);
- strcpy(week[i],TempChar);
- strcpy(TempChar,month[k]);
- strcpy(month[k],month[i]);
- strcpy(month[i],TempChar);
- strcpy(TempChar,date[k]);
- strcpy(date[k],date[i]);
- strcpy(date[i],TempChar);
- strcpy(TempChar,time[k]);
- strcpy(time[k],time[i]);
- strcpy(time[i],TempChar);
- TempInt=year[k];
- year[k]=year[i];
- year[i]=TempInt;
- }
- }
- }
- for (i=0; i<lines; i++)
- {
- printf("%d/t/t%d/t%d/t%d/t%d/t%s %s %s %s %d/n",num[i], sort[i],
- amount[i], out_1[i], out_5[i],week[i], month[i],
- date[i],time[i], year[i]);
- }
- break;
- }
- free(p);
- }
- void Account(int chos1, int chos2, FILE *fp, FILE *fp2, FILE *fp3)
- {
- time_t t;
- int i, pri=0, diff=0, innum, into, sum=0;
- char name[10];
- int out_1=0, out_5=0;
- pri=sale[chos1-1].price;
- switch(chos1)
- {
- case 1:
- strcpy(name, "糖果");
- break;
- case 2:
- strcpy(name, "餅乾");
- break;
- case 3:
- strcpy(name, "可樂");
- break;
- case 4:
- strcpy(name, "雪碧");
- break;
- }
- for (i=0; i<4; i++)
- {
- fscanf(fp, "%5d %5d %5d %5d/n", &sale[i].state,&sale[i].amount,&sale[i].num,&sale[i].price);
- }
- for (i=0; i<3; i++)
- {
- fscanf(fp2, "%5d/n", &change[i]);
- }
- fscanf(fp2, "%5d/n", ∑);
- if (sale[chos1-1].amount==0||sale[chos1-1].state==0)
- {
- printf("該商品已經售完或下架,交易失敗。。。/n");
- return;
- }
- switch (chos2) /*按菜單計算用戶投入零錢*/
- {
- case 1:
- into=1;
- innum=1;
- break;
- case 2:
- into=1;
- innum=2;
- break;
- case 3:
- into=1;
- innum=5;
- break;
- case 4:
- into=5;
- innum=1;
- break;
- case 5:
- into=10;
- innum=1;
- break;
- }
- diff=into*innum-pri; /*計算投入零錢與商品價格的差價*/
- if (diff<0)
- {
- printf("投入現金太少,交易失敗。。。/n");
- return;
- }
- else if (diff<5)
- {
- if (change[0]>=diff)
- {
- out_1=diff;
- change[0]-=out_1;
- switch (into)
- {
- case 1:
- if (innum==1)
- change[0]+=1;
- if (innum==2)
- change[0]+=2;
- if (innum==5)
- change[0]+=5;
- break;
- case 5:
- change[1]+=1;
- break;
- case 10:
- change[2]+=1;
- break;
- }
- fseek(fp2,0, 0);
- fprintf(fp2, "%5d/n", change[0]);
- fseek(fp2,1*7, 0);
- fprintf(fp2, "%5d/n", change[1]);
- fseek(fp2,2*7, 0);
- fprintf(fp2, "%5d/n", change[2]);
- fseek(fp,(chos1-1)*25, 0);
- fprintf(fp,"%5d %5d %5d %5d/n",sale[chos1-1].state, sale[chos1-1].amount-1,sale[chos1-1].num,sale[chos1-1].price);
- time(&t);
- fprintf(fp3,"%d/t/t%2d/t%d/t%d/t%d/t%s",sale[chos1-1].num,into,innum,out_1,out_5,ctime(&t));
- sum+=sale[chos1-1].price;
- fseek(fp2, 3*7, 0);
- fprintf(fp2, "%5d/n", sum);
- printf("交易成功!已吐出%s商品!找您%d元!包括%d張1元,%d張5元。/n", name, diff, out_1, out_5);
- return;
- }
- else
- {
- printf("售貨機內零錢不足,交易失敗。。。/n");
- return;
- }
- }
- else
- {
- if (change[1]!=0)
- {
- if (change[0]>=(diff-5))
- {
- out_5=1;
- out_1=diff-5;
- change[1]-=out_5;
- change[0]-=out_1;
- switch (into)
- {
- case 1:
- if (innum==1)
- change[0]+=1;
- if (innum==2)
- change[0]+=2;
- if (innum==5)
- change[0]+=5;
- break;
- case 5:
- change[1]+=1;
- break;
- case 10:
- change[2]+=1;
- break;
- }
- fseek(fp2,0, 0);
- fprintf(fp2, "%5d/n", change[0]);
- fseek(fp2,1*7, 0);
- fprintf(fp2, "%5d/n", change[1]);
- fseek(fp2,2*7, 0);
- fprintf(fp2, "%5d/n", change[2]);
- fseek(fp,(chos1-1)*25, 0);
- fprintf(fp,"%5d %5d %5d %5d/n",sale[chos1-1].state, sale[chos1-1].amount-1,sale[chos1-1].num,sale[chos1-1].price);
- time(&t);
- fprintf(fp3,"%d/t/t%d/t%d/t%d/t%d/t%s",sale[chos1-1].num,into,innum,out_1,out_5,ctime(&t));
- sum+=sale[chos1-1].price;
- fseek(fp2, 3*7, 0);
- fprintf(fp2, "%5d/n", sum);
- printf("交易成功!已吐出%s商品!找您%d元!包括%d張1元,%d張5元。/n", name, diff, out_1, out_5);
- return;
- }
- else
- {
- printf("售貨機內零錢不足,交易失敗。。。/n");
- return;
- }
- }
- else
- {
- printf("售貨機內零錢不足,交易失敗。。。/n");
- return;
- }
- }
- }
- /*商品上架函數*/
- void InputSale(FILE *fp)
- {
- int num, amount;
- printf("請輸入上架商品編碼(1,2,3,4):");
- scanf("%d", &num);
- while (num!=1&&num!=2&&num!=3&&num!=4)
- {
- printf("輸入錯誤!/n");
- printf("請輸入上架商品編碼(1糖果,2餅乾,3可樂,4雪碧):");
- scanf("%d", &num);
- }
- fseek(fp,(num-1)*25, 0);
- fscanf(fp, "%5d %5d %5d %5d/n", &sale[num-1].state,&sale[num-1].amount,&sale[num-1].num,&sale[num-1].price);
- printf("當前該商品數量爲:%d/n", sale[num-1].amount);
- printf("請輸入要設置的數量:", num);
- scanf("%d", &amount);
- while (amount<0||amount>10000)
- {
- printf("輸入數目太大或太小!/n");
- printf("請輸入要設置的數量:", num);
- scanf("%d", &amount);
- }
- sale[num-1].amount=amount;
- sale[num-1].state=1;
- fseek(fp,(num-1)*25, 0);
- fprintf(fp,"%5d %5d %5d %5d/n",sale[num-1].state, sale[num-1].amount,sale[num-1].num,sale[num-1].price);
- system("cls");
- }
- /*零鈔設置函數*/
- void InputChange(FILE *fp2)
- {
- int i, sort, amount, num;
- printf("請輸入貨幣面額(1, 5, 10):");
- scanf("%d", &sort);
- while (sort!=1&&sort!=5&&sort!=10)
- {
- printf("輸入錯誤!/n");
- printf("請輸入貨幣面額(1, 5, 10):");
- scanf("%d", &sort);
- }
- switch (sort)
- {
- case 1:
- num=0;
- break;
- case 5:
- num=1;
- break;
- case 10:
- num=2;
- break;
- }
- for (i=0; i<3; i++)
- {
- fscanf(fp2, "%5d/n", &change[i]);
- }
- printf("當前%d元數量爲%d/n", sort, change[num]);
- printf("請輸入該貨幣數量:");
- scanf("%d", &amount);
- change[num]=amount;
- while (amount<0||amount>10000)
- {
- printf("輸入數目太大或太小!/n");
- printf("請輸入該貨幣數量:");
- scanf("%d", &amount);
- }
- fseek(fp2, num*7, 0);
- fprintf(fp2, "%5d/n", change[num]);
- system("cls");
- }
- /*商品下架函數*/
- void RemoveSale(FILE *fp)
- {
- int num, i;
- printf("請輸入要下架的商品編號(1,2,3,4):");
- scanf("%d", &num);
- while (num!=1&&num!=2&&num!=3&&num!=4)
- {
- printf("輸入錯誤!/n");
- printf("請輸入要下架的商品編號(1,2,3,4):");
- scanf("%d", &num);
- }
- sale[num-1].state=0;
- for (i=0; i<4; i++)
- {
- fprintf(fp,"%d %d %d %d/n",sale[i].state, sale[i].amount,sale[i].num,sale[i].price);
- }
- system("cls");
- }
- /*銷售界面*/
- void xsjm(void)
- {
- printf(" 進入管理界面密碼:206/n");
- printf("*********銷售界面************/n");
- printf("* 請選擇您要購買的商品! */n");
- printf("* 1. 糖果 1元 */n");
- printf("* 2. 餅乾 2元 */n");
- printf("* 3. 可樂 5元 */n");
- printf("* 4. 雪碧 5元 */n");
- printf("* 0. 退出程序 */n");
- printf("*****************************/n");
- }
- /*零鈔界面*/
- void lcjm(void)
- {
- printf("*********零鈔界面************/n");
- printf("* 請選擇投入零鈔類型及界面!*/n");
- printf("* 1. 1元 * 1 */n");
- printf("* 2. 1元 * 2 */n");
- printf("* 3. 1元 * 5 */n");
- printf("* 4. 5元 * 1 */n");
- printf("* 5. 10元 * 1 */n");
- printf("* 0. 返回銷售界面 */n");
- printf("*****************************/n");
- }
- /*管理界面*/
- void gljm(void)
- {
- printf("**********管理界面***********/n");
- printf("* 1. 商品上架 */n");
- printf("* 2. 零鈔設置 */n");
- printf("* 3. 對帳功能 */n");
- printf("* 4. 銷售日誌查詢 */n");
- printf("* 5. 商品下架 */n");
- printf("* 6. 銷售統計 */n");
- printf("* 0. 返回銷售界面 */n");
- printf("*****************************/n");
- }
自動售貨機銷售系統 C
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.