Lua 编程方面的帮助,需要使用 3 个运算符(加、减和乘)构建基本计算器

我们有以下任务

使用 Lua 创建三个函数,可以执行加法、减法和乘法

程序应该:

接受任意数量的数字。 检查运算符 + - *。如果不是,则输出“无效的操作数”并退出。 接受两个操作数。如果任何操作数不是数字,则输出“无效的操作数”并退出。使用 - tonumber() 进行操作并打印结果。

我们编写了以下代码,但不能按预期工作,不确定我们在哪里出错了,请指引

local operators = {
    ["+"] = function(x,y) return x+y end,
    ["-"] = function(x,y) return x-y end,
    ["*"] = function(x,y) return x*y end
}

local function default()
    print "Invalid operand"
end

local num1 = io.read()
local num2 = io.read()
local operator = io.read()

local func (
 if operator == "+" then
   local add = tonumber(num1) + tonumber(num2)
 end
 if operator == "-" then
   local subtract = tonumber(num1) - tonumber(num2)
 end
 if operator == "*" then
   local multiply = tonumber(num1) * tonumber(num2)
 end
)
or default
print(func(num1,num2))
io.read()

正确的代码是

local operators = {
  ["+"] = function (x, y) return x + y end,
  ["-"] = function (x, y) return x - y end,
  ["*"] = function (x, y) return x * y end,
}

local operator = operators[io.read()]
local num1 = tonumber(io.read())
local num2 = tonumber(io.read())

if num1 and num2 then
  if operator then
    print(operator(num1, num2))
  else
    print("Invalid operator")
  end
else
  print("Invalid operand")
end
点赞
用户2858170
用户2858170

你的代码存在语法错误,意义不是很明确。

local func (
 if operator == "+" then
   local add = tonumber(num1) + tonumber(num2)
 end
 if operator == "-" then
   local subtract = tonumber(num1) - tonumber(num2)
 end
 if operator == "*" then
   local multiply = tonumber(num1) * tonumber(num2)
 end
)
or default

为定义本地函数,你可以这样写:

local myFunction = function() end

或者

local function myFunction() end

但不是这样写:

local func()

因为你定义函数时它永远不能是 nil。所以像这样简短定义一个函数

local default = function() end
local myFunction = function() end or default

就没意义。

您应该添加说明,让用户知道在调用 io.read() 之前应输入什么。

你代码中这部分没有用到:

local operators = {
    ["+"] = function(x,y) return x+y end,
    ["-"] = function(x,y) return x-y end,
    ["*"] = function(x,y) return x*y end
}

还有,你的 func 函数仅仅创建了一些未使用的本地变量。

该函数没有返回值,因此像 print(func(num1, num2)) 这样打印返回值的命令将无法打印预期的结果。

此外,您没有检查用户是否输入了有效的字符,如数字或运算符。这将导致 Lua 错误,如果用户输入的是其他字符。

2020-08-13 09:24:40
用户6879826
用户6879826

利用operators表来进行计算。不要将operator设置为用户输入的字符串,而是直接将其设置为用户需要的函数。如果用户输入的运算符不在表中,operator将被设置为nil

local operator = operators[io.read()]

同样,将num1num2直接设置为将要操作的数字,而不是将它们设置为用户输入的字符串:

local num1 = tonumber(io.read())
local num2 = tonumber(io.read())

如果tonumber无法将用户输入转换为数字,则num1num2将被设置为nil

您可以利用输入无效给出nil值的事实来验证输入。您不需要default()函数仅仅报告输入有误,因此我在下面的最终代码中没有使用它。如果您确实想要删除此报告到函数,则可能至少应区分错误的操作数和错误的运算符:

local function bad_operand()
  print("Invalid operand")
end

local function bad_operator()
  print("Invalid operator")
end

现在,您只需要检查参数是否有效,以及运算符是否有效,如果有效,则执行计算。以下是代码:

local operators = {
  ["+"] = function (x, y) return x + y end,
  ["-"] = function (x, y) return x - y end,
  ["*"] = function (x, y) return x * y end,
}

local num1 = tonumber(io.read())
local num2 = tonumber(io.read())
local operator = operators[io.read()]

if num1 and num2 then
  if operator then
    print(operator(num1, num2))
  else
    print("Invalid operator")
  end
else
  print("Invalid operand")
end

以下是一些示例运行:

~/code/lua/scratch $ lua calc.lua
3
4
-
-1
~/code/lua/scratch $ lua calc.lua
7
5
*
35
~/code/lua/scratch $ lua calc.lua
3
/
/
Invalid operand
~/code/lua/scratch $ lua calc.lua
3
4
/
Invalid operator

使用operators表的好处是,我们现在可以将运算符添加到表中,以扩展程序的功能,而无需进行任何其他更改:

local operators = {
  ["+"] = function (x, y) return x + y end,
  ["-"] = function (x, y) return x - y end,
  ["*"] = function (x, y) return x * y end,
  ["/"] = function (x, y) return x / y end,
  ["//"] = function (x, y) return x // y end,
}

测试新运算符:

~/code/lua/scratch $ lua calc.lua
3
4
/
0.75
~/code/lua/scratch $ lua calc.lua
7
3
//
2
2020-08-13 13:03:03