Пример #1
0
        public static int Convert(string conversionString)
        {
            if (conversionString == null || conversionString.Equals(""))
            {
                return(0);
            }

            NumericTermDatabase.BuildFields();
            string[] words = conversionString.Split(' ');

            int  multiplier      = 1 * NumericTermDatabase.CheckNegativity(words[0]);
            bool hundredInEffect = false;

            int total = 0;

            for (int i = words.Length - 1; i >= 0; i--)
            {
                string word = words[i];
                ;
                if (NumericTermDatabase.IsMag(word))
                {
                    int mag = NumericTermDatabase.GetMagValue(word);

                    if (mag == 100)
                    {
                        hundredInEffect = true;
                    }
                    else
                    {
                        multiplier /= Math.Abs(multiplier);
                        multiplier *= mag;
                    }
                }
                else if (NumericTermDatabase.CheckNegativity(word) == 1)
                {
                    if (hundredInEffect)
                    {
                        multiplier *= 100;
                    }

                    total += NumericTermDatabase.GetTermValue(word) * multiplier;

                    if (hundredInEffect)
                    {
                        hundredInEffect = false;
                        multiplier     /= 100;
                    }
                }
            }

            return(total);
        }
Пример #2
0
        public static string Convert(int conversionInt)
        {
            NumericTermDatabase.BuildFields();

            if (conversionInt == 0)
            {
                return("zero");
            }

            bool negative = false;

            if (conversionInt < 0)
            {
                conversionInt = -conversionInt;
                negative      = true;
            }

            int[]  digits          = GetDigits(conversionInt);
            string convertedString = "";

            int  spotType   = 0;
            int  multiplier = 1;
            bool canCheck   = true;

            for (int i = digits.Length - 1; i >= 0; i--)
            {
                int digit = digits[i];

                if (spotType == 0)
                {
                    canCheck = true;
                    string term;

                    if (i - 1 >= 0)
                    {
                        if (digits[i - 1] == 1)
                        {
                            term          = NumericTermDatabase.GetTerm(10 + digit);
                            digits[i - 1] = 0;
                        }
                        else
                        {
                            term = NumericTermDatabase.GetTerm(digit);
                        }
                    }
                    else
                    {
                        term = NumericTermDatabase.GetTerm(digit);
                    }

                    if (digit != 0)
                    {
                        string mag = NumericTermDatabase.GetMagTerm(multiplier);
                        if (canCheck && !(mag.Equals("hundred") || mag.Equals("")))
                        {
                            convertedString = mag + " " + convertedString;
                        }

                        convertedString = term + " " + convertedString;
                        canCheck        = false;
                    }

                    spotType++;
                }
                else if (spotType == 1)
                {
                    if (digit != 0)
                    {
                        string mag = NumericTermDatabase.GetMagTerm(multiplier);
                        if (canCheck && !(mag.Equals("hundred") || mag.Equals("")))
                        {
                            convertedString = mag + " " + convertedString;
                        }

                        convertedString = NumericTermDatabase.GetTerm(digit * 10) + " " + convertedString;
                        canCheck        = false;
                    }

                    spotType++;
                }
                else if (spotType == 2)
                {
                    if (digit != 0)
                    {
                        string mag = NumericTermDatabase.GetMagTerm(multiplier);
                        if (canCheck && !(mag.Equals("hundred") || mag.Equals("")))
                        {
                            convertedString = mag + " " + convertedString;
                        }

                        convertedString = NumericTermDatabase.GetTerm(digit) + " hundred " + convertedString;
                        canCheck        = false;
                    }

                    spotType = 0;

                    multiplier *= 1000;
                }
            }

            if (negative)
            {
                convertedString = "negative " + convertedString;
            }
            Console.WriteLine(convertedString.Length - 1);
            return(convertedString.Substring(0, convertedString.Length - 1));
        }