Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace DZFile5
  5. {
  6.     class MainClass
  7.     {
  8.         static string[] words = new string[0];
  9.         static int[] counts = new int[0];
  10.  
  11.         public static void Main(string[] args)
  12.         {
  13.             string dataPath = GetFilePath("Введите путь к файлу с данными:", true, false); ;
  14.             string[] lines = File.ReadAllLines(dataPath);
  15.             //
  16.  
  17.             for (int i = 0; i < lines.Length; i++)
  18.             {
  19.                 string line = lines[i];
  20.                 string word = GetName(line);
  21.                 int count = GetNum(line);
  22.  
  23.                 AddCount(word, count);
  24.             }
  25.  
  26.             //
  27.             string resultPath = GetFilePath("Введите путь куда сохранить результат:", false, true);
  28.             for (int i = 0; i < words.Length; i++)
  29.             {
  30.                 File.AppendAllText(resultPath, words[i] + " " + counts[i] + "\n");
  31.             }
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Добавляет пару в хранилище
  36.         /// </summary>
  37.         /// <param name="word"></param>
  38.         /// <param name="count"></param>
  39.         public static void AddElement(string word, int count)
  40.         {
  41.             string[] newWords = new string[words.Length + 1];
  42.             int[] newCounts = new int[counts.Length + 1];
  43.             for (int k = 0; k < words.Length; k++)
  44.             {
  45.                 newWords[k] = words[k];
  46.                 newCounts[k] = counts[k];
  47.             }
  48.  
  49.             newWords[newWords.Length - 1] = word;
  50.             newCounts[newCounts.Length - 1] = count;
  51.  
  52.             counts = newCounts;
  53.             words = newWords;
  54.         }
  55.  
  56.         /// <summary>
  57.         /// Добавляет количество в существующую пару или создаёт новую пару
  58.         /// </summary>
  59.         /// <param name="word">Слово</param>
  60.         /// <param name="count">Количество</param>
  61.         public static void AddCount(string word, int count)
  62.         {
  63.             int i = Find(word);
  64.             if (i != -1)
  65.             {
  66.                 counts[i] += count;
  67.             }
  68.             else
  69.             {
  70.                 AddElement(word, count);
  71.             }
  72.         }
  73.  
  74.         /// <summary>
  75.         /// Возвращает индекс в хранилище
  76.         /// </summary>
  77.         /// <param name="word">Искомое слово</param>
  78.         /// <returns></returns>
  79.         public static int Find(string word)
  80.         {
  81.             for (int i = 0; i < words.Length; i++)
  82.             {
  83.                 if (words[i] == word)
  84.                 {
  85.                     return i;
  86.                 }
  87.             }
  88.             return -1;
  89.         }
  90.        
  91.         /// <summary>
  92.         /// Запрашивает у пользователя путь к файлу
  93.         /// </summary>
  94.         /// <param name="message">Пригласительное сообщение</param>
  95.         /// <param name="isNeedExists">Если true то запрашивает путь пока не будет введён путь к существующему файлу</param>
  96.         /// <param name="isClearQuestion">Если true и выбран существующий файл, предлагает его очистить</param>
  97.         /// <returns></returns>
  98.         static string GetFilePath(string message, bool isNeedExists, bool isClearQuestion)
  99.         {
  100.             while (true)
  101.             {
  102.                 Console.WriteLine(message);
  103.                 string path = Console.ReadLine();
  104.                 if (File.Exists(path))
  105.                 {
  106.                     if (isClearQuestion)
  107.                     {
  108.                         Console.WriteLine("Хотите ли вы удалить содержимое файла? (Y/N)");
  109.                         string answer = Console.ReadLine();
  110.                         if(answer.ToUpper() == "Y")
  111.                         {
  112.                             File.Delete(path);
  113.                             File.Create(path).Close();
  114.                         }
  115.                     }
  116.                     return path;
  117.                 }
  118.                 else
  119.                 {
  120.                     if (isNeedExists)
  121.                     {
  122.                         Console.WriteLine("Нужно указать существующий файл");
  123.                     }
  124.                     else
  125.                     {
  126.                         return path;
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.  
  132.         static string GetName(string line)
  133.         {
  134.             string[] all = line.Split(' ');
  135.             string name = all[0];
  136.             return name;
  137.         }
  138.  
  139.         static int GetNum(string line)
  140.         {
  141.             string[] all = line.Split(' ');
  142.             int num = Convert.ToInt32(all[1]);
  143.             return num;
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement