Jump to content




Calculator


  • You cannot reply to this topic
5 replies to this topic

#1 UP844

  • Members
  • 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.

#2 Shnupbups

  • Members
  • 596 posts
  • LocationThat 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.

#3 UP844

  • Members
  • 21 posts

Posted 23 February 2013 - 05:07 PM

View PostShnupbups100, on 23 February 2013 - 05:03 PM, said:

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!!!!

#4 UP844

  • Members
  • 21 posts

Posted 23 February 2013 - 05:48 PM

View PostShnupbups100, on 23 February 2013 - 05:03 PM, said:

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#>


#5 NDFJay

  • Members
  • 216 posts
  • LocationLeeds, England

Posted 23 February 2013 - 06:42 PM

View PostUP844, on 23 February 2013 - 05:48 PM, said:

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


#6 Shnupbups

  • Members
  • 596 posts
  • LocationThat place over there. Y'know. The one where I am.

Posted 23 February 2013 - 06:54 PM

View PostNDFJay, on 23 February 2013 - 06:42 PM, said:

if #args < 3 then
error("Usage: <programname> <first#> <operation> <second#>")
end
Fixed :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users