-1

我是编程新手,我从 Objective C 作为我的第一语言开始。我在乱看一些书籍和教程,最后编写了一个计算器......一切都很好,我正在进入(编程真的很有趣)

现在我问自己如何将阿拉伯数字翻译成中文数字(例如,阿拉伯数字 4 是中文四而 8 是八,这意味着四 + 四 = 八 中国数字系统与阿拉伯数字系统有点不同,它们有 100、1000 的符号, 10000 和 ja 有点扭曲,搞砸了我的大脑......无论如何,有没有人有一些建议、提示、提示或解决方案,我如何告诉计算机如何处理这些数字,甚至如何计算它们?

我认为一切皆有可能,所以我不会问“如果它甚至可能吗?”

4

3 回答 3

2

考虑到维基百科http://en.wikipedia.org/wiki/Chinese_numerals描述的中文数字系统(普通话),例如:

  • 45被解释为[4] [10] [5],写成四十五</li>
  • 114解释为[1][100][1][10][4]写成一百一十四

所以诀窍是将一个数字分解为 10 的幂:

x = c(k)*10^k + ... + c(1)*10 + c(0)

其中 k 是除以 x 使得商至少为 1 的 10 的最大幂。在上面的第二个示例中,114 = 1*10^2 + 1*10 + 4。

这 x = c(k)*10^k + ... + c(1)*10 + c(0) 变为[c(k)][10^k]...[c(1)][10][c(0)]。在第二个例子中,114 = [1] [100] [1] [10] [4]。

然后将括号内的每个数字映射到相应的正弦图:

0 = 〇</p>

1 = 一

2 = 二</p>

3 = 三</p>

4 = 四</p>

5 = 五</p>

6 = 六</p>

7 = 七</p>

8 = 八</p>

9 = 九</p>

10 = 十</p>

100 = 百</p>

1000 = 千</p>

10000 = 万</p>

只要掌握好[c(k)][10^k]...[c(1)][10][c(0)]表格,就很容易转换成计算机可以处理的整数或对应的中文数字。所以[c(k)][10^k]...[c(1)][10][c(0)]我将这种形式存储在一个大小为 k+2 的整数数组中。

于 2013-02-25T22:49:16.247 回答
1

我不熟悉 Objective-C,因此我无法为您提供 iOS 解决方案。尽管如此,以下是 Android 的 Java 代码......我认为它可能对您有所帮助,也对我有所帮助。

double text2double(String text) {


    String[] units = new String[] { "〇", "一", "二", "三", "四",
            "五", "六", "七", "八", "九"};

    String[] scales = new String[] { "十", "百", "千", "万",
            "亿" };

    HashMap<String, ScaleIncrementPair> numWord = new HashMap<String, ScaleIncrementPair>();

    for (int i = 0; i < units.length; i++) {
        numWord.put(units[i], new ScaleIncrementPair(1, i));
    }   

    numWord.put("零", new ScaleIncrementPair(1, 0));
    numWord.put("两", new ScaleIncrementPair(1, 2));

    for (int i = 0; i < scales.length; i++) {
        numWord.put(scales[i], new ScaleIncrementPair(Math.pow(10, (i + 1)), 0));
    }

    double current = 0;
    double result = 0;

    for (char character : text.toCharArray()) {

        ScaleIncrementPair scaleIncrement = numWord.get(String.valueOf(character));
        current = current * scaleIncrement.scale + scaleIncrement.increment;
        if (scaleIncrement.scale > 10) {
            result += current;
            current = 0;
        }
    }

    return result + current;
}

class ScaleIncrementPair {
    public double scale;
    public int increment;

    public ScaleIncrementPair(double s, int i) {
        scale = s;
        increment = i;
    }
}
于 2015-01-08T20:04:21.507 回答
1

你可以利用NSNumberFormatter.

像下面的代码,首先NSNumber从汉字中获取,然后将它们组合起来。

func getNumber(fromText text: String) -> NSNumber? {
    let locale = Locale(identifier: "zh_Hans_CN")
    let numberFormatter = NumberFormatter()
    numberFormatter.locale = locale
    numberFormatter.numberStyle = .spellOut
    guard let number = numberFormatter.number(from: text) else { return nil }
    print(number)
    return number
}
于 2017-01-30T15:35:41.063 回答