A. 請問c語言如何實現輸入一個商品的名字和購買數量,自動計算出總價,商品有蘋果和梨子,單價分別是10和
#define_CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
structgoods
{
chargoodsName[20];
intgoodNum;
};
intpriceCount(structgoodsinputGoods)
{
if(!strcmp(inputGoods.goodsName,"蘋果"))
returninputGoods.goodNum*10;
if(!strcmp(inputGoods.goodsName,"梨子"))
returninputGoods.goodNum*11;
else
return-1;
}
intmain()
{
structgoodsinputGoods={};
while(1)
{
printf("請輸入商品名字和購買數量 ");
scanf("%s%d",inputGoods.goodsName,&inputGoods.goodNum);
if(priceCount(inputGoods)!=-1)
printf("購買%s的總價是:%d ",inputGoods.goodsName,priceCount(inputGoods));
else
//printf("我頭像,驚喜")
printf("商品名字輸入有問題 ");
}
system("pause");
return0;
}
運行結果:
B. C語言程序
#include<stdio.h>
intmain()
{
floatval=0.0;
printf("輸入火腿腸數量:");
scanf("%f",&val);
floatnum=val*10.5;
printf(" 輸入方便麵數量:");
scanf("%f",&val);
num+=val*12.5;
printf(" 輸入牛奶數量:");
scanf("%f",&val);
num+=val*55.9;
printf(" 應付金額%f¥輸入支付金額:",num);
scanf("%f",&val);
printf(" 應找顧客金額:%f",val-num);
scanf("%f",&val);
return0;
}