Advertisement
Guest User

це3ацыу

a guest
Apr 26th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4.  
  5. namespace Lab5
  6. {
  7. class Log
  8. {
  9. public int LengthHash(string file, int start, int end)
  10. {
  11. if (start < 0 || end < 0)
  12. {
  13. return 0;
  14. }
  15. if (start == 0)
  16. {
  17. string temp = null;
  18. int i = 0;
  19. while (file[i++] != '#')
  20. {
  21. temp += file[i];
  22. }
  23. return temp.Length - 1;
  24. }
  25. else
  26. {
  27. int counter = 0;
  28. int counter2 = 0;
  29. int valueE = 0;
  30.  
  31. while (counter != end)
  32. {
  33. if (file[valueE++] == '#' && counter != end)
  34. {
  35. counter++;
  36. }
  37. }
  38. int valueS = 0;
  39. while (counter2 != start)
  40. {
  41. if (file[valueS++] == '#' && counter2 != start)
  42. {
  43. counter2++;
  44. }
  45. }
  46. return valueE - valueS - 1;
  47. }
  48. }
  49. public void FileRead(string file)//вывод на экран содержимого любого файла
  50. {
  51. Console.WriteLine("Data file: {0}\n", file);
  52. string st;
  53. try
  54. {
  55. StreamReader sr = File.OpenText(file);
  56.  
  57. while ((st = sr.ReadLine()) != null)
  58. {
  59. Console.WriteLine(st);
  60. }
  61. Console.WriteLine("\n");
  62. sr.Close();
  63. }
  64. catch (Exception ex)
  65. {
  66. Console.WriteLine(ex.Message);
  67. }
  68. }
  69.  
  70.  
  71. public void analisisFileUsers(string nameFile)//анализ действия выбранного пользователя
  72. {
  73. try
  74. {
  75. StreamReader sr = new StreamReader(nameFile);
  76.  
  77. Console.Write("Анализ работы пользователя:");
  78. string user = Console.ReadLine();
  79.  
  80. StreamWriter sw = new StreamWriter("user.txt");
  81. sw.WriteLine("пользователь:{0}", user);
  82. while (true)
  83. {
  84. string st = sr.ReadLine();
  85. if (st == null)
  86. break;
  87. if (st.StartsWith(user) == true)
  88. {
  89. sw.WriteLine(st);
  90. }
  91. }
  92. sw.Close();
  93. sr.Close();
  94. //анализ того, на какие сайты заходил выбранный пользователь, сколько было передано информации
  95. StreamReader userFile = new StreamReader("user.txt");
  96. int kol = 3;
  97.  
  98. int kolRazd = 0;
  99. string x = "#";
  100. StreamWriter userSite = File.CreateText("userSite.txt");
  101. userSite.WriteLine("пользователь {0} посещал сайты:", user);
  102. while (true)
  103. {
  104. string str = userFile.ReadLine();
  105. if (str == null)
  106. break;
  107. //посещал сайты
  108. for (int i = 0; i < str.Length; i++)
  109. {
  110. if (str.Substring(i, 1) == x)
  111. {
  112. kolRazd++;
  113.  
  114. if (kolRazd == kol)
  115. {
  116. userSite.WriteLine(str.Substring((i + 1), LengthHash(str, 3, 4)));
  117. kolRazd = 0;
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. userFile.Close();
  124. userSite.Close();
  125.  
  126. int kol2 = 4;
  127. int kolRazd2 = 0;
  128. int counter = 0;
  129. StreamReader userFile2 = new StreamReader("user.txt");
  130. StreamWriter userSite2 = File.CreateText("userSite2.txt");
  131. userSite2.WriteLine("пользователь {0} потратил трафик:", user);
  132. while (true)
  133. {
  134. string str2 = userFile2.ReadLine();
  135.  
  136. if (str2 == null)
  137. break;
  138. //потрачено трафика
  139. for (int j = 0; j < str2.Length; j++)
  140. {
  141. if (str2.Substring(j, 1) == x)
  142. {
  143. kolRazd2++;
  144.  
  145. if (kolRazd2 == kol2)
  146. {
  147. userSite2.WriteLine(str2.Substring((j + 1), LengthHash(str2, 4, 5)));
  148. counter += Convert.ToInt32(str2.Substring((j + 1), LengthHash(str2, 4, 5)));
  149. kolRazd2 = 0;
  150. break;
  151. }
  152. }
  153.  
  154. }
  155. }
  156. userSite2.WriteLine("Summ: {0}", counter);
  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 Obj = new Log();
  201. Console.Write("Содержимое файла: ");
  202. string nameFile = Obj.nameFile();
  203. Obj.FileRead(nameFile);
  204. }
  205. break;
  206. case 2:
  207. {
  208. Log Obj = new Log();
  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