Advertisement
filin

laba_2_z_1_new

Oct 20th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Z_1
  8. {
  9.     class Program
  10.     {
  11.         enum Methods
  12.         {
  13.             While = 1,
  14.             DoWhile = 2,
  15.             For = 3
  16.         }
  17.         static void Main(string[] args)
  18.         {
  19.             Console.Title = "laba2_1";
  20.             Console.WriteLine("Программа выводит только положительные целые числа из диапазона от А до В (A<=В);");
  21.             string q;
  22.             do
  23.             {
  24.                 Int64 leftBorder, rightBorder;
  25.                 do
  26.                 {
  27.                     Console.Write("Введите начало диапазона: ");
  28.                     leftBorder = (Int64)Math.Ceiling(EnterDouble());
  29.                     Console.Write("Введите конец диапазона: ");
  30.                     rightBorder = (Int64)Math.Floor(EnterDouble());
  31.                     if (leftBorder < rightBorder)
  32.                     {
  33.                         Console.WriteLine("Выберите способ: \n1)While \n2)DoWhile \n3)For");
  34.                         Console.Write("Введите номер способа: ");
  35.                         Methods number = (Methods)EnterInt();
  36.                         switch (number)
  37.                         {
  38.                                 case Methods.While:
  39.                                     {
  40.                                         While(leftBorder, rightBorder);
  41.                                     }
  42.                                     break;
  43.                                 case Methods.DoWhile:
  44.                                     {
  45.                                         DoWhile(leftBorder, rightBorder);
  46.                                     }
  47.                                     break;
  48.                                 case Methods.For:
  49.                                     {
  50.                                         For(leftBorder, rightBorder);
  51.                                     }
  52.                                     break;
  53.                                 default:
  54.                                     {
  55.                                         Console.WriteLine("Способ не выбран");
  56.                                     }
  57.                                     break;
  58.                         }
  59.                     }
  60.                     else
  61.                     {
  62.                         Console.WriteLine("Недопустимое значение: a>b. Введите a и b заново");
  63.                     }
  64.                     CheckRange(rightBorder);
  65.                 } while (leftBorder > rightBorder);
  66.                 Console.WriteLine("Если хотите продолжить введите 1");
  67.                 q = Console.ReadLine();
  68.             }
  69.             while (q.Equals("1"));
  70.         }
  71.         static void While(Int64 a, Int64 b)
  72.         {
  73.             while (a <= b)
  74.             {
  75.                 if (a > 0)
  76.                 {
  77.                     Console.Write("{0} ", a);
  78.                 }
  79.                 a++;
  80.             }
  81.             Console.WriteLine();
  82.         }
  83.         static void DoWhile(Int64 a, Int64 b)
  84.         {
  85.             do
  86.             {
  87.                 if (a > 0)
  88.                 {
  89.                     Console.Write("{0} ", a);
  90.                 }
  91.                 a++;
  92.             }
  93.             while (a <= b);
  94.             Console.WriteLine();
  95.         }
  96.         static void For(Int64 a, Int64 b)
  97.         {
  98.             for (Int64 i = a; i <= b; i++)
  99.             {
  100.                 {
  101.                     if (i > 0)
  102.                     {
  103.                         Console.Write("{0} ", i);
  104.                     }
  105.                 }
  106.             }
  107.             Console.WriteLine();
  108.         }
  109.         static double EnterDouble()
  110.         {
  111.             double value;
  112.             bool result = false;
  113.             result = double.TryParse(Console.ReadLine(), out value);
  114.             if (result == false)
  115.             {
  116.                 do
  117.                 {
  118.                     Console.Write("Некорректные данные. Введите заново: ");
  119.                     result = double.TryParse(Console.ReadLine(), out value);
  120.                 }
  121.                 while (!result);
  122.             }
  123.             return value;
  124.         }
  125.         static int EnterInt()
  126.         {
  127.             int value;
  128.             bool result = false;
  129.             result = int.TryParse(Console.ReadLine(), out value);
  130.             if (result == false)
  131.             {
  132.                 do
  133.                 {
  134.                     Console.Write("Некорректные данные. Введите заново: ");
  135.                     result = int.TryParse(Console.ReadLine(), out value);
  136.                 }
  137.                 while (!result);
  138.             }
  139.             return value;
  140.         }
  141.         static void CheckRange(Int64 rightBorder)
  142.         {
  143.             if (rightBorder < 0)
  144.             {
  145.                 Console.WriteLine("В этом диапазоне нет положительных чисел");
  146.             }
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement