Advertisement
VladSmirN

лаба 5

Dec 2nd, 2021
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5.  
  6. namespace lab5
  7. {
  8.     class Program
  9.     {
  10.         static int N = 0;
  11.         static Random rnd = new Random();
  12.         static int kol = 0;
  13.         static int nmb = 1;
  14.         static void AddL(List<int> list, int n,int M=10)
  15.         {
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 list.Add(rnd.Next(0, M));
  19.             }
  20.         }
  21.         static void PrintL(List<int> list, int n)
  22.         {
  23.             for (int i = 0; i < list.Count; i++)
  24.             {
  25.                 Console.Write(list[i] + " ");
  26.  
  27.             }
  28.             Console.WriteLine();
  29.         }
  30.        
  31.        
  32.         static void Write_text(string name) //заполнение файла текстом
  33.         {
  34.             try
  35.             {
  36.                 using (StreamWriter f = new StreamWriter(name))
  37.                 {
  38.  
  39.                      
  40.                    
  41.  
  42.                         string buf = Console.ReadLine();
  43.  
  44.                         f.WriteLine(buf);
  45.  
  46.                  
  47.  
  48.                 }
  49.             }
  50.             catch (IOException e)
  51.             {
  52.                 Console.WriteLine("Error:" + e.Message);
  53.                 return;
  54.             }
  55.         }
  56.         static HashSet<char> Find_Text(string name ) //чтение из файла
  57.         {
  58.             HashSet<char> hs = new HashSet<char>();
  59.             try
  60.             {
  61.                
  62.                 using (StreamReader f = new StreamReader(name))
  63.                 {
  64.  
  65.                     int i = 0;
  66.                     string s;
  67.                     s = f.ReadLine();
  68.                     string[] splitLine = s.Split(' ');
  69.  
  70.                     for(int j=1 ;j<= splitLine.Count(); ++j)
  71.                     {
  72.                         if (j % 2 == 0)
  73.                         {
  74.                             for (int k = 0; k < splitLine[j-1].Count(); ++k)
  75.                             {
  76.                                 hs.Add(splitLine[j - 1][k]);
  77.                             }
  78.                         }
  79.  
  80.                     }
  81.                
  82.  
  83.                 }
  84.                 return hs;
  85.             }
  86.             catch (IOException e)
  87.             {
  88.                 Console.WriteLine("Error:" + e.Message);
  89.                 return hs;
  90.             }
  91.         }
  92.          
  93.         static void Write_Text_2(string name) //заполнение файла текстом
  94.         {
  95.             try
  96.             {
  97.                 int N = 0;
  98.                 using (StreamWriter f = new StreamWriter(name))
  99.                 {
  100.  
  101.                     Console.WriteLine("Введите количество студентов");
  102.                     string buf = Console.ReadLine();
  103.                     //f.WriteLine(buf);
  104.                     N = int.Parse(buf);
  105.                     Console.WriteLine("Заполнение файла");
  106.                     for (int i = 1; i <= N; i++)
  107.                     {
  108.  
  109.                         buf = Console.ReadLine();
  110.  
  111.                         f.WriteLine(buf);
  112.  
  113.                     }
  114.  
  115.                 }
  116.                 return  ;
  117.             }
  118.             catch (IOException e)
  119.             {
  120.                 Console.WriteLine("Error:" + e.Message);
  121.                 return  ;
  122.             }
  123.         }
  124.         static SortedList<string,bool> Find_Text_2(string name ) //чтение из файла
  125.         {
  126.             SortedList<string, bool>  sl = new SortedList<string, bool>() ;
  127.             try
  128.             {
  129.  
  130.                 using (StreamReader f = new StreamReader(name))
  131.                 {
  132.  
  133.                     int i = 0;
  134.                     string s;
  135.                     //string buf = Console.ReadLine();
  136.                      
  137.                     //int N = int.Parse(buf);
  138.                     while ((s = f.ReadLine()) != null  )
  139.                     {
  140.                          
  141.                         string[] splitLine = s.Split(' ');
  142.                         int a =  int.Parse(splitLine[2]);
  143.                         int b = int.Parse(splitLine[3]);
  144.                         int c = int.Parse(splitLine[4]);
  145.                         if(a>30 && b>30 && c >30 && a + b + c > 140)
  146.                         {
  147.                             sl.Add(splitLine[0] + " " + splitLine[1], true);
  148.                         }
  149.                          
  150.                     }
  151.  
  152.                 }
  153.                 return sl;
  154.             }
  155.             catch (IOException e)
  156.             {
  157.                 Console.WriteLine("Error:" + e.Message);
  158.                 return sl;
  159.             }
  160.         }
  161.  
  162.  
  163.      
  164.  
  165.  
  166.         static void task1()
  167.         {
  168.             List<int> list = new List<int>();
  169.             Console.WriteLine("Введите n");
  170.             string buf = Console.ReadLine();
  171.             int n = int.Parse(buf);
  172.  
  173.             AddL(list, n);
  174.             PrintL(list, list.Count);
  175.  
  176.             list.Reverse();
  177.  
  178.             PrintL(list, list.Count);
  179.         }
  180.  
  181.         static void task2()
  182.         {
  183.             List<int> list = new List<int>();
  184.             Console.WriteLine("Введите n");
  185.             string buf = Console.ReadLine();
  186.             int n = int.Parse(buf);
  187.  
  188.             AddL(list, n);
  189.             PrintL(list, list.Count);
  190.  
  191.             Console.WriteLine("Введите E");
  192.             buf = Console.ReadLine();
  193.             int E = int.Parse(buf);
  194.  
  195.             Console.WriteLine("Введите F");
  196.             buf = Console.ReadLine();
  197.             int F = int.Parse(buf);
  198.             if (E == F)
  199.             {
  200.                 Console.WriteLine("E==F");
  201.                 return ;
  202.             }
  203.  
  204.             int offset =0;
  205.              while(offset <= list.Count && list.IndexOf(E, offset) !=-1)
  206.             {
  207.  
  208.                 int i = list.IndexOf(E, offset);
  209.                 list.Insert(i, F);
  210.                 list.Insert(i + 2, F);
  211.                 offset = i + 2;
  212.  
  213.             }
  214.             PrintL(list, list.Count);
  215.         }
  216.  
  217.         static void task3()
  218.         {
  219.              
  220.             List<List<int>>books  =new List<List<int>>();
  221.  
  222.             Console.WriteLine("Введите количество книг");
  223.             string buf = Console.ReadLine();
  224.             int M = int.Parse(buf);
  225.  
  226.             Console.WriteLine("Введите количество читателей");
  227.             buf = Console.ReadLine();
  228.             int N = int.Parse(buf);
  229.  
  230.  
  231.             for(int i = 0; i < M; ++i)
  232.             {
  233.                 List<int> book = new List<int>();
  234.                 AddL(book, rnd.Next(0, 20), N);
  235.                 books.Add(book);
  236.                 PrintL(book, book.Count);
  237.             }
  238.             HashSet<int> libary = new HashSet<int>();
  239.              
  240.             for (int i = 0; i < M; ++i)
  241.             {
  242.                 for (int j = 0; j < books[i].Count; ++j)
  243.                 {
  244.                     libary.Add(books[i][j]);
  245.                 }
  246.  
  247.                 if(libary.Count == N)
  248.                 {
  249.                     Console.WriteLine("Книгу под номером " +i+ " прочитали все ");
  250.                 }else
  251.                 if (libary.Count == 0)
  252.                 {
  253.                     Console.WriteLine("Книгу под номером " + i + " никто не прочитал ");
  254.                 }else
  255.                 {
  256.                     Console.WriteLine("Книгу под номером " + i + " прочитали часть четатилей ");
  257.                 }
  258.                 libary.Clear();
  259.             }
  260.         }
  261.         static void task4()
  262.         {
  263.             Write_text("task4.dat");
  264.             HashSet<char> hs = Find_Text("task4.dat");
  265.             List<char> sortedList = new List<char>(hs);
  266.             sortedList.Sort();
  267.             foreach (var item in sortedList)
  268.                 Console.WriteLine(item);
  269.         }
  270.         static void task5()
  271.         {
  272.  
  273.  
  274.              
  275.  
  276.              
  277.             Write_Text_2("task5.dat");
  278.             SortedList<string, bool> sl = Find_Text_2("task5.dat" );
  279.  
  280.             foreach (var item in sl)
  281.                 Console.WriteLine(item.Key);
  282.         }
  283.         static void Main(string[] args)
  284.         {
  285.             task1();
  286.             task2();
  287.             task3();
  288.             task4();
  289.             task5();
  290.            
  291.  
  292.         }
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement