Advertisement
g-stoyanov

OrderGivenNumbersByOccuranceDemoUpgrade

Feb 25th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. namespace NumberInputAndSorting
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     class Program
  8.     {
  9.         const int bottomBorder = 1;
  10.         const int topBorder = 20;
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<int, uint> numbers = new Dictionary<int, uint>();
  14.             int input = 0;
  15.             string command = string.Empty;
  16.             bool inputIsValid = false;
  17.  
  18.             while (true)
  19.             {
  20.                 inputIsValid = GetValidInteger(bottomBorder, topBorder, out input, out command);
  21.                 if (!inputIsValid)
  22.                 {
  23.                     switch (command)
  24.                     {
  25.                         case "stop":
  26.                             {
  27.                                 var result = numbers.OrderByDescending(x => x.Value);
  28.  
  29.                                 foreach (var number in result)
  30.                                 {
  31.                                     Console.WriteLine("{0} -> {1}", number.Key, number.Value);
  32.                                 }
  33.  
  34.                                 return;
  35.                             }
  36.                             break;
  37.                         default:
  38.                             {
  39.                                 return;
  40.                             }
  41.                     }
  42.                 }
  43.                 else
  44.                 {
  45.                     if (numbers.ContainsKey(input))
  46.                     {
  47.                         numbers[input]++;
  48.                     }
  49.                     else
  50.                     {
  51.                         numbers.Add(input, 1);
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.  
  57.         static bool GetValidInteger(int bottomBorder, int topBorder, out int number, out string command)
  58.         {
  59.             int inputNumber = 0;
  60.             bool isInBorders = false;
  61.             number = 0;
  62.             command = string.Empty;
  63.             do
  64.             {
  65.                 string userInput = string.Empty;
  66.                 bool isValidInteger = false;
  67.                 Console.Write("Please input integer in diapason {0} - {1}, 'stop' to calculate results or 'exit' to close program: ");
  68.                 userInput = Console.ReadLine();
  69.                 if (userInput == "stop" || userInput == "exit")
  70.                 {
  71.                     command = userInput;
  72.                     return false;
  73.                 }
  74.                 else
  75.                 {
  76.                     isValidInteger = int.TryParse(userInput, out inputNumber);
  77.                     isInBorders = isValidInteger && (inputNumber >= bottomBorder && inputNumber <= topBorder);
  78.                 }
  79.  
  80.  
  81.             } while (!isInBorders);
  82.  
  83.             number = inputNumber;
  84.             return true;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement