Home > Back-end >  Query fruit prices
Query fruit prices

Time:11-04

2-5 query fruit prices (20 points)
Given four kinds of fruits, the apple (apple), pear, pear, orange (orange), grape (grape), the unit price corresponding to 3.00 yuan/kg, 2.50 yuan/kg, 4.10 yuan/kg, 10.20 yuan/kg,
First on the screen the following menu:
[1] apple
[2] the pear
[3] orange
[4] grape
[0] exit
Users can input number 1 ~ 4 query corresponding to the unit price of fruit when continuous query more than 5 times, program should quit query; Less than five times the user input 0 exit; Enter a different number, according to the price of 0,
Input format:

Input on a line given in user input of a number of consecutive Numbers,
The output format:

Displayed on the screen menu first, and then corresponding each input of users, according to the format in one line "price=price" output query results, in which prices keep two decimal places, when a user query more than 5 times in a row, or active input 0, end of the program,
Input the sample 1:

3-1 0 2
The output sample 1:

[1] apple
[2] the pear
[3] orange
[4] grape
[0] exit
Price=4.10
Price=0.00
Enter the sample 2:

1, 2, 3, 4, 3, 4, 5 6 7 8
The output sample 2:

[1] apple
[2] the pear
[3] orange
[4] grape
[0] exit
Price=3.00
Price=2.50
Price=4.10
Price=4.10
Price=10.20




#include
Int main ()
{
Int choice, I;
Double price.
for(i=1; i<=5; I++) {
Printf (" [1] apple \ n ");
Printf (" [2] the pear \ n ");
Printf (" [3] orange \ n ");
Printf (" [4] grape \ n ");
Printf (" [0] exit \ n ");

The scanf (" % d ", & amp; Choice);
If (choice==0)
break;
The switch (choice) {
Case 1: price=3.00; break;
Case 2: price=2.50; break;
Case 3: price=4.10; break;
Case 4: price=10.20; break;
Default: price=0.00; break;
}
Printf (" price=% 2 lf \ n ", price);
}
return 0;
}

CodePudding user response:

for(i=1; i<=5; I++) personal feel to
While (1) or for (;; ) is better, with a variable queryTimes record query times, reached the break out of the loop, if the input to zero also break out of circulation,
Other code didn't scan,

CodePudding user response:

 int choice, I; 
Double price.
Printf (" [1] apple \ n ");
Printf (" [2] the pear \ n ");
Printf (" [3] orange \ n ");
Printf (" [4] grape \ n ");
Printf (" [0] exit \ n ");
for(i=1; i<=5; I++) {


The scanf (" % d ", & amp; Choice);
If (choice==0)
break;
The switch (choice) {
Case 1: price=3.00; break;
Case 2: price=2.50; break;
Case 3: price=4.10; break;
Case 4: price=10.20; break;
Default: price=0.00; break;
}
Printf (" price=% 2 lf \ n ", price);
}
//return 0;
getchar();
getchar();
getchar();
getchar();
return 0;

CodePudding user response:

This the if (choice==0)
break;
What is used for

CodePudding user response:

A little bit more simple:
 
#include

Int main ()
{
Float prices []={0, 3, 2.5, 4.1, 10.2};

Puts (" [1] [2] pear apple \ n \ n [3] orange \ n [4] grape \ n [0] exit ");
For (int n=0, c; The scanf (" % d ", & amp; C)==1 & amp; & C & amp; & N & lt; 5; N++)
Printf (" price=% 2 f \ n ", prices [c & gt; 0 & amp; & amp; c & lt; 5? C: 0]).

return 0;
}

  • Related