Advertisement
cherokee

C# Part 2 - Arrays - Exercise 3

Jan 6th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LexicographicalArrayCompare
  5. {
  6.     static byte GetArraySize(string arrayNum)
  7.     {
  8.         bool noError = false;
  9.         byte arraySize = 0;
  10.         do
  11.         {
  12.             Console.Write("Enter size of the {0} array: ", arrayNum);
  13.             noError = byte.TryParse(Console.ReadLine(), out arraySize);
  14.             if (!noError || arraySize == 0)
  15.             {
  16.                 Console.WriteLine("incorrect size (probably symbol or 0 entered)!");
  17.                 Console.WriteLine("Try again <press Enter>...");
  18.                 Console.ReadLine();
  19.                 Console.Clear();
  20.                 noError = false;
  21.             }
  22.         } while (!noError);
  23.         return arraySize;
  24.     }
  25.  
  26.     static char[] GetArrayContent(string arrayNum, int size)
  27.     {
  28.         char[] arrayOutput = new char[size];
  29.         string lettersInput = "";
  30.         bool noError = false;
  31.         int counter = 0;
  32.         do
  33.         {
  34.             Console.Clear();
  35.             Console.WriteLine("Enter {0} array elements on one line without spaces: ", arrayNum);
  36.             lettersInput = Console.ReadLine().ToUpper();
  37.  
  38.             for (int i = 0; i < lettersInput.Length; i++)
  39.             {
  40.                 if (counter == size)
  41.                 {
  42.                     noError = true;
  43.                     break;
  44.                 }
  45.                 if (lettersInput[i] < 'A' || lettersInput[i] > 'Z' || lettersInput[i] == ' ')
  46.                 {
  47.                     Console.WriteLine("There was wrong input!");
  48.                     Console.WriteLine("Try again <press Enter>...");
  49.                     Console.ReadLine();
  50.                     Console.Clear();
  51.                     noError = false;
  52.                     break;
  53.                 }
  54.                 else
  55.                 {
  56.                     arrayOutput[counter] = Convert.ToChar(lettersInput[i]);
  57.                     noError = true;
  58.                     counter++;
  59.                 }
  60.  
  61.             }
  62.         } while (!noError);
  63.         return arrayOutput;
  64.     }
  65.    
  66.     static Tuple<int, bool?> GetSmallerArray(int firstArray, int secondArray)
  67.     {
  68.         bool? longerArray = null; // null = equal; false = 2st array; true = 1st array
  69.         if (firstArray > secondArray)
  70.         {
  71.             longerArray = true;
  72.         }
  73.         else if (firstArray < secondArray)
  74.         {
  75.             longerArray = false;
  76.         }
  77.         firstArray ^= secondArray;
  78.         secondArray = firstArray ^ secondArray;
  79.         firstArray ^= secondArray;
  80.         Tuple<int, bool?> result = Tuple.Create(secondArray, longerArray);
  81.         return result;
  82.     }
  83.    
  84.     static void Main()
  85.     {
  86.         // Write a program that compares two char arrays lexicographically (letter by letter).
  87.         Console.Title = "Compare two char arrays lexicographycaly";
  88.         char[] arrayOne = new char[GetArraySize("first")];
  89.         char[] arrayTwo = new char[GetArraySize("second")];
  90.         arrayOne = GetArrayContent("first", arrayOne.Length);
  91.         arrayTwo = GetArrayContent("second", arrayTwo.Length);
  92.         //compare lexicographically both arrays
  93.         Tuple<int, bool?> biggerArray = GetSmallerArray(arrayOne.Length, arrayTwo.Length);
  94.         for (int i = 0; i < biggerArray.Item1; i++)
  95.         {
  96.             // compare letter by letter
  97.             // if difference -> array with smaller letter wins
  98.             if (arrayOne[i] < arrayTwo[i])
  99.             {
  100.                 Console.WriteLine("First Array is lexicographicaly first.");
  101.                 break;
  102.             }
  103.             else if (arrayOne[i] > arrayTwo[i])
  104.             {
  105.                 Console.WriteLine("Second Array is lexicographicaly first.");
  106.                 break;
  107.             }
  108.             // if we are that last index and no diferences, we check length of arrays:
  109.             //  - if equal -> both wins - else the smaller wins
  110.             if (i + 1 == biggerArray.Item1 && !biggerArray.Item2.HasValue)
  111.             {
  112.                 Console.WriteLine("Both Arrays are lexicographicaly equal.");
  113.                 break;
  114.             }
  115.             else if (i + 1 == biggerArray.Item1 && biggerArray.Item2.Value)
  116.             {
  117.                 Console.WriteLine("Second Array is first lexicographicaly.");
  118.                 break;
  119.             }
  120.             else if (i + 1 == biggerArray.Item1 && !biggerArray.Item2.Value)
  121.             {
  122.                 Console.WriteLine("First Array is first lexicographicaly.");
  123.                 break;
  124.             }
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement