将数字作为单词更改为数字值

时间:2017-10-03 00:21:13

标签: lua

我正在尝试将一个字符串更改为数字值,即1到50之间的数字,即if local =“十二”它会使local_num = 12,如果local =“one”则local_num = 1,等等。我只是使用数组并循环比较吗?

2 个答案:

答案 0 :(得分:0)

不需要循环。只需直接使用此表:

Word2Number={
    ["zero"]=0,
    ["one"]=1,
    ["two"]=2,
    ["three"]=3,
    ["four"]=4,
    ["five"]=5,
    ["six"]=6,
    ["seven"]=7,
    ["eight"]=8,
    ["nine"]=9,
    ["ten"]=10,
    ["eleven"]=11,
    ["twelve"]=12,
    ["thirteen"]=13,
    ["fourteen"]=14,
    ["fifteen"]=15,
    ["sixteen"]=16,
    ["seventeen"]=17,
    ["eighteen"]=18,
    ["nineteen"]=19,
    ["twenty"]=20,
    ["twenty-one"]=21,
    ["twenty-two"]=22,
    ["twenty-three"]=23,
    ["twenty-four"]=24,
    ["twenty-five"]=25,
    ["twenty-six"]=26,
    ["twenty-seven"]=27,
    ["twenty-eight"]=28,
    ["twenty-nine"]=29,
    ["thirty"]=30,
    ["thirty-one"]=31,
    ["thirty-two" ]=32,
    ["thirty-three" ]=33,
    ["thirty-four"]=34,
    ["thirty-five"]=35,
    ["thirty-six"]=36,
    ["thirty-seven"]=37,
    ["thirty-eight"]=38,
    ["thirty-nine"]=39,
    ["forty"]=40,
    ["forty-one"]=41,
    ["forty-two"]=42,
    ["forty-three"]=43,
    ["forty-four"]=44,
    ["forty-five"]=45,
    ["forty-six"]=46,
    ["forty-seven"]=47,
    ["forty-eight"]=48,
    ["forty-nine"]=49,
    ["fifty"]=50,
}

答案 1 :(得分:0)

这是拼写整数的代码

local append, concat, floor, abs = table.insert, table.concat, math.floor, math.abs
local num = {'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
   'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'}
local tens = {'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'}
local bases = {{floor(1e18), ' quintillion'}, {floor(1e15), ' quadrillion'}, {floor(1e12), ' trillion'},
   {floor(1e9), ' billion'}, {1000000, ' million'}, {1000, ' thousand'}, {100, ' hundred'}}

local insert_word_AND = false  -- 101 = "one hundred and one" / "one hundred one"

local function IntegerNumberInWords(n)
   -- Returns a string (spelling of integer number n)
   -- n should be from -2^53 to 2^53  (-2^63 < n < 2^63 for integer argument in Lua 5.3)
   local str = {}
   if n < 0 then
      append(str, "minus")
   end
   n = floor(abs(n))
   if n == 0 then
      return "zero"
   end
   if n >= 1e21 then
      append(str, "infinity")
   else
      local AND
      for _, base in ipairs(bases) do
         local value = base[1]
         if n >= value then
            append(str, IntegerNumberInWords(n / value)..base[2])
            n, AND = n % value, insert_word_AND or nil
         end
      end
      if n > 0 then
         append(str, AND and "and")   -- a nice pun !
         append(str, num[n] or tens[floor(n/10)-1]..(n%10 ~= 0 and '-'..num[n%10] or ''))
      end
   end
   return concat(str, ' ')
end

这是如何使用它:

local function digitize(number_as_text)
   number_as_text = number_as_text:lower():gsub("%W", "")
   for i = 1, 50 do
      if IntegerNumberInWords(i):lower():gsub("%W", "") == number_as_text then
         return i
      end
   end
end

print(digitize(io.read()))