? Basic Calculator (moved) ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Basic Calculator (moved)

Submitted
Updated
Author
Nathaniel
Public Key
(KJDQQ3A4)
Min. Compat.
All
Version
1.0
Rating
2 votes
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 Head Admin Second Year My account is over 2 years old Website 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 Nathaniel Final(?) Contest Winner I won the (possibly) final SmileBASIC Source Contest! October 2018 Programming Contest Scholar Received for knowing a great deal about programming topics Achievements Helper Received for being very helpful around SmileBASIC Source 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 Final(?) Contest Winner I won the (possibly) final SmileBASIC Source Contest! October 2018 Programming Contest Scholar Received for knowing a great deal about programming topics Achievements Helper Received for being very helpful around SmileBASIC Source 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 Final(?) Contest Winner I won the (possibly) final SmileBASIC Source Contest! October 2018 Programming Contest Scholar Received for knowing a great deal about programming topics Achievements Helper Received for being very helpful around SmileBASIC Source Achievements Ah, I see. Thanks!
Nathaniel Nathaniel Final(?) Contest Winner I won the (possibly) final SmileBASIC Source Contest! October 2018 Programming Contest Scholar Received for knowing a great deal about programming topics Achievements Helper Received for being very helpful around SmileBASIC Source Achievements definitely didn't add any easter eggs...