Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace NumberInputAndSorting
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- const int bottomBorder = 1;
- const int topBorder = 20;
- static void Main(string[] args)
- {
- Dictionary<int, uint> numbers = new Dictionary<int, uint>();
- int input = 0;
- string command = string.Empty;
- bool inputIsValid = false;
- while (true)
- {
- inputIsValid = GetValidInteger(bottomBorder, topBorder, out input, out command);
- if (!inputIsValid)
- {
- switch (command)
- {
- case "stop":
- {
- var result = numbers.OrderByDescending(x => x.Value);
- foreach (var number in result)
- {
- Console.WriteLine("{0} -> {1}", number.Key, number.Value);
- }
- return;
- }
- break;
- default:
- {
- return;
- }
- }
- }
- else
- {
- if (numbers.ContainsKey(input))
- {
- numbers[input]++;
- }
- else
- {
- numbers.Add(input, 1);
- }
- }
- }
- }
- static bool GetValidInteger(int bottomBorder, int topBorder, out int number, out string command)
- {
- int inputNumber = 0;
- bool isInBorders = false;
- number = 0;
- command = string.Empty;
- do
- {
- string userInput = string.Empty;
- bool isValidInteger = false;
- Console.Write("Please input integer in diapason {0} - {1}, 'stop' to calculate results or 'exit' to close program: ");
- userInput = Console.ReadLine();
- if (userInput == "stop" || userInput == "exit")
- {
- command = userInput;
- return false;
- }
- else
- {
- isValidInteger = int.TryParse(userInput, out inputNumber);
- isInBorders = isValidInteger && (inputNumber >= bottomBorder && inputNumber <= topBorder);
- }
- } while (!isInBorders);
- number = inputNumber;
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement