想请教一道题 谢谢
一个产品有两种版本:其一是标准版,价格是$3.5,其二是豪华版,价格是$5.5。编写一个程序,提示用户输入产品的版本和数量,然后根据输入的产品数量,计算并输出价格。
#include <stdio.h>
int main(void)
{
double total_price = 0.0; /* Total price */
int type = 0; /* Product type */
int quantity = 0; /* Quantity ordered */
const double type1_price = 3.50;
const double type2_price = 5.50;
/* Get the product type */
printf("Enter the type (1 or 2): ");
scanf("%d", &type);
/* Get the order quantity */
printf("Enter the quantity: ");
scanf("%d", &quantity);
/* Calculate the total price */
total_price = quantity*(type1_price + (type-1)*(type2_price-type1_price));————————这步不懂什么意思 /* Output the area */
printf("The price for %d of type %d is $%.2f\n", quantity, type, total_price);
return 0;
}
[解决办法]
total_price = quantity*(type1_price + (type-1)*(type2_price-type1_price));type为1时表示是标准版,值为2表示豪华版type-1如果为0,即为标准版,则total_price = quantity * type1_pricetype-1如果为1,即为豪华版,则total_price = quantity*(type1_price + (type-1)*(type2_price-type1_price));其中(type-1)*(type2_price-type1_price)表示的是豪华版比标准版贵的差价.这个其实就是一个算术问题,只是用程序语言表示出来了而已~
[解决办法]
如果认为我说的对你有帮助就丢个饭碗哈,你看假如你输入的是类型1,也就是type=1,这个时候总价格total_price=quantity*type1_price,也就是类型1的数量和单价的乘积了,;假如你输入的事类型2,,也就是type=2,此时把type=2带入表达式展开,得到total_price=quantity*type2_price,也就是类型2的数量和单价的乘积
[解决办法]
total_price = quantity*(type1_price + (type-1)*(type2_price-type1_price));