Пример #1
0
        /// <summary>
        ///   Convert an integral number to it's written form.
        /// </summary>
        /// <param name="number">
        ///   The number to convert
        /// </param>
        /// <returns>
        ///  A string representing the input number in words
        /// </returns>
        protected virtual string ConvertIntegral(long number)
        {
            string buffer = number.ToString();

            StringBuilder words = new StringBuilder();

            bool       isTenOnes  = false;
            List <int> lastDigits = new List <int>();

            for (int i = 0; i < buffer.Length; i++)
            {
                Digit digit = new Digit(buffer, i);

                if ((digit.digit == 1) && (digit.basePlaceValue == BasePlaceValue.Tens))
                {
                    isTenOnes = true;
                    continue;
                }

                string word = ComposeWord(digit, isTenOnes, lastDigits);

                lastDigits.Add(digit.digit);

                if (digit.basePlaceValue == BasePlaceValue.Ones)
                {
                    lastDigits.Clear();
                }

                if (word.Length > 0)
                {
                    words.Append(word);
                    words.Append(" ");
                }

                isTenOnes = false;
            }

            return(words.ToString().Trim());
        }
Пример #2
0
 /// <summary>
 ///   Compose a word from a digit
 /// </summary>
 /// <param name="digit">
 ///   Information about a digit within a string
 /// </param>
 /// <param name="isTenOnes">
 ///   Whether the function should produce a number n, such that n ∈ {10, ..., 19}
 /// </param>
 /// <param name="lastDigits">
 ///   A list of last digits in a BasePlaceValue
 ///   If a digit has a Ones BasePlaceValue then lastDigits will contain the 2 previous digits if there were any
 /// </param>
 protected abstract string ComposeWord(Digit digit, bool isTenOnes, List <int> lastDigits);
Пример #3
0
        /// <summary>
        ///   See <c>AConverter.ComposeWord</c> documentation
        /// </summary>
        protected override string ComposeWord(Digit digit, bool isTensOnes, List <int> lastDigits)
        {
            string word = "";

            if (isTensOnes)
            {
                word  = TensOnes[digit.digit];
                word += " " + composeBigPlaceValueExtensionWord(MAX_EXTENSION_VALUE, digit.bigPlaceValue);
            }
            else
            {
                switch (digit.basePlaceValue)
                {
                case BasePlaceValue.Ones:

                    int lastDigit = lastDigits.FirstOrDefault();

                    bool shouldShowDigit = lastDigit > 0 || digit.digit > 1 || digit.bigPlaceValue == BigPlaceValue.None;

                    if (shouldShowDigit)
                    {
                        word = Ones[digit.digit];
                    }

                    int extensionValue;

                    if (digit.digit <= 1 && lastDigits.Sum() > 0)
                    {
                        extensionValue = MAX_EXTENSION_VALUE;
                    }
                    else
                    {
                        extensionValue = digit.digit;
                    }

                    string bigPlaceValueWord = composeBigPlaceValueExtensionWord(extensionValue, digit.bigPlaceValue);

                    if (bigPlaceValueWord.Length > 0)
                    {
                        if (digit.digit > 0)
                        {
                            word += " ";
                        }
                        word += bigPlaceValueWord;
                    }
                    break;

                case BasePlaceValue.Tens:
                    word = (composeTensWord(digit.digit));
                    break;

                case BasePlaceValue.Hundrends:
                    word = composeHundredsWord(digit.digit);
                    break;

                default:
                    break;
                }
            }

            return(word);
        }