想请教一道题 谢谢
一个产品有两种版本:其一是标准版,价格是$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))
total_price = quantity * type1_price
+ quantity * (type-1) * (type2_price-type1_price)
如果type=1,说明购买的全部是标准版的。第二部分等于0
如果type=2,则结果是全部标准版的价格加上豪华版与标准版的差价
[解决办法]
显然得把格式输出符改成%f