Discuss Scratch

Morgan14A
Scratcher
10 posts

Calculator help for an equation solver !

Hey guys ! I'm trying to make a Graph plotter, but I'm blocking on the the equation writer. Explantions:
(the link to my project, there's another link below: http://scratch.mit.edu/projects/23457750/ )

At the beginning, I make two lists: the “readableEquation” and “result” list. These lists works like that: I take the answer (It ask before “Y=” and you had to write the equation) and I put every numbers separated in the “readableEquation” list with the “letter” variable , like that:

set [letter v] to [0]
repeat (length of (answer))
add (letter (letter) of (answer)) to [readableEquation v]
change [letter v] by (1)
end

Then I've the equation ready to be calculated by the program. So I make a “operation” variable and this script to calculate the final result, the Y position of plotter, the sprite that draw the graph step by step (the “X” variable is the x psition of the plotter, that draw the graph) :

set [letter v] to [0]
set [operation v] to [0]
repeat (length of [readableEquation v])
if <(item (letter) of [readableEquation v]) = [+]> then
set [operation v] to [+]
else
if <(item (letter) of [readableEquation v]) = [-]> then
set [operation v] to [-]
else
if <(item (letter) of [readableEquation v]) = [*]> then
set [operation v] to [*]
else
if <(item (letter) of [readableEquation v]) = [/]> then
set [operation v] to [/]
else
if <(item (letter) of [readableEquation v]) = [x]> then
if <(oparetion) = [0]> then
add (X) to [result v]
else
if <(operation) = [+]> then
add ((item (1 v) of [result v]) + (X)) to [result v]
delete (1 v) of [result v]
else
if <(operation) = [-]> then
add ((item (1 v) of [result v]) - (X)) to [result v]
delete (1 v) of [result v]
else
if <(operation) = [*]> then
add ((item (1 v) of [result v]) * (X)) to [result v]
delete (1 v) of [result v]
else
if <(operation) = [/]> then
add ((item (1 v) of [result v]) / (X)) to [result v]
delete (1 v) of [result v]
else
if <(oparetion) = [0]> then
add (item (letter) of [readableEquation v]) to [result v]
else
if <(operation) = [+]> then
add ((item (1 v) of [result v]) + ((item (letter) of [readableEquation v]))) to [result v]
delete (1 v) of [result v]
else
if <(operation) = [-]> then
add ((item (1 v) of [result v]) - ((item (letter) of [readableEquation v]))) to [result v]
delete (1 v) of [result v]
else
if <(operation) = [*]> then
add ((item (1 v) of [result v]) * ((item (letter) of [readableEquation v]))) to [result v]
delete (1 v) of [result v]
else
if <(operation) = [/]> then
add ((item (1 v) of [result v]) / ((item (letter) of [readableEquation v]))) to [result v]
delete (1 v) of [result v]
end
end
end

Then I have the final result (the Y position of the plotter) but it doesn't respect the operation priority (I don't know how it's called in english, but it's the rule that say that we have to calculate at first the parenthesis, next the exponents, then the multiplication and last the addition) and I don't know how to do that simply I tried to make something with the parenthesis, but it didn't work very well (you can check it here => http://scratch.mit.edu/projects/23457750/)

I added a new feature with the side of the screen and the steps, if you want to run the project, please first read the description.

If anyone can help me, please reply !

if I made mistakes by writing, tell me I try to speak english well

Morgan14A
Scratcher
10 posts

Calculator help for an equation solver !

…but I'm blocking on the the equation writer. Explantions: …

Explanations of course ^.^

Powered by DjangoBB