Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class LexicographicalArrayCompare
- {
- static byte GetArraySize(string arrayNum)
- {
- bool noError = false;
- byte arraySize = 0;
- do
- {
- Console.Write("Enter size of the {0} array: ", arrayNum);
- noError = byte.TryParse(Console.ReadLine(), out arraySize);
- if (!noError || arraySize == 0)
- {
- Console.WriteLine("incorrect size (probably symbol or 0 entered)!");
- Console.WriteLine("Try again <press Enter>...");
- Console.ReadLine();
- Console.Clear();
- noError = false;
- }
- } while (!noError);
- return arraySize;
- }
- static char[] GetArrayContent(string arrayNum, int size)
- {
- char[] arrayOutput = new char[size];
- string lettersInput = "";
- bool noError = false;
- int counter = 0;
- do
- {
- Console.Clear();
- Console.WriteLine("Enter {0} array elements on one line without spaces: ", arrayNum);
- lettersInput = Console.ReadLine().ToUpper();
- for (int i = 0; i < lettersInput.Length; i++)
- {
- if (counter == size)
- {
- noError = true;
- break;
- }
- if (lettersInput[i] < 'A' || lettersInput[i] > 'Z' || lettersInput[i] == ' ')
- {
- Console.WriteLine("There was wrong input!");
- Console.WriteLine("Try again <press Enter>...");
- Console.ReadLine();
- Console.Clear();
- noError = false;
- break;
- }
- else
- {
- arrayOutput[counter] = Convert.ToChar(lettersInput[i]);
- noError = true;
- counter++;
- }
- }
- } while (!noError);
- return arrayOutput;
- }
- static Tuple<int, bool?> GetSmallerArray(int firstArray, int secondArray)
- {
- bool? longerArray = null; // null = equal; false = 2st array; true = 1st array
- if (firstArray > secondArray)
- {
- longerArray = true;
- }
- else if (firstArray < secondArray)
- {
- longerArray = false;
- }
- firstArray ^= secondArray;
- secondArray = firstArray ^ secondArray;
- firstArray ^= secondArray;
- Tuple<int, bool?> result = Tuple.Create(secondArray, longerArray);
- return result;
- }
- static void Main()
- {
- // Write a program that compares two char arrays lexicographically (letter by letter).
- Console.Title = "Compare two char arrays lexicographycaly";
- char[] arrayOne = new char[GetArraySize("first")];
- char[] arrayTwo = new char[GetArraySize("second")];
- arrayOne = GetArrayContent("first", arrayOne.Length);
- arrayTwo = GetArrayContent("second", arrayTwo.Length);
- //compare lexicographically both arrays
- Tuple<int, bool?> biggerArray = GetSmallerArray(arrayOne.Length, arrayTwo.Length);
- for (int i = 0; i < biggerArray.Item1; i++)
- {
- // compare letter by letter
- // if difference -> array with smaller letter wins
- if (arrayOne[i] < arrayTwo[i])
- {
- Console.WriteLine("First Array is lexicographicaly first.");
- break;
- }
- else if (arrayOne[i] > arrayTwo[i])
- {
- Console.WriteLine("Second Array is lexicographicaly first.");
- break;
- }
- // if we are that last index and no diferences, we check length of arrays:
- // - if equal -> both wins - else the smaller wins
- if (i + 1 == biggerArray.Item1 && !biggerArray.Item2.HasValue)
- {
- Console.WriteLine("Both Arrays are lexicographicaly equal.");
- break;
- }
- else if (i + 1 == biggerArray.Item1 && biggerArray.Item2.Value)
- {
- Console.WriteLine("Second Array is first lexicographicaly.");
- break;
- }
- else if (i + 1 == biggerArray.Item1 && !biggerArray.Item2.Value)
- {
- Console.WriteLine("First Array is first lexicographicaly.");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement