Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace DZFile5
- {
- class MainClass
- {
- static string[] words = new string[0];
- static int[] counts = new int[0];
- public static void Main(string[] args)
- {
- string dataPath = GetFilePath("Введите путь к файлу с данными:", true, false); ;
- string[] lines = File.ReadAllLines(dataPath);
- //
- for (int i = 0; i < lines.Length; i++)
- {
- string line = lines[i];
- string word = GetName(line);
- int count = GetNum(line);
- AddCount(word, count);
- }
- //
- string resultPath = GetFilePath("Введите путь куда сохранить результат:", false, true);
- for (int i = 0; i < words.Length; i++)
- {
- File.AppendAllText(resultPath, words[i] + " " + counts[i] + "\n");
- }
- }
- /// <summary>
- /// Добавляет пару в хранилище
- /// </summary>
- /// <param name="word"></param>
- /// <param name="count"></param>
- public static void AddElement(string word, int count)
- {
- string[] newWords = new string[words.Length + 1];
- int[] newCounts = new int[counts.Length + 1];
- for (int k = 0; k < words.Length; k++)
- {
- newWords[k] = words[k];
- newCounts[k] = counts[k];
- }
- newWords[newWords.Length - 1] = word;
- newCounts[newCounts.Length - 1] = count;
- counts = newCounts;
- words = newWords;
- }
- /// <summary>
- /// Добавляет количество в существующую пару или создаёт новую пару
- /// </summary>
- /// <param name="word">Слово</param>
- /// <param name="count">Количество</param>
- public static void AddCount(string word, int count)
- {
- int i = Find(word);
- if (i != -1)
- {
- counts[i] += count;
- }
- else
- {
- AddElement(word, count);
- }
- }
- /// <summary>
- /// Возвращает индекс в хранилище
- /// </summary>
- /// <param name="word">Искомое слово</param>
- /// <returns></returns>
- public static int Find(string word)
- {
- for (int i = 0; i < words.Length; i++)
- {
- if (words[i] == word)
- {
- return i;
- }
- }
- return -1;
- }
- /// <summary>
- /// Запрашивает у пользователя путь к файлу
- /// </summary>
- /// <param name="message">Пригласительное сообщение</param>
- /// <param name="isNeedExists">Если true то запрашивает путь пока не будет введён путь к существующему файлу</param>
- /// <param name="isClearQuestion">Если true и выбран существующий файл, предлагает его очистить</param>
- /// <returns></returns>
- static string GetFilePath(string message, bool isNeedExists, bool isClearQuestion)
- {
- while (true)
- {
- Console.WriteLine(message);
- string path = Console.ReadLine();
- if (File.Exists(path))
- {
- if (isClearQuestion)
- {
- Console.WriteLine("Хотите ли вы удалить содержимое файла? (Y/N)");
- string answer = Console.ReadLine();
- if(answer.ToUpper() == "Y")
- {
- File.Delete(path);
- File.Create(path).Close();
- }
- }
- return path;
- }
- else
- {
- if (isNeedExists)
- {
- Console.WriteLine("Нужно указать существующий файл");
- }
- else
- {
- return path;
- }
- }
- }
- }
- static string GetName(string line)
- {
- string[] all = line.Split(' ');
- string name = all[0];
- return name;
- }
- static int GetNum(string line)
- {
- string[] all = line.Split(' ');
- int num = Convert.ToInt32(all[1]);
- return num;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement