问题:
An online retailer sells five different products whose retail prices are shown in the following table:
Write a program that reads a series of number: (1) Product number and
(2) Quantity sold, and display the total retail value of all products sold (Use ‘switch’ statement)
#include<stdio.h>
#include<stdlib.h>
int main(void) {
/*零售计算,商品id从1到5,当商品id=-1时结束,输入1到5以外的数提示输入无效*/
int id, quan;
double total = 0;
double price1 = 0, price2 = 0, price3 = 0, price4 = 0, price5 = 0;
printf("Enter pairs of item numbers and quantities.\n");
printf("Enter -1 for the item number to end input.\n");
while (1) {
scanf_s("%d", &id);
if (id == -1) {
break;
}
scanf_s("%d", &quan);
switch (id) {
case 1: price1 = price1 + 2.98*quan;
break;
case 2:price2 = price2 + 4.50*quan;
break;
case 3:price3 = price3 + 9.98*quan;
break;
case 4:price4 = price4 + 4.49*quan;
break;
case 5:price5 = price5 + 6.87*quan;
break;
default:
printf("Invoid product code:%d\n",id);
printf("\tQuantity:%d\n", quan);
break;
}
}
total = price1 + price2 + price3 + price3 + price4 + price5;
printf("The total retail value was:%.2lf\n",total);
system("pause");
return 0;
}