Advertisement
Guest User

укныргнко

a guest
Apr 26th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. using System;
  2. using System.IO;//Подключаем пространство имен для работы с файлами
  3.  
  4.  
  5. namespace Lab5
  6. {
  7. class LOG_File
  8. {
  9. public int LengthHash(string file, int start, int end)
  10. {
  11. string temp = null, temp2;
  12. int counter = 0;
  13. int counter2 = 0;
  14.  
  15. temp2 = file;
  16.  
  17. if (start == 0)
  18. {
  19. int i = 0;
  20. while (temp2[i++] != '#')
  21. {
  22. temp += temp2[i];
  23. }
  24. return temp.Length - 1;
  25. }
  26. else
  27. {
  28. int valueE = 0;
  29. counter = 0;
  30. while (counter != end)
  31. {
  32. if (temp2[valueE++] == '#' && counter != end)
  33. {
  34. counter++;
  35. }
  36. }
  37. int valueS = 0;
  38. counter2 = 0;
  39. while (counter2 != start)
  40. {
  41. if (temp2[valueS++] == '#' && counter2 != start)
  42. {
  43. counter2++;
  44. }
  45. }
  46. return valueE - valueS - 1;
  47. }
  48. return 0;
  49. }
  50. public void readFile(string nameFile)//вывод на экран содержимого любого файла
  51. {
  52. Console.WriteLine("Исходные данные {0}\n", nameFile);
  53. string st;
  54. try
  55. {
  56. StreamReader sr = File.OpenText(nameFile);
  57.  
  58. while ((st = sr.ReadLine()) != null)
  59. {
  60. Console.WriteLine(st);
  61. }
  62. Console.WriteLine("\n");
  63. sr.Close();
  64. }
  65. catch (Exception)
  66. {
  67. Console.WriteLine("Файл, подходящий для чтения не найден");
  68. }
  69. }
  70.  
  71.  
  72. public void analisisFileUsers(string nameFile)//анализ действия выбранного пользователя
  73. {
  74. try
  75. {
  76. StreamReader sr = new StreamReader(nameFile);
  77.  
  78. Console.Write("Анализ работы пользователя:");
  79. string user = Console.ReadLine();
  80.  
  81. StreamWriter sw = new StreamWriter("user.txt");
  82. sw.WriteLine("пользователь:{0}", user);
  83. while (true)
  84. {
  85. string st = sr.ReadLine();
  86. if (st == null)
  87. break;
  88. if (st.StartsWith(user) == true)
  89. {
  90. sw.WriteLine(st);
  91. }
  92. }
  93. sw.Close();
  94. sr.Close();
  95. //анализ того, на какие сайты заходил выбранный пользователь, сколько было передано информации
  96. StreamReader userFile = new StreamReader("user.txt");
  97. int kol = 3;
  98.  
  99. int kolRazd = 0;
  100. string x = "#";
  101. StreamWriter userSite = File.CreateText("userSite.txt");
  102. userSite.WriteLine("пользователь {0} посещал сайты:", user);
  103. while (true)
  104. {
  105. string str = userFile.ReadLine();
  106. string temp = userFile.ReadLine();
  107. if (str == null)
  108. break;
  109. //посещал сайты
  110. for (int i = 0; i < str.Length; i++)
  111. {
  112. if (str.Substring(i, 1) == x)
  113. {
  114. kolRazd++;
  115.  
  116. if (kolRazd == kol)
  117. {
  118. userSite.WriteLine(str.Substring((i + 1), LengthHash(temp, 3, 4)));
  119. kolRazd = 0;
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. userFile.Close();
  126. userSite.Close();
  127.  
  128. int kol2 = 4;
  129. int kolRazd2 = 0;
  130. StreamReader userFile2 = new StreamReader("user.txt");
  131. StreamWriter userSite2 = File.CreateText("userSite2.txt");
  132. userSite2.WriteLine("пользователь {0} потратил трафик:", user);
  133. while (true)
  134. {
  135. string str2 = userFile2.ReadLine();
  136.  
  137. if (str2 == null)
  138. break;
  139. //потрачено трафика
  140. for (int j = 0; j < str2.Length; j++)
  141. {
  142. if (str2.Substring(j, 1) == x)
  143. {
  144. kolRazd2++;
  145.  
  146. if (kolRazd2 == kol2)
  147. {
  148. //userSite2.WriteLine(str2.Substring((j + 1), 4));
  149. userSite2.WriteLine(str2.Substring((j + 1), LengthHash(str2, 4, 5)));
  150. kolRazd2 = 0;
  151. break;
  152. }
  153. }
  154.  
  155. }
  156. }
  157. userSite2.Close();
  158. userFile2.Close();
  159. }
  160.  
  161. catch (Exception ex)
  162. {
  163. Console.WriteLine("Файл не найден\n{0}", ex.Message);
  164. }
  165. }
  166.  
  167. public string nameFile()//имя файла
  168. {
  169. string nameFile = Console.ReadLine();
  170. return nameFile;
  171. }
  172. }
  173.  
  174. class Program
  175. {
  176. static void Main()
  177. {
  178. Program Obj = new Program();
  179. Obj.Menu();
  180. }
  181.  
  182. void Menu()
  183. {
  184. int i = 0;
  185. do
  186. {
  187. Console.Write("Меню:\n1) Печать из файла\n2) Анализ работы пользователя\n3) Выйти из программы\n\nВыберете пункт: ");
  188. try
  189. {
  190. i = Convert.ToInt32(Console.ReadLine());
  191. }
  192. catch (Exception ex)
  193. {
  194. Console.WriteLine(ex.Message);
  195. }
  196. switch (i)
  197. {
  198. case 1:
  199. {
  200. LOG_File Obj = new LOG_File();
  201. Console.Write("Содержимое файла: ");
  202. string nameFile = Obj.nameFile();
  203. Obj.readFile(nameFile);
  204. }
  205. break;
  206. case 2:
  207. {
  208. LOG_File Obj = new LOG_File();
  209. Console.Write("Анализировать из файла: ");
  210. string nameFile = Obj.nameFile();
  211. Obj.analisisFileUsers(nameFile);
  212. }
  213. break;
  214. case 3:
  215. break;
  216. default:
  217. Console.WriteLine("Error input, retype pls");
  218. break;
  219. }
  220. }
  221. while (i != 3);
  222. }
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement