Basic Calculator (moved) ● SmileBASIC Source

Register

# Basic Calculator (moved)

Submitted
Updated
Author
Nathaniel
Public Key
REMOVED
Min. Compat.
All
Version
1.0
Rating
7 Comment(s) 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself 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) Nathaniel OSP Contest 1 Contest Participant I participated in the first SmileBASIC Source OSP Contest! OSP Contest 2 Contest Participant I participated in the second SmileBASIC Source OSP Contest! OSP Contest 3 Contest Participant I participated in the third SmileBASIC Source OSP Contest! Yeah... The system isn't perfect. I'll try to fix it eventually. niconii Video Games I like to play video games! Hobbies Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Drawing I like to draw! Hobbies 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 5``` Note 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:
Nathaniel OSP Contest 1 Contest Participant I participated in the first SmileBASIC Source OSP Contest! OSP Contest 2 Contest Participant I participated in the second SmileBASIC Source OSP Contest! OSP Contest 3 Contest Participant I participated in the third SmileBASIC Source OSP Contest! 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.``` niconii Video Games I like to play video games! Hobbies Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Drawing I like to draw! Hobbies 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. Nathaniel OSP Contest 1 Contest Participant I participated in the first SmileBASIC Source OSP Contest! OSP Contest 2 Contest Participant I participated in the second SmileBASIC Source OSP Contest! OSP Contest 3 Contest Participant I participated in the third SmileBASIC Source OSP Contest! Ah, I see. Thanks!