Advertisement
FLISEN

Untitled

Jan 18th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.24 KB | None | 0 0
  1. using System;
  2.  
  3. class ComplexProgram
  4. {
  5.     static void Main()
  6.     {
  7.  
  8.         Console.WriteLine("Hello! Please, choose what task you want to solve and enter its number.\n");
  9.         Console.WriteLine("1. Reverses the digits of a number\n2. Calculates the average of a sequence of integers\n"
  10.         + "3. Solves a linear equation a * x + b = 0\n");
  11.  
  12.         TaskChoice();
  13.  
  14.     }
  15.  
  16.     private static void TaskChoice()
  17.     {
  18.         Console.Write("Task number: ");
  19.         int task = int.Parse(Console.ReadLine());
  20.  
  21.         switch (task)
  22.         {
  23.             case 1: NumberInput();
  24.                 TaskContinue();
  25.                 break;
  26.             case 2: SequenceLength();
  27.                 TaskContinue();
  28.                 break;
  29.             case 3: EquationInput();
  30.                 TaskContinue();
  31.                 break;
  32.             default: Console.WriteLine("Enter a number from 1 to 3!\n");
  33.                 TaskChoice();
  34.                 break;
  35.         }
  36.     }
  37.     private static void TaskContinue()
  38.     {
  39.         Console.WriteLine();
  40.         Console.WriteLine("Do you want to solve something else? (Y/N)");
  41.         char cont = char.Parse(Console.ReadLine());
  42.         if (cont == 'Y' || cont == 'y')
  43.         {
  44.             TaskChoice();
  45.         }
  46.         else if (cont == 'N' || cont == 'n')
  47.         {
  48.             Console.WriteLine("Thank you for using the program!");
  49.         }
  50.         else
  51.         {
  52.             Console.WriteLine("Please, enter a correct answer!");
  53.             TaskContinue();
  54.         }
  55.     }
  56.  
  57.     private static void NumberInput()
  58.     {
  59.         bool pos = false;
  60.         decimal number;
  61.         do
  62.         {
  63.             Console.WriteLine("Enter a positive number!");
  64.             number = decimal.Parse(Console.ReadLine());
  65.             if (number > 0)
  66.             {
  67.                 pos = true;
  68.             }
  69.         }
  70.         while (!pos);
  71.  
  72.         Reverse(number);
  73.     }
  74.     private static void Reverse(decimal number)
  75.     {
  76.         string stringNumber = number.ToString();
  77.         string[] reversedNumber = new string[stringNumber.Length];
  78.  
  79.         for (int i = stringNumber.Length - 1; i >= 0; i--)
  80.         {
  81.             reversedNumber[stringNumber.Length - i - 1] = stringNumber[i].ToString();
  82.         }
  83.  
  84.         foreach (string digit in reversedNumber)
  85.         {
  86.             Console.Write(digit);
  87.         }
  88.         Console.WriteLine();
  89.     }
  90.  
  91.     private static void SequenceLength()
  92.     {
  93.         bool nonEmpty = false;
  94.         int number;
  95.         do
  96.         {
  97.             Console.WriteLine("Enter a how many real positive numbers will calculate!");
  98.             number = int.Parse(Console.ReadLine());
  99.             if (number > 0)
  100.             {
  101.                 nonEmpty = true;
  102.             }
  103.         }
  104.         while (!nonEmpty);
  105.  
  106.         InputAndCalculation(number);
  107.     }
  108.     private static void InputAndCalculation(int number)
  109.     {
  110.         Console.WriteLine("Enter the numbers:");
  111.         int sum = 0;
  112.         for (int i = 0; i < number; i++)
  113.         {
  114.             sum += int.Parse(Console.ReadLine());
  115.         }
  116.         Console.WriteLine("Their average sum is: {0} ", (decimal)sum / number);
  117.     }
  118.  
  119.     private static void EquationInput()
  120.     {
  121.         bool pos = false;
  122.         decimal a;
  123.         do
  124.         {
  125.             Console.WriteLine("Enter the first number of linear equation - a>0!");
  126.             a = decimal.Parse(Console.ReadLine());
  127.             if (a > 0)
  128.             {
  129.                 pos = true;
  130.             }
  131.         }
  132.         while (!pos);
  133.         Console.WriteLine("Enter the second number of linear equation - b!");
  134.         decimal b = decimal.Parse(Console.ReadLine());
  135.  
  136.         EquationCalculation(a, b);
  137.        
  138.     }
  139.     private static void EquationCalculation(decimal a, decimal b)
  140.     {
  141.         decimal x = 0;
  142.         if (b > 0)
  143.         {
  144.             Console.WriteLine();
  145.             Console.Write("Your linear equation is: {0}*X + {1} = 0\n", a, b);
  146.             Console.WriteLine("X = {0}", x = -b / a);
  147.         }
  148.         else
  149.         {
  150.             Console.WriteLine();
  151.             Console.Write("Your linear equation is: {0}*X - {1} = 0\n", a, Math.Abs(b));
  152.             Console.WriteLine("X = {0}", x = b / a);
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement