Advertisement
RMarK0

Untitled

Oct 24th, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     class Employee : IComparable<Employee>
  11.     {
  12.         public string LastName;
  13.         public string Position;
  14.         public int PhoneNumber;
  15.  
  16.         public Employee(string LastName, string Position, int PhoneNumber)
  17.         {
  18.             this.LastName = LastName;
  19.             this.Position = Position;
  20.             this.PhoneNumber = PhoneNumber;
  21.         }
  22.  
  23.         public int CompareTo(Employee other)
  24.         {
  25.             return String.Compare(this.LastName, other.LastName);
  26.         }
  27.     }
  28.  
  29.     class Program
  30.     {
  31.         static int SearchPhoneByName(Employee[] array, int searchedValue, int first, int last)
  32.         {
  33.             if (first > last)
  34.             {
  35.                 return -1;
  36.             }
  37.             var middle = (first + last) / 2;
  38.             var middleValue = array[middle].PhoneNumber;
  39.  
  40.             if (middleValue == searchedValue)
  41.             {
  42.                 return middle;
  43.             }
  44.             else
  45.             {
  46.                 if (middleValue > searchedValue)
  47.                 {
  48.                     return SearchPhoneByName(array, searchedValue, first, middle - 1);
  49.                 }
  50.                 else
  51.                 {
  52.                     return SearchPhoneByName(array, searchedValue, middle + 1, last);
  53.                 }
  54.             }
  55.         }
  56.  
  57.         static void SortByAlphabet(ref Employee[] array)
  58.         {
  59.             for (int i = 0; i < array.Length-1; i++)
  60.             {
  61.                 for (int j = i + 1; j < array.Length; j++)
  62.                 {
  63.                     if (array[i].CompareTo(array[j]) > 0)
  64.                     {
  65.                         Employee temp = array[i];
  66.                         array[i] = array[j];
  67.                         array[j] = temp;
  68.                     }
  69.                 }
  70.             }
  71.  
  72.         }
  73.  
  74.         static void Main(string[] args)
  75.         {
  76.             string PathToSource = @"C:\Программа\Phone_Memory.txt";
  77.             string PathToOutput = @"C:\Программа\Output.txt";
  78.  
  79.             int employyesCount = 0;
  80.            
  81.             using (StreamReader strReader = new StreamReader(PathToSource))
  82.             {
  83.                 string line;
  84.                 while ((line = strReader.ReadLine()) != null)
  85.                 {
  86.                     employyesCount++;
  87.                 }
  88.             }
  89.  
  90.             employyesCount /= 3;
  91.             Employee[] phoneMemory = new Employee[employyesCount];
  92.  
  93.             Console.WriteLine($"{employyesCount} работников записано в телефоне");
  94.  
  95.             using (StreamReader strReader = new StreamReader(PathToSource))
  96.             {
  97.                 for (int i = 0; i < employyesCount; i++)
  98.                 {
  99.                     string name = strReader.ReadLine();
  100.                     string position = strReader.ReadLine();
  101.                     int phoneNumber = int.Parse(strReader.ReadLine());
  102.                     phoneMemory[i] = new Employee(name, position, phoneNumber);
  103.                 }
  104.             }
  105.             SortByAlphabet(ref phoneMemory);
  106.  
  107.             string NameForSearch = Console.ReadLine();
  108.             int SearchPhone = 0;
  109.             for (int i = 0; i < employyesCount; i++)
  110.             {
  111.                 if (phoneMemory[i].LastName == NameForSearch)
  112.                     SearchPhone = phoneMemory[i].PhoneNumber;
  113.             }
  114.  
  115.             var SearchResult = SearchPhoneByName(phoneMemory, SearchPhone, 0, phoneMemory.Length - 1);
  116.             using (StreamWriter strWriter = new StreamWriter(PathToOutput, true))
  117.             {
  118.                 if (SearchResult < 0)
  119.                     strWriter.WriteLine("Номер не найден");
  120.                 else
  121.                     strWriter.WriteLine("Номер найден. Индекс элемента со значением {0} равен {1}", SearchPhone, SearchResult);
  122.             }
  123.             Console.Read();
  124.         }
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement