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
Example of user input:
For the process of computing the total, you should follow the following steps:
Step : Store the entire userinputted expression in a string variable, say problem.
Step : 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 : Write a boolean function for multiplication and division
higher precedence
Step : Write another boolean function for addition and subtraction
lower precedence
Step : Use a loop to check for operators As you find each
operator, extract the numbers before and after the operator.
Step : Once you identify an operator, copy the relevant part of the
expression into a new string, evaluate it eg if the operator is divide
the two numbers and store the result.
Step : 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 : 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 : 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 : 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 YN
Sample Output:
Enter a problem to solve:
Result:
Do you want to try again YN Y
Enter a problem to solve:
Result:
Do you want to try again YN N
Goodbye!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
