Question: write a program and a menu to provide options for addition, subtraction, multiplication, or division. After obtaining your selection, the program requests two numbers and then performs the selected operation. This program should only accept the menu options it provides. It should use the float type and allow users to enter a number again if they can. In the case of division, if the user inputs 0 as the second number, the program should prompt the user to enter a new value. A typical program runs as follows:
Enter the operation of you choice:a.add s.subtractm.multiply d.divideq.quitaEnter first number : 22.4Enter second number :oneone is not an number.Please enter a number . such as 2.5,-1.78E8, or 3: 122.4+1=23.4Enter the operation of you choice:a.add s.subtractm.multiply d.divideq.quitdEnter first number : 18.4Enter second number: 0Enter a number other than 0:0.218.4/0.2=92Enter the operation of you choice:a.add s.subtractm.multiply d.divideqBye.
My answer is as follows:
# Include <stdio. h> # include <stdbool. h> # include <stdlib. h> void menu (); bool menu_switch (char input); char menu_input (); float get_num (); void add (); void subtract (); void multiply (); void divide (); void clear_buffer (); // set int main (void) {bool _ exit; For (_ exit = false; menu (), just in case (), (_ exit = menu_switch (menu_input ()))! = True;)/* Get the return value of the selected item and assign _ exit to determine whether to exit */continue; printf ("Bye. \ n "); Return 0;} void menu () {printf (". add \ t \ ts. subtract \ n "); printf (" M. mulyiply \ TD. divide \ n "); printf (" Q. quit \ n "); printf (" >>> ");} bool menu_switch (char input) {Switch (input) {Case 'A': add (); clear_buffer (); // to ensure that the buffer is absolutely clean, each computing function is cleared once after the end to clear the selection options or return false for residual data in the buffer when a number is entered; case's ': Subtract (); clear_buffer (); Return false; Case 'M ': Multiply (); clear_buffer (); Return false; Case 'D': Divide (); clear_buffer (); Return false; Case 'q': Return true; default: printf ("[Please enter true option] \ n"); Return false ;}} char menu_input () {int ch; char output; while (CH = getchar ())! = '\ N') {output = (char) ch; // obtain the ASCII value of the first character stored in CH and forcibly convert it to the char type stored in output clear_buffer (); // clear the remaining content in the buffer and return output; // return the output value to menu_switch for selection and determination} float get_num () {float input; int ch; while (scanf ("% F", & input )! = 1) {// determines whether the input value is a float value. Otherwise, the system prompts the user to enter the value while (CH = getchar () in a loop ())! = '\ N') putchar (CH); printf (": Please enter true number (such as 3,-13, 2e6):") ;}return input ;} void add () {float fnum, snum; printf ("Please enter first number >>>"); fnum = get_num (); printf ("Please enter second number >>>"); snum = get_num (); printf ("%. 2f + %. 2f = %. 2f \ n ", fnum, snum, fnum + snum);} void subtract () {float fnum, snum; printf (" Please enter first number >>> "); fnum = get_num (); print F ("Please enter second number >>>"); snum = get_num (); printf ("%. 2f-%. 2f = %. 2f \ n ", fnum, snum, fnum-snum);} void multiply () {float fnum, snum; printf (" Please enter first number >>> "); fnum = get_num (); printf ("Please enter second number >>>>"); snum = get_num (); printf ("%. 2f * %. 2f = %. 2f \ n ", fnum, snum, fnum * snum);} void divide () {float fnum, snum; printf (" Please enter first number >>> "); while (( Fnum = get_num () = 0) {// determines whether it is 0. If it is 0, the user is prompted to re-enter printf ("Enter the number than 0:"); continue ;} printf ("Please enter second number >>>"); While (snum = get_num () = 0) {printf ("Enter the number than 0 :"); continue;} printf ("%. 2f/%. 2f = %. 2f \ n ", fnum, snum, fnum/snum);} void clear_buffer () {While (getchar ()! = '\ N') continue ;}
Function Description:
- Main () Sequential Control
- Menu (); display menu content
- Menu_switch (); select an item
- Menu_input (); select input
- Get_num (); Numeric Input
- Add (); Addition
- Subtract (); Subtraction
- Multiply (); multiplication
- Divide (); Division
- Clear_buffer (); force read of content in the buffer
The flowchart is as follows:
Code exposure