Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Employee : IComparable<Employee>
- {
- public string LastName;
- public string Position;
- public int PhoneNumber;
- public Employee(string LastName, string Position, int PhoneNumber)
- {
- this.LastName = LastName;
- this.Position = Position;
- this.PhoneNumber = PhoneNumber;
- }
- public int CompareTo(Employee other)
- {
- return String.Compare(this.LastName, other.LastName);
- }
- }
- class Program
- {
- static int SearchPhoneByName(Employee[] array, int searchedValue, int first, int last)
- {
- if (first > last)
- {
- return -1;
- }
- var middle = (first + last) / 2;
- var middleValue = array[middle].PhoneNumber;
- if (middleValue == searchedValue)
- {
- return middle;
- }
- else
- {
- if (middleValue > searchedValue)
- {
- return SearchPhoneByName(array, searchedValue, first, middle - 1);
- }
- else
- {
- return SearchPhoneByName(array, searchedValue, middle + 1, last);
- }
- }
- }
- static void SortByAlphabet(ref Employee[] array)
- {
- for (int i = 0; i < array.Length-1; i++)
- {
- for (int j = i + 1; j < array.Length; j++)
- {
- if (array[i].CompareTo(array[j]) > 0)
- {
- Employee temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- }
- }
- static void Main(string[] args)
- {
- string PathToSource = @"C:\Программа\Phone_Memory.txt";
- string PathToOutput = @"C:\Программа\Output.txt";
- int employyesCount = 0;
- using (StreamReader strReader = new StreamReader(PathToSource))
- {
- string line;
- while ((line = strReader.ReadLine()) != null)
- {
- employyesCount++;
- }
- }
- employyesCount /= 3;
- Employee[] phoneMemory = new Employee[employyesCount];
- Console.WriteLine($"{employyesCount} работников записано в телефоне");
- using (StreamReader strReader = new StreamReader(PathToSource))
- {
- for (int i = 0; i < employyesCount; i++)
- {
- string name = strReader.ReadLine();
- string position = strReader.ReadLine();
- int phoneNumber = int.Parse(strReader.ReadLine());
- phoneMemory[i] = new Employee(name, position, phoneNumber);
- }
- }
- SortByAlphabet(ref phoneMemory);
- string NameForSearch = Console.ReadLine();
- int SearchPhone = 0;
- for (int i = 0; i < employyesCount; i++)
- {
- if (phoneMemory[i].LastName == NameForSearch)
- SearchPhone = phoneMemory[i].PhoneNumber;
- }
- var SearchResult = SearchPhoneByName(phoneMemory, SearchPhone, 0, phoneMemory.Length - 1);
- using (StreamWriter strWriter = new StreamWriter(PathToOutput, true))
- {
- if (SearchResult < 0)
- strWriter.WriteLine("Номер не найден");
- else
- strWriter.WriteLine("Номер найден. Индекс элемента со значением {0} равен {1}", SearchPhone, SearchResult);
- }
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement