USING C++ Please 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 infix expression to postfix, and

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
icon
Concept explainers
Question

USING C++

Please 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 infix expression to postfix, and then compute the result using the algorithms discussed in videos/pdfs 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 only if you need it.  You can keep your stack simple -- e.g. it doesn’t need to be templated, it can just hold a simple data type like char.  Additionally, it doesn’t 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.

Assumptions

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.  If there are parenthesis in the expression, they are balanced.

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.

 

If you can please improve your program to remove the assumption that the string contains a valid, syntactically correct infix expression.  It should compute the result if the string is valid, and gracefully handle an invalid string, for example by outputting an error message. 

 
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 infix expression to postfix, and then compute the result
using the algorithms discussed in videos/pdfs 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 only if you need it. You can keep your stack simple -- e.g. it doesn't need to be
templated, it can just hold a simple data type like char. Additionally, it doesn't 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.
Assumptions
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. If there are parenthesis in the expression,
they are balanced.
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, 'O' 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.
Transcribed Image Text: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 infix expression to postfix, and then compute the result using the algorithms discussed in videos/pdfs 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 only if you need it. You can keep your stack simple -- e.g. it doesn't need to be templated, it can just hold a simple data type like char. Additionally, it doesn't 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. Assumptions 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. If there are parenthesis in the expression, they are balanced. 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, 'O' 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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 16 images

Blurred answer
Knowledge Booster
Depth First Search
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning