Question
Can someone please help me with the following program. The concept of stack is extremely important in computer science and is used in a wide
The concept of stack is extremely important in computer science and is used in a wide variety of problems. This assignment requires you to write a program that can be used to evaluate ordinary arithmetic expressions that contains any of the five arithmetic operators (+, -, *, /, %).
This exercise requires three distinct steps, namely:-
Step 1 - Verify that the expression
Given an arithmetic expression, called an infixed expression, to verify that it is properly formed as far as parentheses are concerned, do the following:
{
If the character is a left parenthesis (, push it on to the stack. However if the character is a right parenthesis, ), visit the stack and pop the top element from off the stack.
}
Step 2 - Convert infixed expression to postfix
Given that an arithmetic expression is properly form with respect to parentheses, do the following:
{
After a symbol is scanned, there are four (4) basic rules to observed and apply accordingly:
Otherwise, if the symbol is either ( or ), check for the following conditions:
If the symbol is (, push on to the stack,
Otherwise
If the symbol is )
{
Pop everything from the operator stack down to the first (. Write each item
popped from the stack to the output string. Do not write the item ). Discard it.
}
If the operator on the top of the stack has higher or equal precedence, that operator is popped from off the stack, and is written to the to the output string. This process is continues until one of two things happen:
If the operator on the top of the stack has higher or equal precedence, that operator is popped from off the stack, and is written to the to the output string, and the recently scanned symbol is placed on the stack
OR
The operator on the stack has lower precedence than the one just scanned. When this situation arises, the recently scanned symbol is pushed onto the stack.
}
4. After the arithmetic expression is exhausted, any operator is remaining on the stack must be popped from off and is written to the output string.
Step 3 - Evaluate the post fixed expression
While there are more symbols in the postfix string
{
{
NOTE: If the stack does not have two operands, a malformed postfix expression has occurred, and evaluation should be terminated.
}
}
NOTE: If the stack is empty or if it has more than one operand remaining, the result is unreliable.
Extend this algorithm to include square brackets and curly braces. For example, expressions of the following kind should be considered:
Implement the above two algorithms for the following binary operators: addition +, subtraction -, multiplication *, division /, and modulus operation %. All operations are integer operations. To keep things simple, place at least one blank space between each token in the input string.
Please help
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