Given four kinds of fruits, apples (apple)
, pears (pear)
, oranges (orange)
, and grapes (grape)
, the unit prices correspond to 3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤
.
First, the following menu is displayed on the screen:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
The user can enter the number to 1~4
query the unit price of the corresponding fruit. When the number of consecutive queries exceeds the number of 5
times, the program should automatically exit the query; 5
if the user enters less than the number of times 0
, it exits; enter other numbers, the display price is 0
.
Input format:
Enter a number of serial numbers entered by the user in a row.
Output format:
First display the menu on the screen. Then, corresponding to each input of the user, price = 价格
output the query result in the format " " in one line , and the price retains two decimal places. When the user continuously inquires more than 5 times, or actively enters 0, the program ends.
Input example 1:
3 -1 0 2
Output example 1:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 4.10
price = 0.00
Input example 2:
1 2 3 3 4 4 5 6 7 8
Output example 2:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 3.00
price = 2.50
price = 4.10
price = 4.10
price = 10.20
Code:
# include <stdio.h>
# include <stdlib.h>
int main() {
int num,i;
printf("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit\n");
for (i=0;i<5;i++) {
scanf("%d",&num);
if (num == 0) {
break;
}
switch(num) {
case 1 : printf("price = 3.00\n");break;
case 2 : printf("price = 2.50\n");break;
case 3 : printf("price = 4.10\n");break;
case 4 : printf("price = 10.20\n");break;
default : printf("price = 0.00\n");break;
}
}
return 0;
}
Submit screenshot:
Problem-solving ideas:
It switch
might be better to use multi-branch select statements!