Question: In this example, you will create a C + + program that evaluates a simple arithmetic expression with multiple operations and operands input by the

In this example, you will create a C++ program that evaluates a simple
arithmetic expression with multiple operations and operands input by the user. The program will prompt the user to enter a problem to solve, which the input. will be a string containing a basic arithmetic expression, which can include addition ('+'), subtraction ('-'), multiplication ('*'), and division ('/'), which will be the only valid operand that the user will need to include. For any other operand that the user inputs, it needs to display an error message. The
program should accept more than one operand. This means you should allow the user to enter complex expressions like 5+3-2*4/1.
Example of user input: "12+24-5/3*2"
For the process of computing the total, you should follow the following steps:
Step 1: Store the entire user-inputted expression in a string variable, say problem.
Step 2: Traverse the problem string and break it into smaller tokens
(numbers and operators) using a character array. This allows easier
manipulation of each character, so you can parse numbers and
operands.
Step 3: Write a boolean function for multiplication and division
(higher precedence).
Step 4: Write another boolean function for addition and subtraction
(lower precedence).
Step 5: Use a loop to check for operators (*,/,+,-). As you find each
operator, extract the numbers before and after the operator.
Step 6: Once you identify an operator, copy the relevant part of the
expression into a new string, evaluate it (e.g., if the operator is /, divide
the two numbers), and store the result.
Step 7: After performing the operation (like * or /), copy the result back into the original string, replacing the section of the expression that has
been evaluated.
Step 8: Continue evaluating the rest of the expression by repeating the process. Once multiplication and division are handled, the program will
then evaluate addition and subtraction using the same approach.
Step 9: If at any point an invalid operator is encountered, the program
should display an error and ask the user to input a valid expression.
Step 10: The program will continue looping through the problem string, replacing each section that contains an operator with the calculated result, until all operators are processed and the final result is computed.
Furthermore, include the following Boolean operations:
Multiplication and Division Boolean Function: This function will
handle all occurrences of * and /. It will loop through the character array and whenever a * or / is encountered, the numbers around it will be calculated, and the result will replace that section in the string.
Addition and Subtraction Boolean Function: This function will handle
all occurrences of + and -. After handling multiplication and division,
this function will process the remaining operations in the expression.
The program will output the result of the computation after parsing and evaluating the input expression. After outputting the result, it will ask the user:
"Do you want to try again (Y/N)?"
Sample Output:
Enter a problem to solve: 12+24-5/3*2
Result: 31
Do you want to try again (Y/N)? Y
Enter a problem to solve: 8*7-6+10/5
Result: 51
Do you want to try again (Y/N)? N
Goodbye!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!