Posted 20 February 2013 - 03:49 PM
Hello Pro's, i'm practicing lua programming and I decided to make a calculator (a text calculator) with an api created by me. This is the API:
And this is de main program:
The problem is that the program don't add or deduct or nothing. It gives the same result always: "12". The "12" is because I had the functions in the api inside the main program without an api, and I was trying out the root square with 144.
Thanks =)
--Definiciones de funcion calculadora
--Suma, resta, multiplicacion, division
--Potencia, raiz cuadrada
function add(x, y)
res = x + y
end
function sub(x, y)
res = x - y
end
function mult(x, y)
res = x * y
end
function div(x, y)
res = x / y
end
function pot(x, y)
res = x ^ y
end
function rsqr(x)
local r
local t = 0
r = x
while t ~= r do
t = r
r = (x/r + r)/2
end
res = r
end
And this is de main program:
os.loadAPI("calc")
local opc
print("Pick a number")
print("")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Square of a number")
print("6. Root Square")
opc = io.read()
if opc == "1" then
print("First number:")
x = io.read()
print("Second number:")
y = io.read()
calc.suma(x, y)
print("")
print("Result: "..res)
x = nil
y = nil
elseif opc == "2" then
print("First number:")
x = io.read()
print("Second number:")
y = io.read()
calc.resta(x, y)
print("")
print("Result: "..res)
x = nil
y = nil
elseif opc == "3" then
print("First number:")
x = io.read()
print("Second number:")
y = io.read()
calc.mult(x, y)
print("")
print("Result: "..res)
x = nil
y = nil
elseif opc == "4" then
print("First number:")
x = io.read()
print("Second number:")
y = io.read()
calc.div(x, y)
print("")
print("Result: "..res)
x = nil
y = nil
elseif opc == "5" then
print("First number (base):")
x = io.read()
print("Second number:")
y = io.read()
calc.pot(x, y)
print("")
print("Result: "..res)
x = nil
y = nil
elseif opc == "6" then
print("Number:")
x = io.read()
calc.sqr(x)
print("")
print("Result: "..res)
x = nil
end
The problem is that the program don't add or deduct or nothing. It gives the same result always: "12". The "12" is because I had the functions in the api inside the main program without an api, and I was trying out the root square with 144.
Thanks =)