Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace Lab5
- {
- class Log
- {
- public int LengthHash(string file, int start, int end)
- {
- if (start < 0 || end < 0)
- {
- return 0;
- }
- if (start == 0)
- {
- string temp = null;
- int i = 0;
- while (file[i++] != '#')
- {
- temp += file[i];
- }
- return temp.Length - 1;
- }
- else
- {
- int counter = 0;
- int counter2 = 0;
- int valueE = 0;
- while (counter != end)
- {
- if (file[valueE++] == '#' && counter != end)
- {
- counter++;
- }
- }
- int valueS = 0;
- while (counter2 != start)
- {
- if (file[valueS++] == '#' && counter2 != start)
- {
- counter2++;
- }
- }
- return valueE - valueS - 1;
- }
- }
- public void FileRead(string file)//вывод на экран содержимого любого файла
- {
- Console.WriteLine("Data file: {0}\n", file);
- string st;
- try
- {
- StreamReader sr = File.OpenText(file);
- while ((st = sr.ReadLine()) != null)
- {
- Console.WriteLine(st);
- }
- Console.WriteLine("\n");
- sr.Close();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- public void analisisFileUsers(string nameFile)//анализ действия выбранного пользователя
- {
- try
- {
- StreamReader sr = new StreamReader(nameFile);
- Console.Write("Анализ работы пользователя:");
- string user = Console.ReadLine();
- StreamWriter sw = new StreamWriter("user.txt");
- sw.WriteLine("пользователь:{0}", user);
- while (true)
- {
- string st = sr.ReadLine();
- if (st == null)
- break;
- if (st.StartsWith(user) == true)
- {
- sw.WriteLine(st);
- }
- }
- sw.Close();
- sr.Close();
- //анализ того, на какие сайты заходил выбранный пользователь, сколько было передано информации
- StreamReader userFile = new StreamReader("user.txt");
- int kol = 3;
- int kolRazd = 0;
- string x = "#";
- StreamWriter userSite = File.CreateText("userSite.txt");
- userSite.WriteLine("пользователь {0} посещал сайты:", user);
- while (true)
- {
- string str = userFile.ReadLine();
- if (str == null)
- break;
- //посещал сайты
- for (int i = 0; i < str.Length; i++)
- {
- if (str.Substring(i, 1) == x)
- {
- kolRazd++;
- if (kolRazd == kol)
- {
- userSite.WriteLine(str.Substring((i + 1), LengthHash(str, 3, 4)));
- kolRazd = 0;
- break;
- }
- }
- }
- }
- userFile.Close();
- userSite.Close();
- int kol2 = 4;
- int kolRazd2 = 0;
- int counter = 0;
- StreamReader userFile2 = new StreamReader("user.txt");
- StreamWriter userSite2 = File.CreateText("userSite2.txt");
- userSite2.WriteLine("пользователь {0} потратил трафик:", user);
- while (true)
- {
- string str2 = userFile2.ReadLine();
- if (str2 == null)
- break;
- //потрачено трафика
- for (int j = 0; j < str2.Length; j++)
- {
- if (str2.Substring(j, 1) == x)
- {
- kolRazd2++;
- if (kolRazd2 == kol2)
- {
- userSite2.WriteLine(str2.Substring((j + 1), LengthHash(str2, 4, 5)));
- counter += Convert.ToInt32(str2.Substring((j + 1), LengthHash(str2, 4, 5)));
- kolRazd2 = 0;
- break;
- }
- }
- }
- }
- userSite2.WriteLine("Summ: {0}", counter);
- userSite2.Close();
- userFile2.Close();
- }
- catch (Exception ex)
- {
- Console.WriteLine("Файл не найден\n{0}", ex.Message);
- }
- }
- public string nameFile()//имя файла
- {
- string nameFile = Console.ReadLine();
- return nameFile;
- }
- }
- class Program
- {
- static void Main()
- {
- Program Obj = new Program();
- Obj.Menu();
- }
- void Menu()
- {
- int i = 0;
- do
- {
- Console.Write("Меню:\n1) Печать из файла\n2) Анализ работы пользователя\n3) Выйти из программы\n\nВыберете пункт: ");
- try
- {
- i = Convert.ToInt32(Console.ReadLine());
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- switch (i)
- {
- case 1:
- {
- Log Obj = new Log();
- Console.Write("Содержимое файла: ");
- string nameFile = Obj.nameFile();
- Obj.FileRead(nameFile);
- }
- break;
- case 2:
- {
- Log Obj = new Log();
- Console.Write("Анализировать из файла: ");
- string nameFile = Obj.nameFile();
- Obj.analisisFileUsers(nameFile);
- }
- break;
- case 3:
- break;
- default:
- Console.WriteLine("Error input, retype pls");
- break;
- }
- }
- while (i != 3);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement