For this program you will be implementing a simple decimal calculator. By simple I mean it only need to support the binary operators +, -, *, and / with their standard definitions (i.e., addition, subtraction, multiplication, and division) and parenthesis. Multiplication and division have higher precedence than addition and subtraction, all of them have left to right associativity, and parentheses can be used to override the precedence.

The program will accept a string containing the expression to be evaluated and print the result of evaluating the expression. Some simple example are:

>2*3
6.0
>2*3.5-2
5.0
>2*(3.5-2)
3.0

You should evaluate the expression in a single left to right scan of the input string, using an operator and an operand stack to hold intermediate results during the evaluation. You may handle parenthesis either by a recursive call or by using the stack. You can ignore space completely in the user input. Consider any characters other than 0-9, ., +, -, *, and / as an error. Numbers can be integers (e.g., 2, which is 2.0) or reals (e.g., 2.0). You don't have to worry about scientific notation for inputs. You don't have to worry about negative numbers for inputs they are most easily handled using unary -, which we don't have.

You might consider developing your code in steps like we did in class.

  • Start with just integers and +, -, *, and / - that is, no parentheses, just integer parsing, and limited error checking. This will get the basic parsing and evaluation algorithm in place.
  • Add parenthesis handling. This can either use recursion to evaluate a parenthesized subexpression, or push the open parenthesis on the operator stack and handle it in the code.
  • Add real numbers. We will be using decimal floats see below.
  • Add error checking.

Decimal Floating Point

Since Python version 2.4, it has included support for decimal floating point arithmetic. See Section 9.4. decimal Decimal fixed point and floating point arithmetic in the Python 2.7.10 reference manual or Section 9.4 Decimal fixed point and floating point arithmetic in the Python 3.5.0 reference manual.

Report

include a report describing the problem, your approach to solving it (pseudo code, etc), the source code, and sample outputs. Your report should cite any sources of materials you used in solving the problem.

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.