罗马数字转换器 Swift

Roman Numeral Converter In Swift

我已经编程了大约 4 个月了。主要是 Java 脚本,但是,我现在正在转向 Swift 和 Java。

我正在努力实现一个适当的 for-in 语句来创建一个将数字转换为罗马数字的计算器。 (我已经用 JS 完成了)。

我做过的事情:

我卡住的地方是 运行 通过数字循环查找罗马数字

import UIKit

class ViewController: UIViewController {
    var inputNumber = 0
    var outputNumeral = ""

    var numberValue = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    var romanNumeral = [
      "M",
      "CM",
      "D",
      "CD",
      "C",
      "XC",
      "L",
      "XL",
      "X",
      "IX",
      "V",
      "IV",
      "I"
    ]

    func converter(inputNumber: Int) -> String {
        for numb in numberValue {
            while numberValue[0] < numb {
                romanNumeral + outputNumeral
                numb - numberValue[0]
            }
        }

        return outputNumeral
    }    
}

我这样做了,适用于 1 到 4999

let initial = 1980
var val = initial
var countForRoman : [(rom: String, num: Int)] = []    // how many occurences of romanNumbers; don't use dictionaries as not ordered

for (index, number) in numberValue.enumerated() {
    let x = val / number
    if x > 0 {
        countForRoman.append((romanNumeral[index], x))
        val = val - x * number
    }
}
print(countForRoman)    // Just to see how it converted

var romanString = ""
for pairs in countForRoman {
    let iter = pairs.num
    for _ in 1 ... iter { romanString += pairs.rom }
}

print(initial, "in roman is", romanString)

1980 年示例:

[(rom: "M", num: 1), (rom: "CM", num: 1), (rom: "L", num: 1), (rom: "X", num: 3)]
1980 in roman is MCMLXXX

如果你想超过4999(这里最多399999):

var numberValue = [100_000, 90_000, 50_000, 40_000, 10_000, 9000, 5000, 4000, 1000,900,500,400,100,90,50,40,10,9,5,4,1]
var romanNumeral = [
    "(C)",
    "(X)(C)",
    "(L)",
    "(X)(L)",
    "(X)",
    "M(X)",
    "(V)",
    "M(V)",
    "M",
    "CM",
    "D",
    "CD",
    "C",
    "XC",
    "L",
    "XL",
    "X",
    "IX",
    "V",
    "IV",
    "I"
]


let initial = 9999
var val = initial
var countForRoman : [(rom: String, num: Int)] = []    // how many occurences of romanNumbers; don't use dictionaries as not ordered

for (index, number) in numberValue.enumerated() {
    let x = val / number
    if x > 0 {
        countForRoman.append((romanNumeral[index], x))
        val = val - x * number
    }
}
print(countForRoman)    // Just to see how it converted

var romanString = ""
for pairs in countForRoman {
    let iter = pairs.num
    for _ in 1 ... iter { romanString += pairs.rom }
}

print(initial, "in roman is", romanString)

得到

[(rom: "M(X)", num: 1), (rom: "CM", num: 1), (rom: "XC", num: 1), (rom: "IX", num: 1)]
9999 in roman is M(X)CMXCIX

为 39999

[(rom: "(X)", num: 3), (rom: "M(X)", num: 1), (rom: "CM", num: 1), (rom: "XC", num: 1), (rom: "IX", num: 1)]
39999 in roman is (X)(X)(X)M(X)CMXCIX

对于129_999

[(rom: "(C)", num: 1), (rom: "(X)", num: 2), (rom: "M(X)", num: 1), (rom: "CM", num: 1), (rom: "XC", num: 1), (rom: "IX", num: 1)]
129999 in roman is (C)(X)(X)M(X)CMXCIX

只需将这段代码封装在convert函数中即可。