PROGRAM DESCRIPTION

In this C++ program, you will use concepts from Chapters 1 through 3 to implement a calculator using stacks and queues in C++. In particular, your program will convert an infix expression to postfix, and then evaluate the expression.

REQUIREMENTS

  • Your calculator will support the following operators:
    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Modulo: %
    • Exponent: ^
    • Parentheses: ( and )
    • Operators should be processed using the standard PEMDAS order of operations. All operators, with the exception of exponent (^), have left-to-right associativity.
  • Your program should accept integers and floating-point numbers, including negative numbers, as operands. Note that integer division is not done with expressions such as 7 / 9. In other words, operations must be accurate as if entered using a calculator.
  • User input will be given in the form of an infix expression, where each operator and/or operand is separated by a single space. Your program should convert the infix expression to its postfix expression, and then evaluate the expression to yield its result.
  • You will use the STL for stacks and queues to implement your calculator. Specifically, you should use a stack to store the operators in support of the infix- to-postfix conversion. A queue should be used to store the resulting postfix expression that is printed to the terminal. You will then read from the queue to evaluate the expression, utilizing a second stack to store the operands in support of the evaluation process to print the result to the terminal.
  • You may assume valid infix expression input, though parentheses may be invalid (and need to be checked/matched). Please pay attention to the SAMPLE OUTPUT for specific details about the flow and input/output of the program.
  • Your code should be well documented in terms of comments. For example, good comments in general consist of a header (with your name, course section, date, and brief description), comments for each variable, and commented blocks of code.

SAMPLE OUTPUT

$ ./a.out
Enter a valid infix expression: 3 + 4 * 5 / 6
The resulting postfix expression is: 3 4 5 * 6 / +
The result is: 6.33333
$ ./a.out
Enter a valid infix expression: ( 20.4 + 3 ) * -5
The resulting postfix expression is: 20.4 3 + -5 *
The result is: -117
$ ./a.out
Enter a valid infix expression: 5 + ( ( 1 + 2 ) * 4 ) - 3
The resulting postfix expression is: 5 1 2 + 4 * + 3 -
The result is: 14
$ ./a.out
Enter a valid infix expression: ( 2 + 3 ) - 7 / 9
The resulting postfix expression is: 2 3 + 7 9 / -
The result is: 4.22222
$ ./a.out
Enter a valid infix expression: 7.5 - (2 * ( 10 % 6 ) )
The resulting postfix expression is: 7.5 2 10 6 % * -
The result is: -0.5
$ ./a.out
Enter a valid infix expression: 8.4 / 2.5 * ( 3.62 + 2.04 )
The resulting postfix expression is: 8.4 2.5 / 3.62 2.04 + *
The result is: 19.0176
$ ./a.out
Enter a valid infix expression: ( -3 * 4 ) / ( 1 + 5 ) * 7
The resulting postfix expression is: -3 4 * 1 5 + / 7 *
The result is: -14
$ ./a.out
Enter a valid infix expression: 6 + 3 * 4 ^ 2 - 5
The resulting postfix expression is: 6 3 4 2 ^ * + 5 -
The result is: 49
$ ./a.out
Enter a valid infix expression: 7.4 - 3.5 / ( 2.1 * 8.2 ^ 4.7 )
The resulting postfix expression is: 7.4 3.5 2.1 8.2 4.7 ^ * / -
The result is: 7.39992
$ ./a.out
Enter a valid infix expression: 2 * 3 ^ 1.05 ^ 1.84
The resulting postfix expression is: 2 3 1.05 1.84 ^ ^ *
The result is: 6.65221
$ ./a.out
Enter a valid infix expression: ( ( 4.2 - 3.5 ) * 3.4 / 4
The resulting postfix expression is: 4.2 3.5 - 3.4 * 4
Error: Mismatched Parentheses! Program Terminated.
$ ./a.out
Enter a valid infix expression: ( 4.2 - 3.5 ) * 3.4 / 4 )
The resulting postfix expression is: 4.2 3.5 - 3.4 * 4 /
Error: Mismatched Parentheses! Program Terminated.
$ ./a.out
Enter a valid infix expression: ( ( 4.2 - 3.5 ) * 3.4 / 4 )
The resulting postfix expression is: 4.2 3.5 - 3.4 * 4 /
The result is: 0.595
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.