1. 自動售貨機c語言編程 急!!
#include "stdio.h"
#include "conio.h"
struct stDrink
{
float fPrice; // 價格
int iLeft; // 剩餘數
};
void Pay(stDrink* pstPay)
{
float fPay = 0.0f;
if ( pstPay->iLeft<=0 )
{
printf("商品已經售完\r\n");
return;
}
while(1)
{
scanf("%f",&fPay);
if ( fPay<pstPay->fPrice )
{
printf("金額不足,請重新付費\r\n");
}
else if ( fPay>pstPay->fPrice)
{
printf("找零錢%.1f元,請收好\r\n",fPay-pstPay->fPrice);
break;
}
else
{
printf("付費成功\r\n");
break;
}
}
pstPay->iLeft--;
}
int main()
{
stDrink stCoca = {2.5f,5}; // 可樂
stDrink stOrage = {3.5f,5}; // 橙汁
stDrink stMilkTea = {5.0f,5}; // 奶茶
char cInput = 0;
while(1)
{
cInput = getch();
switch(cInput)
{
case '1':
// 可樂
printf("可樂2.5元,請付費\r\n");
Pay(&stCoca);
break;
case '2':
// 橙汁
printf("橙汁3.5元,請付費\r\n");
Pay(&stOrage);
break;
case '3':
// 奶茶
printf("奶茶5元,請付費\r\n");
Pay(&stMilkTea);
break;
case '#':
// 設備維修中
printf("設備維修中\r\n");
break;
case 'e':
// 退出程序
return 0;
}
}
return 0;
}
2. c語言,編寫一個售貨機(POS)計算程序,用於水果店售貨員算賬.蘋果每千克3.2元,梨每千克1.96元,香
#include<stdio.h>
intmain(void)
{
floatp[4]={3.2,1.96,3,24};
floatw[4]={1.5,2,3,1.5};
floats=0;
inti;
for(i=0;i<4;i++)
s+=p[i]*w[i];
printf("%s%12s%12s%13s ","名稱","單價","重量","應付價錢");
printf("--------------------------------------------- ");
printf("%s%12.2f%12.2f%13.3f ","蘋果",p[0],w[0],p[0]*w[0]);
printf("%s%12.2f%12.2f%13.3f ","梨",p[1],w[1],p[1]*w[1]);
printf("%s%12.2f%12.2f%13.3f ","香蕉",p[2],w[2],p[2]*w[2]);
printf("%s%12.2f%12.2f%13.3f ","櫻桃",p[3],w[3],p[3]*w[3]);
printf("--------------------------------------------- ");
printf("%s%37.2f ","總計",s);
printf("%s%37.2f ","付款",100.0);
printf("%s%37.2f ","找零",100.0-s);
return0;
}