Skip to main content
7 of 10
added 13 characters in body
J Atkin
  • 5.1k
  • 1
  • 23
  • 53

Interpret StackyMath!

Time for you to implement my new stack based language! It's called StackyMath. This will be a stack based language with 8 operations on the stack and ways to add numbers to the stack.

List of operations:

  • /: Division. Performed on the top 2 numbers of the stack. Pushes the result back on the stack.
  • *: Multiplication. Performed on the top 2 numbers of the stack. Pushes the result back on the stack
  • -: Subtraction. Performed on the top 2 numbers of the stack. Pushes the result back on the stack
  • +: Addition. Performed on the top 2 numbers of the stack. Pushes the result back on the stack
  • ^: Exponentiation. Performed on the top 2 numbers of the stack. Pushes the result back on the stack
  • %: Modulo. Performed on the top 2 numbers of the stack. Pushes the result back on the stack
  • !: Factorial. Performed on the top number on the stack. Pushes the result back on the stack
  • D: Duplicate the top number on the stack

Operations defined in pseudo code:

  • /: push(pop divided by pop)
  • *: push(pop times pop)
  • -: push(pop minus pop)
  • +: push(pop plus pop)
  • ^: push(pop to the pop)
  • %: push(pop mod pop)
  • !: push(factorial pop)
  • D: t = pop; push(t); push(t)

How to push numbers to the stack:

Adding numbers to the stack is easy, just put the raw number in your program where you need it. If you need to put multiple numbers on the stack you can separate them with a comma (,). Your program will not need to process - numbers in the input, If the user wants one they should push the number they want negated, zero, and -. Numbers in the input of the program are also constrained to positive integers.

Input:

Your program should take the input on the command line, or from std in. Input will only consist of numbers (no scientific notation) delimited by , as needed, and the operations defined above.

Output:

Your program should print the number on the top of the stack.

Error cases:

  • If the program tries to over-pop the stack, you should print StackUnderflowException!!! .
  • If you have division by zero, print DivisionByZeroException!!!
  • If a number that exceeds 64-bits, either while executing the program or processing a number in the input, print NumberOverflowException!!!
  • If somehow you get a negative number on the top of the stack and you need to do a factorial, print NegativeFactorialException!!!
  • If you have a floating point number on the tops of the stack and the next operation is factorial, print FloatingFactorialException!!!
  • If no numbers are on the stack when the program exits (i.e. the program was empty) print EmptyProgram!!!

Note all error output to should got yo std err or closest equivalent.

Note that all numbers are constrained to 64-bit floating point.

Example programs:

50,47* returns 2350 (i.e. 47*50)
50,47/ returns 0.94 (i.e. 47/50)
100,8! returns 40320 (i.e. factorial of 8)
100D* returns 10000 (i.e. 100*100)
! returns StackUnderflowException!!!
5,2/! returns FloatingFactorialException!!! 4,3!2*/ returns 3 654,489,48,43/5*7D+-*% returns 77.68749999999909 (i.e. 43/48*5-(7+7)*489%654)
returns EmptyProgram!!!

(I can add more if needed)

J Atkin
  • 5.1k
  • 1
  • 23
  • 53