21 posts
Posted 23 February 2013 - 04:51 PM
So I want to make my own calculator program. I don't to use someone elses because this is the idea I've had for a program in such a long time. But anyway I'm using this code:
local args = {...}
local first = tonumber (args[1]) or 1
local second = tonumber (args[2]) or 1
I want to know if I can add a code in between line 2 and 3 that says:
local sign = tonumber (args[3]) or (something)
And then
print (first (how to run the sign so I can choose whatever sign I want) second)
So basically it takes first and second, and does whatever to them that the sign thing says. If there's not really a way to make this way work please tell me a way that will.
671 posts
Location
That place over there. Y'know. The one where I am.
Posted 23 February 2013 - 05:03 PM
local args = {...}
local first = tonumber(args[1]) or 1
local sign = args[2] or "+"
local second = tonumber(args[3]) or 1
if sign == "+" then
print(first+second)
elseif sign == "-" then
print(first-second)
elseif sign == "*" or sign == "x" then
print(first*second)
elseif sign == "/" then
print(first/second)
end
Then type
<ProgramName> <number> <+,-,x,*,or /> <number>
to execute. If you don't know how this code works, look on the wiki and the Lua Reference Manual for help.
21 posts
Posted 23 February 2013 - 05:07 PM
local args = {...}
local first = tonumber(args[1]) or 1
local sign = args[2] or "+"
local second = tonumber(args[3]) or 1
if sign == "+" then
print(first+second)
elseif sign == "-" then
print(first-second)
elseif sign == "*" or sign == "x" then
print(first*second)
elseif sign == "/" then
print(first/second)
end
Then type
<ProgramName> <number> <+,-,x,*,or /> <number>
to execute. If you don't know how this code works, look on the wiki and the Lua Reference Manual for help.
Thanks!!!! And I'm really bad at programming so it'll take me a while before I can actually write a decent program without having to ask someone for help with it. But yeah thanks!!!!
21 posts
Posted 23 February 2013 - 05:48 PM
local args = {...}
local first = tonumber(args[1]) or 1
local sign = args[2] or "+"
local second = tonumber(args[3]) or 1
if sign == "+" then
print(first+second)
elseif sign == "-" then
print(first-second)
elseif sign == "*" or sign == "x" then
print(first*second)
elseif sign == "/" then
print(first/second)
end
Then type
<ProgramName> <number> <+,-,x,*,or /> <number>
to execute. If you don't know how this code works, look on the wiki and the Lua Reference Manual for help.
And also, I want to make some way so if first, sign, and second are just left blank it would do something like print
Usage: <programname> <first#> <operation> <second#>
212 posts
Location
Leeds, England
Posted 23 February 2013 - 06:42 PM
And also, I want to make some way so if first, sign, and second are just left blank it would do something like print
Usage: <programname> <first#> <operation> <second#>
if args < 3 then
error("Usage: <programname> <first#> <operation> <second#>")
end
671 posts
Location
That place over there. Y'know. The one where I am.
Posted 23 February 2013 - 06:54 PM
if #args < 3 then
error("Usage: <programname> <first#> <operation> <second#>")
end
Fixed :P/>