Question
Write a program to evaluate a mathematical expression as it is entered by the user. Here, the user will enter alternating numbers and operators, terminating
Write a program to evaluate a mathematical expression as it is entered by the user. Here, the user will enter alternating numbers and operators, terminating with a ‘.’. You are required to keep a running evaluated result for the expression and finally print the output when the user is done. Each ‘type’ of operation needs to have its own method. The expression will only consist of integers and you only need to account for the 5 basic integer math operations ( +, -, *, / and %). Make sure your program conforms to the following requirements:
1. Write 5 methods, one for each basic arithmetic operators. They take 2 parameters each and return a result.
2. In the main method, read a number and a character alternatively until you encounter a ‘.’.
3. Keep a running result variable, where you store the result of the calculation so far.
4. Once the user is done entering the expression, print out the result.
5. You can assume all the numbers are integer.
6. Make sure to call the functions to do the calculations. Don’t do the calculations in main().
7. You can assume that the input will be as expected. You don’t need to do any error checking here.
8. You can ignore operator precedence. Just evaluate expressions as they come in.
There are 2 sample runs here:
Enter the expression:
1
+
26
*
2
%
19
/
5
.
The result is 3
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started