Advertisement
Asinka

SimpleTextBasedMenu

Jan 26th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4.  
  5. class SimpleTextBasedMenu
  6. {
  7.     static int selectedOption = 0;
  8.  
  9.     static int PrintTextBasedMenu()
  10.     {
  11.         Console.WriteLine("------------------------------------------");
  12.         Console.WriteLine("Enter number of option you want to choise:");
  13.         Console.WriteLine("------------------------------------------");
  14.         Console.WriteLine("1. Reverses the digits of a number");
  15.         Console.WriteLine("2. Calculates the average of a sequence of integers");
  16.         Console.WriteLine("3. Solves a linear equation a * x + b = 0");
  17.         Console.WriteLine("\nFor example: 1, 2, 3 ...");
  18.         selectedOption = int.Parse(Console.ReadLine());
  19.  
  20.         return selectedOption;
  21.     }
  22.  
  23.     static void ReverseDigitOfNumber()
  24.     {
  25.         Console.WriteLine("\nEnter positive decimal numbers to reverse:");
  26.  
  27.         string digits = Console.ReadLine();
  28.         while (digits.Substring(0, 1) == "-")
  29.         {
  30.             Console.WriteLine("The decimal number should be non-negative. Please try again!");
  31.             digits = Console.ReadLine();
  32.         }
  33.  
  34.         Console.WriteLine("\nRepresent ot this number is:");
  35.  
  36.         for (int i = digits.Length - 1, count = 0; i >= 0; i--, count++)
  37.         {
  38.             Console.Write("{0}", digits.Substring(i, 1));
  39.         }
  40.         Console.WriteLine("\n");
  41.     }
  42.  
  43.     static void FindAverageOfSequenceIntegers()
  44.     {
  45.         Console.WriteLine("\nEnter length of sequence:");
  46.         int length = int.Parse(Console.ReadLine());
  47.         while (length <= 0)
  48.         {
  49.             Console.WriteLine("The sequnce should not be empty. Please try again!");
  50.             length = int.Parse(Console.ReadLine());
  51.         }
  52.  
  53.         Console.Write("n0 = ");
  54.         int sum = int.Parse(Console.ReadLine());
  55.  
  56.         for (int i = 1; i < length; i++)
  57.         {
  58.             Console.Write("n{0} = ", i);
  59.             sum = sum + int.Parse(Console.ReadLine());
  60.         }
  61.  
  62.         Console.WriteLine("\nThe avarage of the sequence is: {0}", sum / length );
  63.     }
  64.  
  65.     static void SolveLinearEquation()
  66.     {
  67.         Console.WriteLine("\nEnter coefficients of linear equation:");
  68.         Console.WriteLine("a = ");
  69.         double coefficientA = double.Parse(Console.ReadLine());
  70.  
  71.         while (coefficientA == 0)
  72.         {
  73.             Console.WriteLine("Invaled number: a should not be equal to 0. Please try again!");
  74.             coefficientA = double.Parse(Console.ReadLine());
  75.         }
  76.  
  77.         Console.WriteLine("b = ");
  78.         double coefficientB = double.Parse(Console.ReadLine());
  79.         Console.WriteLine("\nResult: x = {0}", -coefficientB / coefficientA);
  80.     }
  81.  
  82.     static void Main()
  83.     {
  84.         PrintTextBasedMenu();
  85.         if (selectedOption == 1)
  86.         {
  87.             ReverseDigitOfNumber();
  88.         }
  89.         else if(selectedOption == 2)
  90.         {
  91.             FindAverageOfSequenceIntegers();
  92.         }
  93.         else if (selectedOption == 3)
  94.         {
  95.             SolveLinearEquation();
  96.         }
  97.         else
  98.         {
  99.             Console.WriteLine("You have entered invalid option!");
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement