自动售货机销售系统 C
来源:互联网 发布:Windows 10专业版64位 编辑:程序博客网 时间:2024/05/01 19:47
- #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;
- int amount;
- int num;
- int price;
- }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)
- {
- 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");
- }