I’m working on a program that solves math problems using the four randomly generated numbers AND randomly generated math operators (x, /, +, -). The answer needs to be a calculated number, but so far the answer is an exact copy of the original math problem. For instance, if the math problem is: “10+5/8x2” the answer should be: “11.25” rather than: “10+5/8x2”. I can understand why it’s doing what it is doing, I just need it to insert the proper math operator in place of the text version. That’s the sticking point.
The intent of this program is to drill an elementary-school-aged nephew in basic math concepts by presenting a math problem to solve then displaying the answer once complete. This is a stripped-down version for simplicity.
Any advice, of course, is much appreciated. Thanks.
-- OrderOfOperation
supportedOrientations(LANDSCAPE_ANY)
displayMode(STANDARD)
function setup()
print("OrderOfOperation:\
Solves a simple math problem while taking Order-Of-Operation rules into account.\
\
Touch screen for next problem.")
-- Randomly generate four numbers and three operations: multiply, divide, add and subtract
opTypeTbl = {" x "," / "," + "," - "}
first = math.random(1,10)
op1 = opTypeTbl[math.random(1,4)]
second = math.random(1,10)
op2 = opTypeTbl[math.random(1,4)]
third = math.random(1,10)
op3 = opTypeTbl[math.random(1,4)]
fourth = math.random(1,10)
end
function draw()
background(40, 40, 50)
Lab(first,op1,second,op2,third,op3,fourth)
end
function Lab(first,op1,second,op2,third,op3,fourth)
-- Display Problem and Answer given four random numbers and three random operators.
local first = first
local op1 = op1
local second = second
local op2 = op2
local third = third
local op3 = op3
local fourth = fourth
fill(255, 255, 255, 255)
fontSize(40)
local p = "Problem: "..first..op1..second..op2..third..op3..fourth
text(p, WIDTH/2,HEIGHT/2+100) -- Example problem: "10 + 5 / 8 x 2"
local a = "Answer: "..first..op1..second..op2..third..op3..fourth
-- Answer: 11.25
text(a,WIDTH/2,HEIGHT/2-100)
end
function touched(t)
-- Calls next problem:
if t.state == ENDED then
restart()
end
end