# Basic Calculator (moved)

Root / Submissions / [.]

Nathaniel

**Created:****Version:**1.0

**Size:**

This program can still be downloaded here.
Just a simple calculator. It still has a few bugs. like, 144^144 outputs infinity, large numbers don't quite work, and a few others. Please notify me if you find any more.

Well, this is a nice calculator, but... this isn't RPN (aka postfix), it's infix. Maybe you saw someone on Miiverse mention the shunting-yard algorithm, but that is for infix, not RPN.
RPN is a different way of writing math, it's not the usual syntax we normally use.

Infix: 1 * 2 + 3 / (4 - 5) RPN: 1 2 * 3 4 5 - / + PN: + * 1 2 / 3 - 4 5Note that RPN and PN don't require parentheses at all, nor an order of operations. In addition, RPN in particular is very simple to parse and evaluate. You can simply read it left to right, using a stack:

Push 1 onto stack (1) Push 2 onto stack (1, 2) Multiply top two items on stack (2) Push 3 onto stack (2, 3) Push 4 onto stack (2, 3, 4) Push 5 onto stack (2, 3, 4, 5) Subtract top two items on stack (2, 3, -1) Divide top two items on stack (2, -3) Add top two items on stack (-1)And -1 is indeed the correct result. P.S. If this seems interesting to you, there are actually programming languages which use RPN for their entire syntax, with functions called like 1 2 3 FUNC and so on. Forth is a very notable example.

Replying to:niconii

Well, this is a nice calculator, but... this isn't RPN (aka postfix), it's infix. Maybe you saw someone on Miiverse mention the shunting-yard algorithm, but that is for infix, not RPN.
RPN is a different way of writing math, it's not the usual syntax we normally use.

Infix: 1 * 2 + 3 / (4 - 5) RPN: 1 2 * 3 4 5 - / + PN: + * 1 2 / 3 - 4 5Note that RPN and PN don't require parentheses at all, nor an order of operations. In addition, RPN in particular is very simple to parse and evaluate. You can simply read it left to right, using a stack:

Push 1 onto stack (1) Push 2 onto stack (1, 2) Multiply top two items on stack (2) Push 3 onto stack (2, 3) Push 4 onto stack (2, 3, 4) Push 5 onto stack (2, 3, 4, 5) Subtract top two items on stack (2, 3, -1) Divide top two items on stack (2, -3) Add top two items on stack (-1)And -1 is indeed the correct result. P.S. If this seems interesting to you, there are actually programming languages which use RPN for their entire syntax, with functions called like 1 2 3 FUNC and so on. Forth is a very notable example.

I know what RPN is. This calculator converts infix to RPN before it solves it. Also, what is a decent way of checking to see if the equation can be converted to RPN? As in-

"1+/1" to "1,1,+,/" 'doesn't work "1+-1" to "1+(-1)" to "1,-1,+" 'works "(5)(7)" to "(5)*(7)" to "5,7,*" 'works "-(10+2)" to "-1*(10+2)" to "-1,10,2,+,*" 'works 'etc.

Replying to:niconii

Well, this is a nice calculator, but... this isn't RPN (aka postfix), it's infix. Maybe you saw someone on Miiverse mention the shunting-yard algorithm, but that is for infix, not RPN.
RPN is a different way of writing math, it's not the usual syntax we normally use.

Infix: 1 * 2 + 3 / (4 - 5) RPN: 1 2 * 3 4 5 - / + PN: + * 1 2 / 3 - 4 5Note that RPN and PN don't require parentheses at all, nor an order of operations. In addition, RPN in particular is very simple to parse and evaluate. You can simply read it left to right, using a stack:

Push 1 onto stack (1) Push 2 onto stack (1, 2) Multiply top two items on stack (2) Push 3 onto stack (2, 3) Push 4 onto stack (2, 3, 4) Push 5 onto stack (2, 3, 4, 5) Subtract top two items on stack (2, 3, -1) Divide top two items on stack (2, -3) Add top two items on stack (-1)And -1 is indeed the correct result. P.S. If this seems interesting to you, there are actually programming languages which use RPN for their entire syntax, with functions called like 1 2 3 FUNC and so on. Forth is a very notable example.

Well, "RPN calculator" tends to specifically mean "a calculator that you type RPN into". Several calculators exist which use only RPN, the calculator in macOS has an RPN mode, and so on. It's kind of a misleading name if you can't actually type RPN into it.
If you're asking how to check if RPN is valid after you already have "1,1,+,/", simply start with a counter at 0, and for each item, if it's a number, add 1, and if it's an operator, subtract 1. This counter represents the number of items on the stack.
If the counter is less than 1, you have too many operators. If it's greater than 1, you have too many numbers.

Replying to:niconiiWell, this is a nice calculator, but... this isn't RPN (aka postfix), it's infix. Maybe you saw someone on Miiverse mention the shunting-yard algorithm, but that is for infix, not RPN.
RPN is a different way of writing math, it's not the usual syntax we normally use.

Infix: 1 * 2 + 3 / (4 - 5) RPN: 1 2 * 3 4 5 - / + PN: + * 1 2 / 3 - 4 5Note that RPN and PN don't require parentheses at all, nor an order of operations. In addition, RPN in particular is very simple to parse and evaluate. You can simply read it left to right, using a stack:

Ah, I see. Thanks!

Replying to:12Me21

It would probably be a good idea not to parse the - as part of numbers, since it's actually an operator (for example, -1^2 is actually -(1^2)).
You'll have to distinguish between negation and subtraction though (maybe use ~ for negation internally)

Yeah... The system isn't perfect. I'll try to fix it eventually.