MaximCherchuk

First Laba

Sep 6th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using System.Security.Cryptography.X509Certificates;
  8.  
  9. namespace ConsoleApplication
  10. {
  11.     class Program
  12.     {
  13.        
  14.         private static List<Time> timelist = new List<Time>();
  15.         private static bool format = false;
  16.        
  17.         static void Main(string[] args)
  18.         {
  19.             while (true)
  20.             {
  21.                 var commandLine = Console.ReadLine();
  22.                 var arguments = commandLine.Split(' ');
  23.                 var command = arguments[0];
  24.                 if (command.Length == 0)
  25.                     continue;
  26.                
  27.                 var method = typeof(Program).GetMethod(command);
  28.                 if (method == null)
  29.                 {
  30.                     Console.WriteLine("Such method does not exist");
  31.                     Console.WriteLine("Type \"help\" to get help\n");
  32.                     continue;
  33.                 }
  34.                 method.Invoke(null, new object[]{SubArray(arguments, 1)});
  35.             }
  36.         }
  37.  
  38.         public static T[] SubArray<T>(T[] data, int index)
  39.         {
  40.             var result = new T[data.Length-1];
  41.             Array.Copy(data, index, result, 0, data.Length-1);
  42.             return result;
  43.         }
  44.        
  45.         public static void Help(object obj)
  46.         {
  47.             Console.WriteLine(
  48.                 "1) help – вывод списка поддерживаемых команд\n" +
  49.                 "2) add – добавить элемент\n" +
  50.                 "3) remove all/n – удалить элемент n либо очистить весь список\n" +
  51.                 "4) list [i] [n] – вывести на печать т элементов, начиная с i\n" +
  52.                 "5) sort – отсортировать по заданному алгоритму (см. варианты)\n" +
  53.                 "6) доп. команда (см. варианты)\n" +
  54.                 "7) exit и quit – завершение работы\n"
  55.             );
  56.         }
  57.  
  58.         public static void Quit(object obj)
  59.         {
  60.             Environment.Exit(0);
  61.         }
  62.        
  63.         public static void Exit(object obj)
  64.         {
  65.             Environment.Exit(0);
  66.         }
  67.  
  68.         public static void Add(object obj)
  69.         {
  70.             var parameters = (string[]) obj;
  71.             var t = double.Parse(parameters[0]);
  72.             var x = double.Parse(parameters[1]);
  73.             timelist.Add(new Time(t, x));
  74.         }
  75.  
  76.         public static void Remove(object obj)
  77.         {
  78.             var parameters = (string[]) obj;
  79.             if (parameters[0] == "all")
  80.             {
  81.                 timelist.Clear();
  82.             }
  83.             else
  84.             {
  85.                 timelist.RemoveAt(int.Parse(parameters[0]) - 1);
  86.             }
  87.         }
  88.  
  89.         public static void List(object obj)
  90.         {
  91.             var parameters = (string[]) obj;
  92.             switch (parameters.Length)
  93.             {
  94.                 case 0:
  95.                     foreach (var timeItem in timelist)
  96.                     {
  97.                          Console.WriteLine(timeItem);  
  98.                     }
  99.                     break;
  100.                 case 1:
  101.                     var begin = int.Parse(parameters[0]) - 1;
  102.                     for (var i = begin; i < timelist.Count; ++i)
  103.                     {
  104.                         Console.WriteLine(timelist.ElementAt(i));
  105.                     }
  106.                     break;
  107.                 case 2:
  108.                     begin = int.Parse(parameters[0]) - 1;
  109.                     var end = int.Parse(parameters[1]) - 1;
  110.                     for (var i = 0; i < end; ++i)
  111.                     {
  112.                         if (begin + i >= timelist.Count)
  113.                         {
  114.                             break;
  115.                         }
  116.                         Console.WriteLine(timelist.ElementAt(begin + i));
  117.                     }
  118.                     break;
  119.             }
  120.         }
  121.  
  122.         public static void Sort(object obj)
  123.         {
  124.             timelist.Sort();
  125.         }
  126.  
  127.         public static void CountTrendTriangle(object obj)
  128.         {
  129.            
  130.         }
  131.  
  132.         public static void Format(object obj)
  133.         {
  134.             var fileFormat = (string) obj;
  135.             switch (fileFormat)
  136.             {
  137.                 case "short":
  138.                     format = false;
  139.                     break;
  140.                 case "full":
  141.                     format = true;
  142.                     break;
  143.                 default:
  144.                     Console.WriteLine(string.Format("There's no such an option: {}", fileFormat));
  145.                     break;
  146.             }
  147.         }
  148.  
  149.         public static void Save(object obj)
  150.         {
  151.             var filepath = (string) obj;
  152.             System.IO.Path.IsPathRooted(filepath);
  153.            
  154.         }
  155.     }
  156.  
  157.  
  158.     internal class Time : IComparable<Time>
  159.     {
  160.         private readonly double t;
  161.         private readonly double x;
  162.  
  163.         public Time(double t, double x)
  164.         {
  165.             this.t = t;
  166.             this.x = x;
  167.         }
  168.        
  169.         public override string ToString()
  170.         {
  171.             return string.Format("({0}, {1})", t, x);
  172.         }
  173.  
  174.         public int CompareTo(Time other)
  175.         {
  176.             if (ReferenceEquals(this, other)) return 0;
  177.             if (ReferenceEquals(null, other)) return 1;
  178.             var tComparison = t.CompareTo(other.t);
  179.             return tComparison != 0 ? tComparison : x.CompareTo(other.x);
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment