Discuss Scratch

--Explosion--
Scratcher
1000+ posts

How Can I Make a Calculator

How can I make a calculator that does order of operations and allows input of long strings of math for example

I know how to do this
2+8=10
4*5=20
How do I make something that can do this
3+5-5=3
(10/5)-(5*67)
deck26
Scratcher
1000+ posts

How Can I Make a Calculator

Or if you prefer to actually receive help …….

This is all about parsing the input and working things out using the usual priorities. I'd start by taking the input and extracting separate items to a list, including brackets and operators. You can then, for example, replace a set of 3 items ‘value’, ‘*’, ‘value’ with a single value and work out what to do next.

If you have nested brackets you can work out the contents of the inner brackets in the same way.

There are lots of examples done by others if you search.
--Explosion--
Scratcher
1000+ posts

How Can I Make a Calculator

When I searched, I only found a to of graphic stuff, I font's care about the graphics just the calculating capabilities, Is there a way that I could have a sprite ask for a problem such as 3+8*345/(7-8) and then have the program calculate it and have the sprite say the answer?
deck26
Scratcher
1000+ posts

How Can I Make a Calculator

staffordboys wrote:

When I searched, I only found a to of graphic stuff, I font's care about the graphics just the calculating capabilities, Is there a way that I could have a sprite ask for a problem such as 3+8*345/(7-8) and then have the program calculate it and have the sprite say the answer?
Yes, in the way I describe.

Did you search in Scratch because I found plenty?

https://scratch.mit.edu/projects/429002/ is a basic one, https://scratch.mit.edu/projects/19901239/ includes brackets but is far more complex than you probably want.

using your example think about how you need to handle that. Split it into components
3
+
8
*
345
etc

Multiplcation has the highest priority in what I've shown since I didn't get as far as the bracket. So you can multiply 8 by 345 and your list is then
3
+
2760

which you can then work out. Really all you're doing is spliiting the input up and working out the precedence of the operators to decide which step you do next and repeating until you only have one item.

--Explosion--
Scratcher
1000+ posts

How Can I Make a Calculator

Thanks you that helps a lot, I am working on it right now
-Rex-
Scratcher
500+ posts

How Can I Make a Calculator

First you want to split the input into tokens (as @deck26 said). Then, as an optional step, insert inplicit operators into the list of tokens (so that 2(2) becomes 2*(2)). Then, use the shunting-yard algorithm to convert the list of tokens into RPN: https://en.wikipedia.org/wiki/Shunting-yard_algorithm (note: it's faster to add items to the end of a list and take the value of the last item instead of inserting items at the beginning of a list and taking the value of the first item). Then, use a stack machine to evaluate the RPN (note that RPN does not use parentheses).

This is how a stack machine would evaluate RPN:

Input: 3 * (2+5)
RPN: 2 5 + 3 *

Parenthesis denote list with commas separating the items.
0. Expression: (2, 5, +, 3, *) Stack: ()
1. Expression: (2, 5, +, 3, *) Stack: (2) Add 2 to the end of the stack
2. Expression: (2, 5, +, 3, *) Stack: (2, 5) Add 5 to the end of the stack
3. Expression: (2, 5, +, 3, *) Stack: (7) Add and remove the last two items on the stack and add the sum to the end
4. Expression: (2, 5, +, 3, *) Stack: (7, 3) Add 3 to the end of the stack
5. Expression: (2, 5, +, 3, *) Stack: (21) Multiply and remove the last two items on the stack and add the product to the end
Result: 21

Last edited by -Rex- (April 1, 2019 21:08:31)

--Explosion--
Scratcher
1000+ posts

How Can I Make a Calculator

I get that but how could you split numbers higher than 9? If I add every character to a list and then calculate it it works fine if only use single digit numbers but if I use numbers with multiple digits It splits the number into every digit such as if I tried:
34*2 it would come out as 8 because 4*2 is 8 and 4 is the last digit of 34, I know how to split items into a list every certain chacter but how do I splita string such as 34-5*45+3 into a list with this
34
-
5
*
45
+
3
deck26
Scratcher
1000+ posts

How Can I Make a Calculator

staffordboys wrote:

I get that but how could you split numbers higher than 9? If I add every character to a list and then calculate it it works fine if only use single digit numbers but if I use numbers with multiple digits It splits the number into every digit such as if I tried:
34*2 it would come out as 8 because 4*2 is 8 and 4 is the last digit of 34, I know how to split items into a list every certain chacter but how do I splita string such as 34-5*45+3 into a list with this
34
-
5
*
45
+
3
Think how you'd do it yourself - you'd identify a digit and check if the next character is a digit and so on.
--Explosion--
Scratcher
1000+ posts

How Can I Make a Calculator

Oh, thanks, I seem so dumb now
mffvd
Scratcher
3 posts

How Can I Make a Calculator

and how to use hooks?

Powered by DjangoBB