Use a while loop and a case structure to to design a program where the user will
ID: 3639529 • Letter: U
Question
Use a while loop and a case structure to to design a program where the user will select one of four different mathematical operations to be performed on a set of inputs (X&Y).Each case structure will contain one of the mathematical operations. The front panel will have a numerical input for X, a numerical input for Y and an input for the user to select either "add", "subtract", "multiply" or "divide" as the mathematical operation to be performed. A numeric indicator on the front panel will show the result of the mathematical operation and a string indicator will show the equation (as shown below) that was used. Use a stop button on the front panel to end the program.
The four mathematical operations are:
X+Y
X-Y
X*Y
X/Y
Explanation / Answer
Let me explain you the problem in simple words first. There is a front end, which is the ex say a (GUI created by Java). In this GUI you will have a slot for X and a slot for Y to input X and Y, as we have a slot in calculator for inputting numbers. You also have options to select from Add, divide, multiply and subtract( say from a drop down menu). You have to perform the selected operation on the input numbers in the backend code and get the output and display it on the slot in the front end. Your program will then wait for next set of inputs until you press the stop button in front end. The best code that will execute when you press the STOP button is flag = 0 which will exit the program. The back-end code. while(flag == 1) // flag will become 0 only when you press the STOP button { switch(operator) // operator will get the value according to the option choice in front end { case 1 : output = X + Y; // X and Y have input numbers, output stores the output to be displayed. strcpy(str, "X+Y"); // str stores the output string break; case 2 : output = X - Y; // X and Y have input numbers, output stores the output to be displayed. strcpy(str, "X-Y"); break; case 3 : output = X * Y; // X and Y have input numbers, output stores the output to be displayed. strcpy(str, "X*Y"); break; case 4 : output = X / Y; // X and Y have input numbers, output stores the output to be displayed. strcpy(str, "X/Y"); break; } } plz feel free to ask doubts if any
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.