0

我正在创建一个类来将整数转换为自然语言中的句子。我正在进行一些基本检查,以确保给定的数字在 -9999 和 9999 之间。我觉得这在大多数情况下都有效。

但是,一旦程序到达“this.convertSentence”——通过 try/catch 块和错误检查,我想知道现在最好的做法是将问题分解为需要运行的各种函数调用以获得任务完成。

我打算用 this.convertSentence 做一些检查数字大小等......然后将数字发送到单独的函数以做更多工作并让它们传播一个句子以返回。我不确定我是否想要在我的班级中使用一个变量,或者我是否应该传递一个变量来构建句子。我想知道这样的事情。

/**
*   A class for converting an integer to a natrual language sentence in Spanish.
*   Accepts integers from -9999 to 9999
*
*/
function NumberToWord () {

    this.getSentence = function(number) {

        // Check for erroneous input. Accepts only -9999 thru 9999 integers
        try
        {
            if(number === parseInt(number) && number > -10000 && number < 10000) {
                return this.convertSentence(number);
            } 
            else {
                throw new Error("Argument is not an integer between -9999 and 9999");
            }
        }
        catch(e){
            console.log(e.name + " " + e.message);
        }



    };

    this.convertSentence = function(number) {
        return "This is where I'll start the logic for the sentence";
    };

}


var numberToWord = new NumberToWord();




// Tests
console.log(numberToWord.getSentence(9999));
console.log(numberToWord.getSentence(-9999));
console.log(numberToWord.getSentence(10000));
console.log(numberToWord.getSentence(-10000));
console.log(numberToWord.getSentence(0));
console.log(numberToWord.getSentence(1.1));
console.log(numberToWord.getSentence(-9999.1));
console.log(numberToWord.getSentence(10001));
console.log(numberToWord.getSentence(-10001));
console.log(numberToWord.getSentence(5.5));
console.log(numberToWord.getSentence());
4

2 回答 2

1

There are a few things I found amiss in your code:

  1. You don't need a class. You simply want to convert a number to a sentence. Use a function.
  2. Why are both getSentence and convertSentence public? Only getSentence should be public.
  3. Since your class will (in all probability) only be instatiated once, use the singleton pattern.

Things I would do:

  1. Because you want to make your code modular, I would use the module pattern.
  2. You can delegate specific tasks to different functions, but keep them in a private namespace.

Here's the code:

Number.prototype.toWord = function () {
    return function (lang) {
        var number = this.valueOf();

        if (parseInt(number) === number) {
            if (number < 10000 && number > 10000) {
                switch (lang) {
                case "es":
                    return toSpanish(number);
                case "en":
                default:
                    return toEnglish(number);                        
                }
            } else throw new RangeError("Expected an integer between ±10000.");
        } else throw new TypeError("Expected an integer.");
    };

    function toSpanish(number) {
        // convert the number to Spanish
    }

    function toEnglish(number) {
        // convert the number to English
    }
}();

Then you can use it like this:

var number = 1337;
alert(number.toWord("es"));

Edit: I wrote a simple function which will do what you want. However it's in English. I don't know Spanish so you'll have to implement that yourself. Here's the demo: http://jsfiddle.net/XKYhx/2/

于 2012-11-17T08:36:18.353 回答
0

我的想法是检查句子中有多少部分并构建一个数组以与子字符串匹配。例如,无论如何用英语(我不会说西班牙语!)

作为自然语言,您会说(减去)xxx 千和 xxx,因为您的数字的最大/最小为 ~10000 / ~-10000,在伪代码中:

    var sign = ""
    var wholeparts = new Array()
    var mantissaparts = new Array()
      if number < 0, 
      sign = "minus"
      number = math.abs(number) // turn the number into a positive number now we have the      sign
    var whole = math.floor(number) //get whole number 
    var mantissa = number - whole //get the after decimal part if exists
    if whole > 1000
      wholeparts.push(math.floor(whole/1000)) //get the thousands part 
      wholeparts.push(whole - parts[0]*1000) // add the hundreds
    else
      parts.push(whole)

 if mantissa.length > 0
   do something similar for the mantissa to the mantissaparts array.

At this point you would have the sentence structure broken down then:

string sentance:
 foreach (var part in wholeparts)
 stringify and check each number, converting to human words depending on index, ie "seven" or "seventy", add each to the string. 
  if wholeparts.length > 1 : sentence = sentence + " thousand and"

then if you had a mantissa, sentence = sentence + "point" .. then and the mantissa as natural language.

我能想到的最佳细分是:

  • 将数字(整数或尾数)转换为数组的方法,
  • 将数组转换为自然语言的方法,并带有一个参数,说明它将使用的不同措辞是整数还是尾数。
  • 接受字符串形式的数字并返回自然语言等价物的方法

希望有帮助..正在思考。

于 2012-11-17T08:24:52.547 回答