svetlozar_kirkov

Methods Menu Exercise (temp)

Oct 3rd, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Threading;
  6.  
  7. namespace ConsoleTests
  8. {
  9.     class ConsoleTests
  10.     {
  11.         static void Main()
  12.         {
  13.             Console.WriteLine("Available choices: ");
  14.             Console.WriteLine();
  15.             Console.WriteLine("1. Put the digits from an integer number into a reversed order");
  16.             Console.WriteLine("2. Calculate the average of given sequence of numbers");
  17.             Console.WriteLine("3. Solve the linear equation a * x + b = 0");
  18.             Console.Write("\nEnter your choice: ");
  19.             byte answer = byte.Parse(Console.ReadLine());
  20.             if (answer == 1)
  21.             {
  22.                 int integer = 0;
  23.                 bool isValid;
  24.                 bool isValidInt32;
  25.                 do
  26.                 {
  27.                     Console.Write("\nEnter valid integer (1 to 50 000 000): ");
  28.                     //int test;
  29.                     isValidInt32 = int.TryParse(Console.ReadLine(), out integer);
  30.                     isValid = integer >= 1 && integer <= 50000000;
  31.                 } while (isValid == false);
  32.                 Console.WriteLine("\n-> Reversed: {0}", ReverseDigits(integer));
  33.             }
  34.             else if (answer == 2)
  35.             {
  36.                 string collection;
  37.                 bool isNotEmpty;
  38.                 do
  39.                 {
  40.                     Console.Write("\nEnter numbers (separated by space): ");
  41.                     collection = Console.ReadLine();
  42.                     isNotEmpty = collection.Length >= 1;
  43.                 } while (isNotEmpty == false);
  44.                 string[] numsArray = collection.Split(' ').ToArray();
  45.                 Console.WriteLine("-> Average: {0}", CalcAverage(numsArray));
  46.             }
  47.             else if (answer == 3)
  48.             {
  49.                 decimal a;
  50.                 bool isNotZero;
  51.                 do
  52.                 {
  53.                     Console.Write("Enter a (a != 0): ");
  54.                      a = decimal.Parse(Console.ReadLine());
  55.                     isNotZero = a != 0;
  56.                 } while (isNotZero == false);
  57.                
  58.                 Console.Write("Enter b: ");
  59.                 decimal b = decimal.Parse(Console.ReadLine());
  60.                 CalcLinearEquation(a,b);
  61.             }
  62.  
  63.         }
  64.  
  65.         static string ReverseDigits(int a)
  66.         {
  67.             int[] digits = a.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();
  68.             Array.Reverse(digits);
  69.             string output = string.Join("", digits);
  70.             return output;
  71.         }
  72.  
  73.         static decimal CalcAverage(params string[] array)
  74.         {
  75.             decimal sum = 0;
  76.             foreach (var num in array)
  77.             {
  78.                 sum += decimal.Parse(Convert.ToString(num));
  79.             }
  80.             decimal average = sum/array.Length;
  81.             return average;
  82.         }
  83.  
  84.         static void CalcLinearEquation(decimal a, decimal b)
  85.         {
  86.             decimal x = -b/a;
  87.             Console.WriteLine("x = {0}", x);
  88.             Console.WriteLine("{0}*({1})+{2}=0", a, x, b);
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment