コード例 #1
0
            public static string GetOnes(int num)
            {
                string ones;

                switch (CodeSnippets.GetPlaceValue(num, 1))
                {
                default: ones = "ERROR!!"; break;

                case 0: ones = ""; break;

                case 1: ones = "one"; break;

                case 2: ones = "two"; break;

                case 3: ones = "three"; break;

                case 4: ones = "four"; break;

                case 5: ones = "five"; break;

                case 6: ones = "six"; break;

                case 7: ones = "seven"; break;

                case 8: ones = "eight"; break;

                case 9: ones = "nine"; break;
                }
                return(ones);
            }
コード例 #2
0
            public static string GetTeens(int num)
            {
                string teens;

                switch (CodeSnippets.GetPlaceValue(num, 1))
                {
                default: teens = ""; break;

                case 1: teens = "eleven"; break;

                case 2: teens = "twelve"; break;

                case 3: teens = "thirteen"; break;

                case 4: teens = "fourteen"; break;

                case 5: teens = "fifteen"; break;

                case 6: teens = "sixteen"; break;

                case 7: teens = "seventeen";  break;

                case 8: teens = "eighteen"; break;

                case 9: teens = "nineteen"; break;
                }
                return(teens);
            }
コード例 #3
0
            public static string GetHundreds(int num)
            {
                string hundreds;

                switch (CodeSnippets.GetPlaceValue(num, 100))
                {
                default: hundreds = ""; break;

                case 1: hundreds = "one hundred"; break;

                case 2: hundreds = "two hundred"; break;

                case 3: hundreds = "three hundred"; break;

                case 4: hundreds = "four hundred"; break;

                case 5: hundreds = "five hundred"; break;

                case 6: hundreds = "six hundred"; break;

                case 7: hundreds = "seven hundred"; break;

                case 8: hundreds = "eight hundred"; break;

                case 9: hundreds = "nine hundred"; break;
                }
                if (CodeSnippets.GetPlaceValue(num, 10) != 0 || CodeSnippets.GetPlaceValue(num, 1) != 0)
                {
                    hundreds += " and ";
                }
                return(hundreds);
            }
コード例 #4
0
            public static void RunEleven()
            {
                Console.WriteLine("\n05.11. * Write a program that converts a number in the range [0…999] to words, corresponding to the English pronunciation. Examples:");
                Console.WriteLine("- 0 --> \"Zero\"");
                Console.WriteLine("- 12 --> \"Twelve\"");
                Console.WriteLine("- 98 --> \"Ninety eight\"");
                Console.WriteLine("- 273 --> \"Two hundred seventy three\"");
                Console.WriteLine("- 400 --> \"Four hundred\"");
                Console.WriteLine("- 501 --> \"Five hundred and one\"");
                Console.WriteLine("- 711 --> \"Seven hundred and eleven\"\n");

                //could work this out better so that if hundreds place, ones are chosen followed by " hundred", rather than making each their own case. Would then be easier to add further numbers ie thousands.

                while (true)
                {
                    Console.WriteLine("Enter int bewteen 0 and 999");
                    int n = CodeSnippets.GetInt();
                    while (n < 0 || n > 999)
                    {
                        Console.WriteLine("Invalid int chosen");
                        n = CodeSnippets.GetInt();
                    }
                    string result = "";

                    StringBuilder sb = new StringBuilder();
                    switch (n)
                    {
                    default:
                        if (CodeSnippets.GetPlaceValue(n, 100) != 0)
                        {
                            sb.Append(GetHundreds(n));
                        }
                        if (CodeSnippets.GetPlaceValue(n, 10) != 0)
                        {
                            if (CodeSnippets.GetPlaceValue(n, 10) == 1)
                            {
                                sb.Append(GetTeens(n));
                            }
                            else
                            {
                                sb.Append(GetTens(n));
                            }
                        }
                        if (CodeSnippets.GetPlaceValue(n, 10) != 1)
                        {
                            sb.Append(GetOnes(n));
                        }
                        result = sb.ToString(); break;

                    case 0: result = "zero"; break;
                    }
                    Console.WriteLine(result.First().ToString().ToUpper() + result.Substring(1));

                    Console.WriteLine("\nCOMPLETE\n");
                }
            }
コード例 #5
0
            public static string GetTens(int num)
            {
                string tens;

                switch (CodeSnippets.GetPlaceValue(num, 10))
                {
                default: tens = ""; break;

                case 0: tens = " and "; break;

                case 1:
                    if (CodeSnippets.GetPlaceValue(num, 1) == 0)
                    {
                        tens = "ten";
                    }
                    else
                    {
                        tens = GetTeens(CodeSnippets.GetPlaceValue(num, 1));
                    }
                    tens = "ten"; break;

                case 2: tens = "twenty"; break;

                case 3: tens = "thirty"; break;

                case 4: tens = "forty"; break;

                case 5: tens = "fifty"; break;

                case 6: tens = "sixty"; break;

                case 7: tens = "seventy"; break;

                case 8: tens = "eighty"; break;

                case 9: tens = "ninty"; break;
                }
                if (CodeSnippets.GetPlaceValue(num, 10) != 1 || CodeSnippets.GetPlaceValue(num, 1) != 0)
                {
                    tens += " ";
                }
                return(tens);
            }