HW4 Assignment

What to do?

You must turn in your program by Wednesday, October 20th, 2021 at 11:59am (NOON).
In this assignment, you will be developing a program that simulates a top-of-the-line calculator which performs advanced commands such as addition, subtractions, multiplication, division, exponentiation, and factorial! The program will accept mathematical expressions in "infix" notation such as...

( ( 27 + 452 ) * 2 - 16 ^ 2 ) / 7

convert it to its "postfix" equivalent expression...

27 452 + 2 * 16 2 ^ - 7 /

and then evaluate the "postfix" expression.
Note: All operations are integer operations. Don't worry about negative input.

The methods to write in Calc.java:

long eval (LongStack stack1);
long intopost (LongStack stack1);

static long exponent (long power, long base);
static long fact (long xxx, long ignored);
static long setupword (char character);

The files you need to copy over from your hw3:

LongStack.java

You do not need to write any debug statements in the Calc.java file, but when you run ./Calc -x, you will see the debug statements you originally wrote in LongStack.java to track allocating, jettisoning, and internal operations.

Function Descriptions

eval:

Utilizing 2 Stacks, evaluate mathematical expressions from "postfix" notation. Refer to the Calc.java.empty for the algorithm.

  • Value of stack1 /* input parameter */ is expected to be a reference to a LongStack object containing "postfix" expressions to evaluate.
  • intopost:

    Utilizing 2 Stacks, convert "infix" mathematical expressions entered by the user into their "postfix" equivalents. Refer to the Calc.java.empty for the algorithm.
    Hint: You will need to use MyLib.getchar() to read input and then push it to your stacks. It has similar functionality to fgetc, which you implemented in hw1.
    Another hint:You will also use MyLib.ungetc() to put a character back to the input stream.

  • Value of stack1 /* output parameter */ is expected to be a reference to an empty LongStack object used to store "postfix" expressions.
  • exponent: /* called from eval */

    Raising base to the power exponent.

  • Values of power are expected to be any positive integer.
  • Values of base are expected to be any positive integer.
  • fact: /* called from eval */

    Calculating xxx factorial.

  • Values of xxx are expected to be any positive integer.
  • Values of ignored can be any valid integer. (But remember to not use magic numbers)
  • setupword: /* called from intopost */

    Constructor funtion for longs representing operators to be stored on your LongStack objects. The representation for the operators should have everything associated with that operator:

  • Values of character are expected to be ASCII codes for operators entered by the user. These values are also expected to be found inside the operators[] array.
  • Grading breakdown

    Commenting/Documentation:
    20%
    Correctness/Execution:
    60%
    Style:
    20%

    How to get started?

    Copy and past the following commands into your terminal

    cd
    cd ../public/hw4
    make install
    cd
    cd hw4/java
    cp ~/hw3/java/LongStack.java LongStack.java
    mv Calc.java.empty Calc.java
    ls