Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. /*
  2.  Дисциплина: Программирование
  3.  Студент: Пак Марк Михайлович
  4.  17.10.19
  5.  Вариант 16
  6.  */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.IO;
  13.  
  14. namespace Task
  15. {
  16.     class Program
  17.     {
  18.         /// <summary>
  19.         /// Метод для преобразовании строки в int
  20.         /// </summary>
  21.         /// <param name="userInput">Пользовательская строка</param>
  22.         /// <returns>int значение</returns>
  23.         static int GetIntInput(string userInput)
  24.         {
  25.             if (!int.TryParse(userInput, out int n))
  26.                 throw new InvalidCastException();
  27.             return n;
  28.         }
  29.  
  30.         /// <summary>
  31.         /// Метод для преобразования строки в int с ограничениями
  32.         /// </summary>
  33.         /// <param name="userInput">Пользовательская строка</param>
  34.         /// <param name="minBorder">Нижняя граница</param>
  35.         /// <param name="maxBorder">Верхняя граница</param>
  36.         /// <returns>int значение</returns>
  37.         static int GetIntInput(string userInput, int minBorder,
  38.             int maxBorder = int.MaxValue)
  39.         {
  40.             if (!int.TryParse(userInput, out int n))
  41.                 throw new InvalidCastException();
  42.             else if (n < minBorder || n > maxBorder)
  43.                 throw new ArgumentOutOfRangeException();
  44.             return n;
  45.  
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Весь пользовательский ввод
  50.         /// </summary>
  51.         /// <returns>Индикатор ошибки при вводе</returns>
  52.         static bool UserInput(out int arraySize, out int needSize, out int[] arr)
  53.         {
  54.             try
  55.             {
  56.                 Console.WriteLine("Введите размер массива: ");
  57.                 arraySize = GetIntInput(Console.ReadLine(), 1);
  58.                 arr = new int[arraySize];
  59.                 for (int i = 0; i < arraySize; i++)
  60.                 {
  61.                     arr[i] = GetIntInput(Console.ReadLine(), 0);
  62.                 }
  63.                 needSize = GetIntInput(Console.ReadLine(), 1);
  64.                 if (needSize > arraySize) throw new IndexOutOfRangeException();
  65.                 return true;
  66.             }
  67.             catch (IndexOutOfRangeException e)
  68.             {
  69.                 Console.WriteLine(e.Message);
  70.             }
  71.             catch (InvalidCastException e)
  72.             {
  73.                 Console.WriteLine(e.Message);
  74.             }
  75.             catch (ArgumentOutOfRangeException e)
  76.             {
  77.                 Console.WriteLine(e.Message);
  78.             }
  79.             arraySize = 0;
  80.             arr = null;
  81.             needSize = 0;
  82.             return false;
  83.         }
  84.  
  85.         static void Main(string[] args)
  86.         {
  87.             do
  88.             {
  89.                 Console.Clear();
  90.                 // Размер массива.
  91.                 int arraySize;
  92.                 // Количество выводных элементов.
  93.                 int needSize;
  94.                 int[] arr;
  95.                 // Пользовательский ввод.
  96.                 do { } while (!UserInput(out arraySize, out needSize, out arr));
  97.                 Console.WriteLine("Ввод успешно завершен...");
  98.                 // Строка содержит последние K элементов.
  99.                 string response = string.Empty;
  100.                 for (int i = arraySize - needSize; i < arraySize; i++)
  101.                 {
  102.                     // Работа с массивом.
  103.                     response += arr[i];
  104.                     response += " ";
  105.                 }
  106.                 // Запись в файл.
  107.                 try
  108.                 {
  109.                     string path = @"../../../output.txt";
  110.                     using (StreamWriter sw = new StreamWriter(path, true))
  111.                     {
  112.                         response += "\n";
  113.                         sw.Write(response);
  114.                     }
  115.                     Console.WriteLine("Запись успешно завершена...");
  116.                 }
  117.                 catch (FileNotFoundException e)
  118.                 {
  119.                     Console.WriteLine(e.Message);
  120.                 }
  121.                 catch (IOException e)
  122.                 {
  123.                     Console.WriteLine(e.Message);
  124.                 }
  125.                 Console.WriteLine("Для выхода нажмите ESC...");
  126.             } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement