Advertisement
stambeto09

13.TaskSolver

Dec 19th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.82 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Numerics;
  4.  
  5. namespace _11.TasksSolver
  6. {
  7.     public class TaskSolver
  8.     {
  9.         public static BigInteger ReversingNumber(BigInteger currentNumber, BigInteger result)
  10.         {
  11.  
  12.             while (currentNumber > 0)
  13.             {
  14.                 result = result * 10 + currentNumber % 10;
  15.                 currentNumber /= 10;
  16.             }
  17.  
  18.             return result;
  19.         }
  20.  
  21.         public static int FindAverageElementInOddSequence(int [] sequence,int averageElement)
  22.         {
  23.             Array.Sort(sequence);
  24.             averageElement = sequence[(sequence.Length - 1) / 2];
  25.  
  26.             return averageElement;
  27.         }
  28.  
  29.         public static int[] FindAverageElementInEvenSequence(int [] sequence, int firstAverageElement,
  30.                                                              int secondAverageElement, int [] results)
  31.         {
  32.             Array.Sort(sequence);
  33.  
  34.             firstAverageElement = sequence [(sequence.Length - 1) / 2];
  35.             secondAverageElement=sequence [((sequence.Length - 1) / 2+1)];
  36.  
  37.             results[0] = firstAverageElement;
  38.             results[1] = secondAverageElement;
  39.  
  40.             return results;                        
  41.         }
  42.  
  43.         public static decimal LinearEquation(decimal aArgument, decimal bArgument)
  44.         {
  45.             //a*x+b=0;
  46.             decimal xArgument = 0;
  47.             xArgument = (-bArgument) / aArgument;
  48.             return xArgument;
  49.         }
  50.         static void Main()
  51.         {
  52.             Console.WriteLine("Enter: ");
  53.             Console.WriteLine("1 for reversing the number;");
  54.             Console.WriteLine("2 for calculating the average of a sequence;");
  55.             Console.WriteLine("3 for solving a linear equation a*x+b=0;");
  56.             Console.Write("Enter your choice here: ");
  57.             int taskChecker = int.Parse(Console.ReadLine());
  58.  
  59.             //If the customer choose to reversing the number;
  60.             if (taskChecker==1)
  61.             {
  62.                 Console.Write("Enter the number you want to reverse: ");
  63.                 BigInteger number = BigInteger.Parse(Console.ReadLine());
  64.                 BigInteger resultFromReversing=0;
  65.                 Console.WriteLine("Your number {0} in reversed type is {1}"
  66.                                   , number, ReversingNumber(number, resultFromReversing));
  67.             }
  68.  
  69.             //If the customer choose to find an average value in sequence, we have two cases:
  70.             //1) If the length of sequence is even;
  71.             if (taskChecker==2)
  72.             {
  73.                 Console.Write("Enter the length of the sequence: ");
  74.                 int lengthOfSequence = int.Parse(Console.ReadLine());
  75.  
  76.                 int[] sequenceOfNumbers = new int[lengthOfSequence];
  77.  
  78.                 for (int index = 0; index < sequenceOfNumbers.Length; index++)
  79.                 {
  80.                     sequenceOfNumbers[index] = int.Parse(Console.ReadLine());
  81.                 }
  82.  
  83.                 //1) If the length of sequence is even;
  84.  
  85.                 if (lengthOfSequence%2==0)
  86.                 {
  87.                     int averageElementInEvenSequence = 0;
  88.                     int secondAverageElementInEvenSequence = 0;
  89.                     int[] averageElementsInSequence = new int[2];
  90.                     int [] array=FindAverageElementInEvenSequence(sequenceOfNumbers, averageElementInEvenSequence
  91.                                                                   , secondAverageElementInEvenSequence, averageElementsInSequence);
  92.  
  93.                     Console.Write("The elements are two because the length is even number: ");
  94.                     for (int index = 0; index < array.Length; index++)
  95.                     {
  96.  
  97.                         Console.Write(array[index]);
  98.  
  99.                         if (index==0)
  100.                         {
  101.                             Console.Write(" and ");
  102.                         }
  103.  
  104.                     }
  105.                     Console.WriteLine(".");
  106.                    
  107.                 }
  108.  
  109.                 //2)Or if  the length of sequence is odd;
  110.  
  111.                 else if (lengthOfSequence%2!=0)
  112.                 {
  113.  
  114.                     int averageElementInOddSequence = 0;
  115.                     int resultFromOddSequence = FindAverageElementInOddSequence(sequenceOfNumbers, averageElementInOddSequence);
  116.                     Console.WriteLine("The average element in sequence is {0}. ",resultFromOddSequence);
  117.  
  118.                 }  
  119.  
  120.             }
  121.            
  122.             if (taskChecker==3)
  123.             {
  124.                
  125.                 Console.Write("Enter your linear equation a*x+b=0 here: ");
  126.                 string equation = Console.ReadLine();
  127.                
  128.                 //After we gave a string, we are trying to find 'a' and 'b' symbol and then to parse them
  129.                 //Into decimal values;
  130.                 int multiplyingSymbolIndex = equation.LastIndexOf("*");
  131.                 string aSymbol = equation.Substring(0,multiplyingSymbolIndex);
  132.                 decimal aValue = Convert.ToDecimal(aSymbol);
  133.                
  134.                 int plusSymbolIndex=equation.LastIndexOf("+");
  135.                 int equalSymbolIndex=equation.LastIndexOf("=");
  136.                 int lengthOfBNumber=equalSymbolIndex-plusSymbolIndex-1;
  137.                 string bSymbol = equation.Substring(plusSymbolIndex+1,lengthOfBNumber);
  138.                 decimal bValue = Convert.ToDecimal(bSymbol);
  139.  
  140.                 decimal resultOfLinearEquation = LinearEquation(aValue, bValue);
  141.  
  142.                 Console.WriteLine("The result from linear equation is {0}. ", resultOfLinearEquation);
  143.  
  144.             }
  145.  
  146.             if ((taskChecker!=1) || (taskChecker!=2) || (taskChecker!=3))
  147.             {
  148.                 Console.WriteLine("Please enter a valid number!!");
  149.             }
  150.  
  151.  
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement