Question
For this assignment, implement a simple stack calculator which can compute an infix expression. It should take in a string containing an infix expression, compute
For this assignment, implement a simple stack calculator which can compute an infix expression. It should take in a string containing an infix expression, compute the result, and print it out. It should handle operators +, -, *, / and parenthesis.
Your program must have two main steps -- first convert the expression to postfix, and then compute the result using the algorithms discussed in class and textbook. These algorithms require that you use a stack. You must implement your own stack, you may not use a library that implements a stack. No credit will be given if you don't implement your own stack.
Although your textbook contains implementations of a stack, I encourage you to try and implement your own stack, using the textbook as a reference if you need it. You can keep your stack simple if you wish -- e.g. it doesnt need to be templated, it can just hold a simple data type like char. Additionally, it doesnt need to handle error conditions because we are guaranteed a string containing a syntactically correct infix expression. You may implement either an array-based stack or a link-based stack.
To keep things simple, you may make the following assumptions:
- there are no spaces or other whitespace in the string
- all the operands are single digits
- the result of every operation is a single digit. For example, 2+3 is allowed because the result is 5. 5*3 is not allowed because the result is 15, which is two digits. 5+3+4 is not allowed because even though the first operation is 8, a single digit, the result of the second operation is 12, two digits. 5+3-4 is allowed because the result of the first operation is 8, and the result of the second operation is 4
- any string entered contains a valid, syntactically correct infix expression with balanced parenthesis (if any)
Conversion between int and char
The expression contains both char and int data, because each operator is a char and each operand is a digit. The easiest way to handle this is to implement a stack which supports char data. Since we know all our operands are single digits, we can simply push the character representing the digit onto the stack. Note this character will be the ASCII value of the character, not the integer value! As an example, the character '7' is ASCII value 55, '8' is 56, etc. If you need the actual integer value of the character, in this case 7, there is a convenient way to determine it. You can subtract the ASCII value of zero, '0' from the character. For example,, the following code will store 7 in i:
char c = '7';
int i = c - '0';
To get the character back, you can add '0' to an integer.
Extra Credit
For up to 10% extra credit, modify your program to remove the assumption that the string contains a valid, syntactically correct infix expression. It should compute the value if the string is valid, and gracefully handle an invalid string.
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