Swift中的罗马数字转换器

时间:2020-01-18 15:08:31

标签: arrays swift swift5 roman-numerals

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

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

我已经完成的事情:

  • 创建了两个数组
  • 创建了输入和输出变量
  • 创建了一个接受Int输入和String输出的函数
  • 创建了一个for-in循环以遍历数字

我陷入困境的地方是通过数字循环查找罗马数字

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 个答案:

答案 0 :(得分:1)

我这样做了,它适用于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函数中,就可以了。