CAPTCHA.cs

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static void Main()
	{
		Random rnd = new Random();
		int numTest = 10;
		while (numTest > 0)
		{
			int num = rnd.Next(1, 9999);
			Console.WriteLine(string.Format("{0} = {1}", num, Task_159(num)));
			numTest--;
		}
	}
	
	private static string GetFourthLevel(int degree)
	{
		switch (degree)
		{
			case 1: return "одна тысяча "; 
			case 2: return "две тысячи ";
			case 3: return "три тысячи ";
			case 4: return "четыре тысячи ";
			case 5: return "пять тысяч ";
			case 6: return "шесть тысяч ";
			case 7: return "семь тысяч ";
			case 8: return "восемь тысяч ";
			case 9: return "девять тысяч ";
			default: return string.Empty;
		}
	}

	private static string GetThird(int degree)
	{
		switch (degree)
		{
			case 1: return "cто ";
			case 2: return "двести ";
			case 3: return "триста ";
			case 4: return "четыреста "; 
			case 5: return "пятьсот ";
			case 6: return "шестьсот ";
			case 7: return "семьсот ";
			case 8: return "восемьсот "; 
			case 9: return "девятьсот "; 
			default: return string.Empty;
		}
	}

	private static string GetSecondLevel(int secondDegree, int firstDegree)
	{
		switch (secondDegree)
		{
			case 1:
				switch (firstDegree)
				{
					case 0: return "десять ";
					case 1: return "одиннадцать ";
					case 2: return "двенадцать ";
					case 3: return "тринадцать ";
					case 4: return "четырнадцать ";
					case 5: return "пятнадцать ";
					case 6: return "шестьнадцать ";
					case 7: return "семнадцать ";
					case 8: return "восемьнадцать ";
					case 9: return "девятнадцать ";
					default: return string.Empty;
				}
			case 2: return "двадцать ";
			case 3: return "тридцать ";
			case 4: return "сорок ";
			case 5: return "пятьдесят ";
			case 6: return "шестьдесят ";
			case 7: return "семьдесят ";
			case 8: return "восемьдесят ";
			case 9: return "девяноста ";
			default: return string.Empty;
		}
	}

	private static string GetFirstLevel(int degree)
	{
		switch (degree)
		{
			case 1: return "один ";
			case 2: return "два ";
			case 3: return "три ";
			case 4: return "четыре ";
			case 5: return "пять ";
			case 6: return "шесть ";
			case 7: return "семь ";
			case 8: return "восемь ";
			case 9: return "девять ";
			default: return string.Empty;
		}
	}
	
	private static string Task_159(int number)
	{
		if (number < 0 || number > 9999)
		{
			throw new ArgumentException("Number should be greater than 0 and less then 10000!");
		}

		string result = string.Empty;
		int[] degrees = new int[4] { -1, -1, -1, -1 };
		int k = 0;
		while (number > 0)
		{
			degrees[k] = number % 10;
			number /= 10;
			k++;
		}

		for (int i = degrees.Length - 1; i >= 0; i--)
		{
			switch (i)
			{
				case 3: result += GetFourthLevel(degrees[i]); break;
				case 2: result += GetThird(degrees[i]); break;
				case 1: 
					result += GetSecondLevel(degrees[i], degrees[i - 1]);
					if (degrees[i] == 1) degrees[i - 1] = -1;
					break;
				case 0: result += GetFirstLevel(degrees[i]); break;
			}
		}
		return result;
	}	
}
;