? Basic Calculator ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Basic Calculator

Submitted
Updated
Author
Nathaniel
Public Key
QRWE1EAD
Min. Compat.
All
Version
1.0
Rating
1 vote
Description 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. Instructions Use the d-pad to move the cursor. Use the touch screen to type stuff.
Categories
Keywords
7 Comment(s) 12Me21 12Me21 Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website 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 Nathaniel Spring 2017 Contest Winner I won the SmileBASIC Source Spring 2017 Contest! Programming Contest Helper Received for being very helpful around SmileBASIC Source Achievements Staff Pick "Your program is one of our favorites!" - Staff Achievements Yeah... The system isn't perfect. I'll try to fix it eventually. niconii 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:
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.
Nathaniel Nathaniel Spring 2017 Contest Winner I won the SmileBASIC Source Spring 2017 Contest! Programming Contest Helper Received for being very helpful around SmileBASIC Source Achievements Staff Pick "Your program is one of our favorites!" - Staff Achievements 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 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 Nathaniel Spring 2017 Contest Winner I won the SmileBASIC Source Spring 2017 Contest! Programming Contest Helper Received for being very helpful around SmileBASIC Source Achievements Staff Pick "Your program is one of our favorites!" - Staff Achievements Ah, I see. Thanks!
Nathaniel Nathaniel Spring 2017 Contest Winner I won the SmileBASIC Source Spring 2017 Contest! Programming Contest Helper Received for being very helpful around SmileBASIC Source Achievements Staff Pick "Your program is one of our favorites!" - Staff Achievements definitely didn't add any easter eggs...