C语言自动售卖机系统
头文件定义数据类型
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
typedef struct jc //检查
{
//1元数量
int yy;
//5元数量
int wy;
//10元数量
int sy;
}JE;//金额
//商品
typedef struct SP
{
//销售金额
int xsje;
//售出
int sc;
//库存
int kc;
char name[20];
}SP;
源码:
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
typedef struct jc //检查
{
//1元数量
int yy;
//5元数量
int wy;
//10元数量
int sy;
}JE;//金额
//商品
typedef struct SP
{
//销售金额
int xsje;
//售出
int sc;
//库存
int kc;
char name[20];
}SP;
int main()
{
//声明 fp 是 FILE型指针,用于指向FILE类型 (文件结构)
FILE *fp;
//数量,余额,商品
int sl, ye, sp, cd, pd;
//商品A,B,C
SP a, b, c;
JE money;
//初始化商品库存为50
a.kc = b.kc = c.kc = 50;
a.sc = b.sc = c.sc = 0;
//初始化纸币面额数量
money.yy = 200;
money.wy = 50;
money.sy = 0;
//销售金额=售出数量*单价
a.xsje = a.sc * 2;
b.xsje = b.sc * 3;
c.xsje = c.sc * 6;
//创建一个文件
fp = fopen("note.txt", "a");
do
{
//菜单
printf("\t\t^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n");
printf("\t\t^* ^*\n");
printf("\t\t^* ------欢迎使用自动售货机管理系统------ ^*\n");
printf("\t\t^* ^*\n");
printf("\t\t^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n");
printf("\n");
printf("\t\t 菜单:\n\n");
printf("\t***********************************************************************\n");
printf("\t************************自*动*售*货*机*系*统***************************\n");
printf("\t*******************1、库 存 查 询*********************\n");
printf("\t*******************2、商 品 上 架*********************\n");
printf("\t*******************3、销 售 查 询*********************\n");
printf("\t*******************4、会 计 查 询*********************\n");
printf("\t*******************5、结 算 功 能*********************\n");
printf("\t*******************6、销 售 界 面*********************\n");
printf("\t***********************************************************************\n");
printf("\t***********************************************************************\n");
printf("\t**请输入1-6的选择:");
scanf("%d",&cd);
switch(cd)
{
case 1:
printf("\t **********库*存*查*询*******************************\n");
printf("\t * 1、A商品 单价: 2元 *\n");
printf("\t * 2、B商品 单价: 3元 *\n");
printf("\t * 3、A商品 单价: 6元 *\n");
printf("\t ****************************************************\n");
printf("\t **请输入1-3选择:");
scanf("%d",&sp);
printf("\n");
switch(sp)
{
case 1:
printf("\t *******************************************\n");
printf("\t * 你选择的2元的商品 *\n");
printf("\t * 2元商品的库存量为:%d *\n",a.kc);
printf("\t *******************************************\n");
fputs("2元商品的库存量为", fp);
fprintf(fp, "%d\n", a.kc);
break;
case 2:
printf("\t *******************************************\n");
printf("\t * 你选择的3元的商品 *\n");
printf("\t * 3元商品的库存量为:%d *\n",b.kc);
printf("\t *******************************************\n");
fputs("3元商品的库存量为", fp);
fprintf(fp, "%d\n", b.kc);
break;
case 3:
printf("\t *******************************************\n");
printf("\t * 你选择的6元的商品 *\n");
printf("\t * 6元商品的库存量为:%d *\n",c.kc);
printf("\t *******************************************\n");
fputs("6元商品的库存量为", fp);
fprintf(fp, "%d\n", c.kc);
break;
}
printf("\t *输入任意数值返回上层\n");
system("PAUSE");
break;
case 2:
printf("\t**************商*品*上*架************************\n");
printf(" * 1、A商品 单价: 2元 *\n");
printf(" * 2、B商品 单价: 3元 *\n");
printf(" * 3、A商品 单价: 6元 *\n");
printf("\t*************************************************\n");
printf("\t **请输入1-3选择:");
scanf("%d",&sp);
printf("\n");
switch(sp)
{
case 1:
printf("\t * 你选择的2元的商品 *\n");
printf("\t * 请输入商品改变量: ");
scanf("%d", &sl);
a.kc += sl;
printf("\t *******************************************\n");
printf("\t * 2元商品的库存量为:%d *\n",a.kc);
printf("\t *******************************************\n");
fputs("2元商品的库存量为", fp);
fprintf(fp, "%d\n", a.kc);
break;
case 2:
printf("\t * 你选择的3元的商品 *\n");
printf("\t * 请输入商品改变量: ");
scanf("%d", &sl);
b.kc += sl;
printf("\t *******************************************\n");
printf("\t * 3元商品的库存量为:%d *\n",b.kc);
printf("\t *******************************************\n");
fputs("3元商品的库存量为", fp);
fprintf(fp, "%d\n", b.kc);
break;
case 3:
printf("\t * 你选择的6元的商品 *\n");
printf("\t * 请输入商品改变量: ");
scanf("%d", &sl);
c.kc += sl;
printf("\t *******************************************\n");
printf("\t * 6元商品的库存量为:%d *\n",c.kc);
printf("\t *******************************************\n");
fputs("6元商品的库存量为", fp);
fprintf(fp, "%d\n", c.kc);
break;
}
system("PAUSE");
break;
case 3:
printf("\t**************销*售*查*询************************\n");
printf(" * 1、A商品 单价: 2元 *\n");
printf(" * 2、B商品 单价: 3元 *\n");
printf(" * 3、A商品 单价: 6元 *\n");
printf("\t*************************************************\n");
printf("\t **请输入1-3选择:");
scanf("%d",&sp);
printf("\n");
switch(sp)
{
case 1:
printf("\t*******************销*售*查*询*******************\n");
printf(" * 你选择的2元的商品 *\n");
printf(" * 销售数量为: %d *\n", a.sc);
printf(" * 销售金额为: %d *\n", a.xsje);
printf("\t*************************************************\n");
fputs("2元商品的销售数量和金额为:", fp);
fscanf(fp, "%d", &a.sc);
fprintf(fp, "%d\n", a.xsje);
break;
case 2:
printf("\t**************销*售*查*询************************\n");
printf(" * 你选择的3元的商品 *\n");
printf(" * 销售数量为: %d *\n", b.sc);
printf(" * 销售金额为: %d *\n", b.xsje);
printf("\t*************************************************\n");
fputs("3元商品的销售数量和金额为:", fp);
fscanf(fp, "%d", &b.sc);
fprintf(fp, "%d\n", b.xsje);
break;
case 3:
printf("\t**************销*售*查*询************************\n");
printf(" * 你选择的6元的商品 *\n");
printf(" * 销售数量为: %d *\n", c.sc);
printf(" * 销售金额为: %d *\n", c.xsje);
printf("\t*************************************************\n");
fputs("6元商品的销售数量和金额为:", fp);
fprintf(fp, "%d", c.sc);
fprintf(fp, "%d\n", c.xsje);
break;
}
system("PAUSE");
break;
case 4:
printf("\t**********************会*计*查*询************************\n");
printf(" * 至今2元商品卖出:%d ,交易额为: %d *\n", a.sc, a.xsje);
printf(" * 至今3元商品卖出:%d ,交易额为: %d *\n", b.sc, b.xsje);
printf(" * 至今6元商品卖出:%d ,交易额为: %d *\n", c.sc, c.xsje);
printf("\t*********************************************************\n");
printf(" * 1元零钞剩余量为: %d *\n",money.yy);
printf(" * 5元零钞剩余量为: %d *\n",money.wy);
printf("\t*********************************************************\n");
fputs("至今2元商品的销售数量和金额为:", fp);
fprintf(fp, "%d,", a.sc);
fprintf(fp, "%d\n", a.xsje);
fputs("至今3元商品的销售数量和金额为:", fp);
fscanf(fp, "%d,", b.sc);
fprintf(fp, "%d\n", b.xsje);
fputs("至今6元商品的销售数量和金额为:", fp);
fprintf(fp, "%d,", c.sc);
fprintf(fp, "%d\n", c.xsje);
fputs("1元零钞剩余量为:",fp);
fprintf(fp, "%d\n", money.yy);
fputs("5元零钞剩余量为:",fp);
fprintf(fp, "%d\n", money.wy);
system("PAUSE");
//清屏
system("cls");
break;
case 5:
fputs("取出10元钞票数量:", fp);
fprintf(fp,"%d\n",money.sy);
money.sy = 0;
printf("\t*******************************************************\n");
printf("\t * 已取出10元钞票 \n");
printf("\t * 请输入1元钞票的初始值: ");
scanf("%d", &money.yy);
fputs("重置的1元钞票的初始值:",fp);
fprintf(fp,"%d\n", money.yy);
printf("\t * 请输入5元钞票的初始值: ");
scanf("%d",&money.wy);
//商品销量清零
a.sc = b.sc = c.sc = 0;
system("PAUSE");
system("cls");
break;
case 6:
while(sp!=110) //管理员密码为110
{
printf("\t * 请放入10元纸币 *\n");
printf("\t * 请输入10: *\n");
printf("\t ");
scanf("%d",&pd);
//目前只能放入10元纸币
if(pd == 10)
{
printf("\t *************销*****售***************************\n");
printf("\t *1、A商品 单价: 2元 *\n");
printf("\t *2、B商品 单价: 3元 *\n");
printf("\t *3、A商品 单价: 6元 *\n");
printf("\t *************************************************\n");
}
printf("\t **请输入1-3选择:");
scanf("%d",&sp);
switch(sp)
{
case 1:
while(1)
{
printf("\t *************************************************\n");
printf("\t * 你选择的是2元的商品 *\n");
printf("\t * 你选择的商品为:2元商品 *\n");
printf("\t * 请选择购买数量:");
scanf("%d",&sl);
if(sl>0&&sl<=5)
{
if(a.kc>=sl && (money.yy+money.wy*5)>=(10-sl*2))
{
ye = 10 - sl*2;
a.xsje += sl*2;
a.sc += sl;
a.kc -= sl;
money.sy++;
if(ye>5 && money.wy>=1)
{
money.wy-=1;
money.yy = money.yy - ye + 5;
}
else
{
money.yy = money.yy - ye;
}
printf("\t ****************************************************\n");
printf("\t * 请从商品口拿出商品及余额: %d 元 *\n",ye);
fputs("此次卖出2元商品数量及收入为:",fp);
fprintf(fp,"%d",sl);
fprintf(fp,"%d\n",sl*2);
system("PAUSE");
system("cls");
break;
}
else
{
system("cls");
printf("\t * 商品不足或零钞不足 *\n");
system("PAUSE");
break;
}
}
else
{
system("cls");
printf(" * 请输入正确数量 *\n");
system("PAUSE");
}
}
break;
case 2:
while(1)
{
printf("\t ***********************************\n");
printf("\t * 你选择的是3元的商品 *\n");
printf("\t * 你选择的商品为:3元商品 *\n");
printf("\t * 请选择购买数量:");
scanf("%d",&sl);
if(sl>0&&sl<=3)
{
if(b.kc>=sl && (money.yy+money.wy*5)>=(10-sl*3))
{
ye = 10 - sl*3;
b.xsje += sl*3;
b.sc += sl;
b.kc -= sl;
money.sy++;
if(ye>5 && money.wy>=1)
{
money.wy-=1;
money.yy = money.yy - ye + 5;
}
else
{
money.yy = money.yy - ye;
}
printf("\t ******************************************\n");
printf("\t * 请从商品口拿出商品及余额: %d 元*\n",ye);
fputs("此次卖出3元商品数量及收入为:",fp);
fprintf(fp,"%d",sl);
fprintf(fp,"%d\n",sl*2);
system("PAUSE");
system("cls");
break;
}
else
{
system("cls");
printf("\t * 商品不足或零钞不足 *\n");
system("PAUSE");
break;
}
}
else
{
system("cls");
printf(" * 请输入正确数量 *\n");
system("PAUSE");
}
}
break;
case 3:
while(1)
{
printf("\t ***********************************\n");
printf("\t * 你选择的是6元的商品 *\n");
printf("\t * 你选择的商品为:6元商品 *\n");
printf("\t * 请选择购买数量:");
scanf("%d",&sl);
if(sl>0&&sl<=1)
{
if(c.kc>=sl && (money.yy+money.wy*5)>=(10-sl*6))
{
ye = 10 - sl*6;
c.xsje += sl*6;
c.sc += sl;
c.kc -= sl;
money.sy++;
if(ye>5 && money.wy>=1)
{
money.wy-=1;
money.yy = money.yy - ye + 5;
}
else
{
money.yy = money.yy - ye;
}
printf("\t **************************************\n");
printf("\t * 请从商品口拿出商品及余额: %d 元*\n",ye);
fputs("此次卖出6元商品数量及收入为:",fp);
fprintf(fp,"%d",sl);
fprintf(fp,"%d\n",sl*6);
system("PAUSE");
system("cls");
break;
}
else
{
system("cls");
printf("\t * 商品不足或零钞不足 *\n");
system("PAUSE");
break;
}
}
else
{
system("cls");
printf(" * 请输入正确数量 *\n");
system("PAUSE");
}
}
break;
default:
system("PAUSE");
continue;
}
system("cls");
}
}
}while(cd<=6);
printf("****************************************\n");
printf(" * 请输入对应的正确数字 *\n");
printf(" * 程序结束! *\n");
printf("****************************************\n");
printf("\t^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n");
printf("\t^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n");
printf("\t^*^*^*^*^*^*退出系统^*^*^*^*^*^*^*^*^*\n");
printf("\t^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n");
printf("\t^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n");
fclose(fp);
return 0;
}
求大佬帮忙加新功能!!!感觉有点功能单调了,需要优化。