Jump to content




How would you make a calculator with multiple numbers


8 replies to this topic

#1 WolfDood

  • Members
  • 20 posts
  • Locationjava.exception:3753:location not found

Posted 11 November 2016 - 08:34 PM

You could probably use loops and I'm probably doind it wrong can some one help me it goes like this:
args = {}
write("Calc > ")
ie = read()
for n in ie:gmatch("%w+") do
table.insert(args, n)
end
noa = #args
if args[1] == "add" then
i = 2
while i < noa do
  res = args[i] + args[i + 1]
  i = i + 1
end
conres = res + args[noa]
print(res)
elseif args[1] == "subtract" then
-- Work on it
elseif args[1] == "divide" then
-- Work on it
elseif args[1] == "multiply" then
-- Work on it
else
error("Incorrect Syntax: usage: <function> <no..>")
end

Thank you.

#2 The_Cat

  • Members
  • 119 posts

Posted 11 November 2016 - 09:55 PM

In the while loop you are adding args[i] to args[i+1] try adding just the args[i] to res:
res = res + args[i].

#3 WolfDood

  • Members
  • 20 posts
  • Locationjava.exception:3753:location not found

Posted 12 November 2016 - 09:16 PM

View PostThe_Cat, on 11 November 2016 - 09:55 PM, said:

In the while loop you are adding args[i] to args[i+1] try adding just the args[i] to res:
res = res + args[i].

Will try that thanks :)

#4 Twijn

  • Members
  • 119 posts

Posted 12 November 2016 - 10:22 PM

View PostWolfDood, on 11 November 2016 - 08:34 PM, said:

You could probably use loops and I'm probably doind it wrong can some one help me it goes like this:
args = {}
write("Calc > ")
ie = read()
for n in ie:gmatch("%w+") do
table.insert(args, n)
end
noa = #args
if args[1] == "add" then
i = 2
while i < noa do
  res = args[i] + args[i + 1]
  i = i + 1
end
conres = res + args[noa]
print(res)
elseif args[1] == "subtract" then
-- Work on it
elseif args[1] == "divide" then
-- Work on it
elseif args[1] == "multiply" then
-- Work on it
else
error("Incorrect Syntax: usage: <function> <no..>")
end

Thank you.

It's likely seeing the argument as a string instead of a number, in that case you would need to use tonumber(args[i]).

Additionally, you should change
args = {}
to
args = {...}
The first will output an empty table, the second will actually get the arguments passed on to the program.

View PostThe_Cat, on 11 November 2016 - 09:55 PM, said:

In the while loop you are adding args[i] to args[i+1] try adding just the args[i] to res:
res = res + args[i].
Don't forget to define "res" to 0. If res is not definied, it'll result to nil and nil + any number doesn't work.

#5 WolfDood

  • Members
  • 20 posts
  • Locationjava.exception:3753:location not found

Posted 12 November 2016 - 10:45 PM

View PostTwijn, on 12 November 2016 - 10:22 PM, said:

View PostWolfDood, on 11 November 2016 - 08:34 PM, said:

You could probably use loops and I'm probably doind it wrong can some one help me it goes like this:
args = {}
write("Calc > ")
ie = read()
for n in ie:gmatch("%w+") do
table.insert(args, n)
end
noa = #args
if args[1] == "add" then
i = 2
while i < noa do
  res = args[i] + args[i + 1]
  i = i + 1
end
conres = res + args[noa]
print(res)
elseif args[1] == "subtract" then
-- Work on it
elseif args[1] == "divide" then
-- Work on it
elseif args[1] == "multiply" then
-- Work on it
else
error("Incorrect Syntax: usage: <function> <no..>")
end

Thank you.

It's likely seeing the argument as a string instead of a number, in that case you would need to use tonumber(args[i]).

Additionally, you should change
args = {}
to
args = {...}
The first will output an empty table, the second will actually get the arguments passed on to the program.

View PostThe_Cat, on 11 November 2016 - 09:55 PM, said:

In the while loop you are adding args[i] to args[i+1] try adding just the args[i] to res:
res = res + args[i].
Don't forget to define "res" to 0. If res is not definied, it'll result to nil and nil + any number doesn't work.

if you put {...} wouldn't that read all the input after the program name when you run it? I'm trying to make a calc where it get multiple input inside the program. anyway thanks

#6 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 13 November 2016 - 12:27 AM

I would use lua for calculate a line, because i am lazy ^^:
local res, err = load('return ' .. read(), 'Calculator', 't', {})
if not res then printError(err) end
local ok, res = pcall(res)
if not ok then printError(res) end

Otherwise i would use gmatch.

Also, i woudl give a second arhument for error: 2. So if the calculator errors, the user knows in which line he has done a mistake.

Edited by Sewbacca, 13 November 2016 - 12:30 AM.


#7 Twijn

  • Members
  • 119 posts

Posted 13 November 2016 - 04:01 AM

View PostWolfDood, on 12 November 2016 - 10:45 PM, said:

View PostTwijn, on 12 November 2016 - 10:22 PM, said:

View PostWolfDood, on 11 November 2016 - 08:34 PM, said:

You could probably use loops and I'm probably doind it wrong can some one help me it goes like this:
args = {}
write("Calc > ")
ie = read()
for n in ie:gmatch("%w+") do
table.insert(args, n)
end
noa = #args
if args[1] == "add" then
i = 2
while i < noa do
  res = args[i] + args[i + 1]
  i = i + 1
end
conres = res + args[noa]
print(res)
elseif args[1] == "subtract" then
-- Work on it
elseif args[1] == "divide" then
-- Work on it
elseif args[1] == "multiply" then
-- Work on it
else
error("Incorrect Syntax: usage: <function> <no..>")
end

Thank you.

It's likely seeing the argument as a string instead of a number, in that case you would need to use tonumber(args[i]).

Additionally, you should change
args = {}
to
args = {...}
The first will output an empty table, the second will actually get the arguments passed on to the program.

View PostThe_Cat, on 11 November 2016 - 09:55 PM, said:

In the while loop you are adding args[i] to args[i+1] try adding just the args[i] to res:
res = res + args[i].
Don't forget to define "res" to 0. If res is not definied, it'll result to nil and nil + any number doesn't work.

if you put {...} wouldn't that read all the input after the program name when you run it? I'm trying to make a calc where it get multiple input inside the program. anyway thanks

Sorry, I didn't realize that you were trying to do that. xP That is correct then, however it should have tonumber() still.

#8 Exerro

  • Members
  • 801 posts

Posted 13 November 2016 - 08:58 AM

Using tonumber isn't entirely necessary in this case. Lua treats valid number strings as numbers with arithmetic operators:
print( "5" + "2" )
print( type( "5" + "2" ) )
That will print 7 and number:
Spoiler

It's probably better practise to convert it explicitly, but if you were wondering why it wasn't breaking, that's why.

#9 jakejakey

  • Members
  • 98 posts

Posted 13 November 2016 - 01:56 PM

View PostWolfDood, on 11 November 2016 - 08:34 PM, said:

You could probably use loops and I'm probably doind it wrong can some one help me it goes like this:
-snip-
You could try using loadstring, here is a working example calculator
print("Welcome to Calculator")
write("Input: ")
local input = read()
local env = {
  ["math"] = math,
}
local func = loadstring("return "..input)
setfenv(func,env)
write("Output: ")
print(func())






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users