Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace _14._Numbers_to_Words_3
- {
- class Program
- {
- // --- SOLUTION WITH 1 METOD + 3 SUB METODS + 1 SUB SUB METOD (11 to 19) --------------------
- static void Main(string[] args)
- {
- int loops = int.Parse(Console.ReadLine());
- for (int i = 1; i <= loops; i++)
- {
- int number = int.Parse(Console.ReadLine());
- string prnData = Letterize(number);
- Console.WriteLine(prnData);
- }
- }
- static string Letterize(int number)
- {
- string borderBreaks = string.Empty;
- // before to start with the calculation is good to clean the
- // border cases (numbers witn more than 3 digits & numbers with less than 3 digits)
- if (number > 999)
- {
- borderBreaks = "too large";
- return borderBreaks;
- }
- else if (number < -999)
- {
- borderBreaks = "too small";
- return borderBreaks;
- }
- string negativeORpositive = string.Empty; // as defoult is asume that the number is positive
- if (number < 0) negativeORpositive = "minus ";
- number = Math.Abs(number); // we will work with the absolute value of "n"
- //
- int onesINT = number % 10;
- int tensINT = (number / 10) % 10;
- int handredsINT = (number / 100) % 10;
- // as a generall - the code is working for every number between -999 to 999
- // but in the requiremnts is askin that if the number is consist of less that 3 digits
- // the answer to be ""
- // that is this filter for
- if (handredsINT == 0) return "";
- // ще завъртим 3+1 метода (3 + метода за 11 до 19)
- string onesSTR = Ones(onesINT, tensINT, handredsINT);
- string tensSTR = Tens(onesINT, tensINT, handredsINT);
- string handredsSTR = Handreds(onesINT, tensINT, handredsINT);
- // --- prn ---
- // the print is a little bit complicate - thats why here is the expression of the print:
- // +/-(1) handreds(2) and(3) tens(4) space(5) ones(6) --> following this, the print
- // will be aranged as 1+2+3+4+5+6 --> for every print member is necessary to be checked
- // a few conditions
- // seems that the next asignemnts are not necessary becouse we olready have some of variables
- // and is not necessary to asigne them again to anather variable
- // BUT, to make more cleare the print arangement, this would be usefull:
- string one = string.Empty; // here, we will kept the + or - value
- one = negativeORpositive;
- string two = string.Empty; // here, we will kept value of the handreds
- if (handredsINT != 0) two = handredsSTR;
- string three = string.Empty; // here, we will kept the word " and " - in case we will need it
- if (handredsINT != 0 && (tensINT != 0 || onesINT != 0)) three = " and ";
- string four = string.Empty; // here, we will kept value of the tens
- if (tensINT != 0) four = tensSTR;
- string five = string.Empty; // this is the spasion between tens and ones (if have)
- if (tensINT >= 2 && onesINT != 0) five = " ";
- string six = string.Empty; // here, we will kept value of the ones
- if (tensINT != 1 && onesINT > 0) six = onesSTR;
- //return value from the metod is:
- string result = one + two + three + four + five + six;
- return result;
- }
- static string Handreds(int ons, int tns, int hndrds)
- // nonused parameters are included in the Handreds( parameters ) - this is in case...
- // sometimes... in the future ... may be... will be needed for a some sort check.
- {
- string handredsAsWord = string.Empty;
- switch (hndrds)
- {
- case 0: handredsAsWord = ""; break;
- case 1: handredsAsWord = "one-hundred"; break;
- case 2: handredsAsWord = "two-hundred"; break;
- case 3: handredsAsWord = "three-hundred"; break;
- case 4: handredsAsWord = "four-hundred"; break;
- case 5: handredsAsWord = "five-hundred"; break;
- case 6: handredsAsWord = "six-hundred"; break;
- case 7: handredsAsWord = "seven-hundred"; break;
- case 8: handredsAsWord = "eight-hundred"; break;
- case 9: handredsAsWord = "nine-hundred"; break;
- //default: handredsAsWord = ""; break;
- }
- return handredsAsWord;
- }
- static string Tens(int ons, int tns, int hndrds)
- // nonused parameters are included in the Tens( parameters ) - this is in case...
- // sometimes... in the future ... may be... will be needed for a some sort check.
- {
- string tensAsWord = string.Empty;
- switch (tns)
- {
- case 0: tensAsWord = ""; break;
- case 1: tensAsWord = case1119(ons); break; // this is the case between 11 and 19
- case 2: tensAsWord = "twenty"; break;
- case 3: tensAsWord = "thirty"; break;
- case 4: tensAsWord = "forty"; break;
- case 5: tensAsWord = "fifty"; break;
- case 6: tensAsWord = "sixty"; break;
- case 7: tensAsWord = "seventy"; break;
- case 8: tensAsWord = "eighty"; break;
- case 9: tensAsWord = "ninety"; break;
- //default: handredsAsWord = ""; break;
- }
- return tensAsWord;
- }
- static string Ones(int ons, int tns, int hndrds)
- // nonused parameters are included in the Ones( parameters ) - this is in case...
- // sometimes... in the future ... may be... will be needed for a some sort check.
- {
- string onesAsWord = string.Empty;
- switch (ons)
- {
- case 0: onesAsWord = ""; break;
- case 1: onesAsWord = "one"; break;
- case 2: onesAsWord = "two"; break;
- case 3: onesAsWord = "three"; break;
- case 4: onesAsWord = "four"; break;
- case 5: onesAsWord = "five"; break;
- case 6: onesAsWord = "six"; break;
- case 7: onesAsWord = "seven"; break;
- case 8: onesAsWord = "eight"; break;
- case 9: onesAsWord = "nine"; break;
- }
- return onesAsWord;
- }
- static string case1119(int ons)
- {
- string teens = string.Empty;
- switch (ons)
- {
- case 0: teens = "ten"; 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement