所以仍在研究将数字转换为中文拼音的程序。完整代码(请原谅我的古怪,我真的很感谢所有对我糟糕编码的帮助)
local digitmap = {
["0"] = "ying2",
["1"] = "yi1",
["2"] = "er2",
["3"] = "san1",
["4"] = "si4",
["5"] = "wu3",
["6"] = "liu4",
["7"] = "qi1",
["8"] = "ba1",
["9"] = "jiu3",
}
print("Enter a number to be converted (up to 10 digits long):")
while true do
number = tonumber(io.read("*line"))
nlength = #(tostring(number))
if number ~= nil and nlength <= 10 then
break
end
print("Invalid input or too long. Please try again:")
end
if number == 0 then
fconvnumber = "ying2"
zero = true
end
local cwords = {}
for c in string.gmatch(number, "%d") do
cwords[#cwords + 1] = digitmap[c]
end
if nlength == 2 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
elseif nlength == 3 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
cwords[nlength - 2] = cwords[nlength - 2] .. " bai3 "
elseif nlength == 4 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
cwords[nlength - 2] = cwords[nlength - 2] .. " bai3 "
cwords[nlength - 3] = cwords[nlength - 3] .. " qian1 "
elseif nlength == 5 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
cwords[nlength - 2] = cwords[nlength - 2] .. " bai3 "
cwords[nlength - 3] = cwords[nlength - 3] .. " qian1 "
cwords[nlength - 4] = cwords[nlength - 4] .. " wan2 "
elseif nlength == 6 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
cwords[nlength - 2] = cwords[nlength - 2] .. " bai3 "
cwords[nlength - 3] = cwords[nlength - 3] .. " qian1 "
cwords[nlength - 4] = cwords[nlength - 4] .. " wan2 "
cwords[nlength - 5] = cwords[nlength - 5] .. " shi2 "
elseif nlength == 7 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
cwords[nlength - 2] = cwords[nlength - 2] .. " bai3 "
cwords[nlength - 3] = cwords[nlength - 3] .. " qian1 "
cwords[nlength - 4] = cwords[nlength - 4] .. " wan2 "
cwords[nlength - 5] = cwords[nlength - 5] .. " shi2 "
cwords[nlength - 6] = cwords[nlength - 6] .. " bai3 "
elseif nlength == 8 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
cwords[nlength - 2] = cwords[nlength - 2] .. " bai3 "
cwords[nlength - 3] = cwords[nlength - 3] .. " qian1 "
cwords[nlength - 4] = cwords[nlength - 4] .. " wan2 "
cwords[nlength - 5] = cwords[nlength - 5] .. " shi2 "
cwords[nlength - 6] = cwords[nlength - 6] .. " bai3 "
cwords[nlength - 7] = cwords[nlength - 7] .. " qian1 "
elseif nlength == 9 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
cwords[nlength - 2] = cwords[nlength - 2] .. " bai3 "
cwords[nlength - 3] = cwords[nlength - 3] .. " qian1 "
cwords[nlength - 4] = cwords[nlength - 4] .. " wan2 "
cwords[nlength - 5] = cwords[nlength - 5] .. " shi2 "
cwords[nlength - 6] = cwords[nlength - 6] .. " bai3 "
cwords[nlength - 7] = cwords[nlength - 7] .. " qian1 "
cwords[nlength - 8] = cwords[nlength - 8] .. " yi4 "
elseif nlength == 10 then
cwords[nlength - 1] = cwords[nlength - 1] .. " shi2 "
cwords[nlength - 2] = cwords[nlength - 2] .. " bai3 "
cwords[nlength - 3] = cwords[nlength - 3] .. " qian1 "
cwords[nlength - 4] = cwords[nlength - 4] .. " wan2 "
cwords[nlength - 5] = cwords[nlength - 5] .. " shi2 "
cwords[nlength - 6] = cwords[nlength - 6] .. " bai3 "
cwords[nlength - 7] = cwords[nlength - 7] .. " qian1 "
cwords[nlength - 8] = cwords[nlength - 8] .. " yi4 "
cwords[nlength - 9] = cwords[nlength - 9] .. " shi2 "
end
while cwords[nlength] == "ying2" and zero ~= true do
cwords[nlength] = nil
nlength = nlength - 1
end
fconvnumber = table.concat(cwords)
fconvnumber = string.gsub(fconvnumber, "ying2 %a+%d", "ying2")
for i=1,nlength do
fconvnumber = string.gsub(fconvnumber, "ying2 ying2", "ying2")
end
fconvnumber = string.gsub(fconvnumber, "yi1 shi2", "shi2")
print(fconvnumber)
因此,例如,如果我输入 100,nlength 为 3,cwords[nlength] == "ying2",它会删除最后一个数组。但它不会再次循环并删除十位 0。我知道我一定是以某种方式错过了逻辑,但我只是看不到它。直到使用 nlength 的早期值吗?谢谢!