14
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 stackD
: 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 or decimals) 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!!!
Notes:
- All error output to should got yo std err or closest equivalent.
- All numbers are constrained to 64-bit floating point.
Example programs:
50,47* -> 2350
50,47/ -> 0.94
100,8! -> 40320
100D* -> 10000
! -> StackUnderflowException!!!
5,2/! -> FloatingFactorialException!!!
4,3!2*/ -> 3
654,489,48,43/5*7D+-*% -> 77.68749999999909
-> EmptyProgram!!!
(I can add more if needed)
3
If it weren't for the Error cases, Vitsy could do this naturally (except converting
– Addison Crump – 2015-12-02T20:36:46.620!
toF
).I figured, that's partly why I included them. – J Atkin – 2015-12-02T20:37:34.903
3
Yours is somewhat broader in scope, though it may be arguable that its a duplicate: http://codegolf.stackexchange.com/questions/221/reverse-polish-notation
– Digital Trauma – 2015-12-02T22:29:58.463Wow, I forgot about that one. But I don't think they are dupes since you have to process errors and more operators are defined in mine. – J Atkin – 2015-12-02T23:07:27.123
654,489,48,43/5*7D+-*%
should return77.6875
. (43/48*5-(7+7)
should be(7+7)-43/48*5
) – user81655 – 2015-12-03T01:32:44.300Aww crap. Quite right, I messed it up. Fixing now. – J Atkin – 2015-12-03T01:43:49.740
Is it fine if we throw an exception with the correct text? Example:
throw 'EmptyProgram!!!';
– usandfriends – 2015-12-03T02:38:32.777Shoulda thought of that. No, you can't. This may seem arbitrary but kinda the idea is to create your own language with its own exceptions, you don't want to see the interpreter throwing exceptions. – J Atkin – 2015-12-03T02:59:33.570
Does that mean my answer is invalid? I designed it to be run without needing a console, so the error message is displayed, but if you looked at the console you would see that it threw an exception also. – user81655 – 2015-12-03T03:20:20.573
No, I think its fine. Just as long as the user can't see it by default. – J Atkin – 2015-12-03T04:30:16.827
In the requirement you only mentioned that in case of error a message has to be printed to STDERR. However your answer also exits in case of error. Is exiting mandatory or optional?
– manatwork – 2015-12-03T11:04:55.800Optional. I did it because exiting was the shortest way to stop the program. I didn't want to continue processing the program if it has errors – J Atkin – 2015-12-03T15:24:41.713