Advertisement
Margoshinka

fiiiiiiileee

Oct 23rd, 2021
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.22 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6.  
  7. namespace lab4
  8. { [Serializable]
  9. struct TOYS
  10. {
  11. public string name;
  12. public int cost, age1, age2;
  13. public TOYS(string s, int c, int a1, int a2)
  14. {
  15. name = s;
  16. cost = c;
  17. age1 = a1;
  18. age2 = a2;
  19.  
  20. }
  21.  
  22. public override string ToString() // перегрузка метода ToString() для формирования строки
  23. {
  24.  
  25. return ($"Игрушка - {name}, стоимость - {cost}, подходит детям от {age1} до {age2} лет");
  26. }
  27. }
  28. class Program
  29.  
  30. {
  31. static int n = 0;
  32. static private Random rnd = new Random();
  33. static List <int> list = new List<int>();
  34. static List<int> list_f = new List<int>();
  35. static int[,] arr = new int[40, 40];
  36.  
  37.  
  38.  
  39. static void Write_(FileStream f)
  40. {
  41. try
  42. {
  43. using (f)
  44. {
  45. using (BinaryWriter fout = new BinaryWriter(f))
  46. {
  47.  
  48.  
  49. Console.WriteLine("Исходный файл:");
  50. int a;
  51. int n = rnd.Next(5, 20);
  52. for (int i = 1; i <= n; i++)
  53. {
  54.  
  55.  
  56.  
  57. a = rnd.Next(-10, 10);
  58. Console.Write(a + " ");
  59. fout.Write(a);
  60. }
  61.  
  62. }
  63.  
  64. }
  65. }
  66. catch(IOException e)
  67. {
  68. Console.WriteLine("Error:" + e.Message);
  69. return;
  70. }
  71.  
  72. }
  73. static void Read_(FileStream f)
  74. {
  75. int a;
  76.  
  77. try
  78. {
  79. using (f)
  80. {
  81. using (BinaryReader F = new BinaryReader(f))
  82. {
  83. for (; F.BaseStream.Position < F.BaseStream.Length;)
  84. {
  85. a = F.ReadInt32();
  86.  
  87. list.Add(a);
  88.  
  89. }
  90.  
  91. }
  92.  
  93. }
  94. }
  95. catch (FileNotFoundException e)
  96. {
  97. Console.WriteLine("Проверьте правильность имени файла" + e.Message);
  98. return;
  99. }
  100. catch (Exception e)
  101. {
  102. Console.WriteLine("Error:" + e.Message);
  103. return;
  104. }
  105.  
  106. }
  107. static void Save(string fileName)
  108. {
  109. BinaryFormatter bf = new BinaryFormatter();
  110. using (Stream writer = new FileStream(fileName, FileMode.Create))
  111. {
  112. bf.Serialize(writer, list);
  113. }
  114. }
  115. static void Load(string fileName)
  116. {
  117. BinaryFormatter bf = new BinaryFormatter();
  118. using (Stream reader = new FileStream(fileName, FileMode.Open))
  119. {
  120. list_f = (List<int>)bf.Deserialize(reader);
  121. }
  122. }
  123. static void inp()
  124. {
  125. string buf;
  126. Console.WriteLine();
  127. Console.WriteLine("Введите n");
  128. buf = Console.ReadLine();
  129. n = int.Parse(buf);
  130. int k = 0;
  131. for (int i=0; i< n; i++)
  132. {
  133. for (int j=0; j<n; j++)
  134. {
  135. if (k <= list_f.Count - 1)
  136. arr[i, j] = list[k];
  137. else break;
  138. k++;
  139. }
  140. }
  141. }
  142. static void out1(int[,] arr) //вывод массива
  143. {
  144. for (int i = 0; i < n; i++)
  145. {
  146. for (int j = 0; j < n; j++)
  147. {
  148. Console.Write("{0}\t", arr[i, j]);
  149. }
  150. Console.WriteLine();
  151. }
  152. }
  153. static int find(int [] max_, int str)
  154. {
  155. for (int i = 0; i < str; i++)
  156. {
  157.  
  158. for (int j = 0; j < n; j++)
  159. {
  160.  
  161.  
  162. max_[i] *= arr[i,j];
  163. }
  164.  
  165. }
  166. Console.WriteLine();
  167. for (int i = 0; i < str; i++)
  168.  
  169. Console.WriteLine(max_[i]);
  170.  
  171. int max = Array.IndexOf(max_, max_.Max());
  172. return max;
  173. }
  174. static void reverse(int rez)
  175. {
  176. for (int i = 0; i < n; i++)
  177. {
  178.  
  179. for (int j = 0; j < n; j++)
  180. {
  181.  
  182.  
  183. arr[i,j] = arr[rez, j];
  184. }
  185.  
  186. }
  187. }
  188. static void Create_BF(string name)
  189. {
  190. FileStream f = new FileStream(name, FileMode.Create);
  191. BinaryFormatter bf = new BinaryFormatter();
  192. TOYS D;
  193. int n, i;
  194. Console.WriteLine("Сколько игрушек?");
  195. n = int.Parse(Console.ReadLine());
  196. string[] d;
  197. for (i = 1; i <= n; i++)
  198. {
  199. Console.WriteLine("{0}-я игрушка", i);
  200. d = Console.ReadLine().Split(' ');
  201. D.name = d[0];
  202. D.cost = int.Parse(d[1]);
  203. D.age1 = int.Parse(d[2]);
  204. D.age2 = int.Parse(d[3]);
  205. bf.Serialize(f, D);
  206. }
  207. f.Close();
  208. }
  209. static void Toy(string name, int k)
  210. {
  211. FileStream f = new FileStream(name, FileMode.Open);
  212. BinaryFormatter bf = new BinaryFormatter();
  213. TOYS D;
  214.  
  215. while (f.Position < f.Length) // пока в файле есть непрочитанные записи
  216. {
  217. D = (TOYS)bf.Deserialize(f);
  218. if ((D.age1 <= 5 && D.age2 >= 5) && D.cost <= k)
  219. Console.WriteLine(D);
  220. }
  221. f.Close();
  222.  
  223. }
  224. static void Write_text(string name)
  225. {
  226. try
  227. {
  228. using(StreamWriter f= new StreamWriter(name))
  229. {
  230. int a;
  231. Console.WriteLine("Введите n");
  232. string buf = Console.ReadLine();
  233. n = int.Parse(buf);
  234. Console.WriteLine("Заполнение файла");
  235. for(int i=1; i <= n; i++)
  236. {
  237.  
  238. buf = Console.ReadLine();
  239. a = int.Parse(buf);
  240. f.WriteLine(a);
  241.  
  242. }
  243.  
  244. }
  245. }
  246. catch (IOException e)
  247. {
  248. Console.WriteLine("Error:" + e.Message);
  249. return;
  250. }
  251. }
  252. static bool Find_Text(int b, string name)
  253. {
  254. try
  255. {
  256. using (StreamReader f = new StreamReader(name))
  257. {
  258. string s;
  259. while ((s = f.ReadLine()) != null)
  260. {
  261. int a = int.Parse(s);
  262. if (a == b) return true;
  263. }
  264. return false;
  265. }
  266. }
  267. catch (IOException e)
  268. {
  269. Console.WriteLine("Error:" + e.Message);
  270. return false;
  271. }
  272. }
  273. static void Write_text2(string name)
  274. {
  275. try
  276. {
  277. using (StreamWriter f = new StreamWriter(name))
  278. {
  279.  
  280. Console.WriteLine("Введите n");
  281. string buf = Console.ReadLine();
  282. n = int.Parse(buf);
  283. Console.WriteLine("Заполнение файла");
  284. for (int i = 1; i <= n; i++)
  285. {
  286.  
  287. buf = Console.ReadLine();
  288.  
  289. f.WriteLine(buf);
  290.  
  291. }
  292.  
  293. }
  294. }
  295. catch (IOException e)
  296. {
  297. Console.WriteLine("Error:" + e.Message);
  298. return;
  299. }
  300. }
  301. static int Find_Text2(int b, string name)
  302. {
  303. try
  304. {
  305. using (StreamReader f = new StreamReader(name))
  306. {
  307. int a=0;
  308. int sum = 0;
  309. string s;
  310. while ((s = f.ReadLine()) != null)
  311. {
  312. string[] splitLine = s.Split(' ');
  313.  
  314. for (int i = 0; i < splitLine.Length; i++)
  315. {
  316. a = int.Parse(splitLine[i]);
  317. if (a % b == 0) sum += a;
  318. }
  319. }
  320. return sum;
  321. }
  322. }
  323. catch (IOException e)
  324. {
  325. Console.WriteLine("Error:" + e.Message);
  326. return -1;
  327. }
  328. }
  329.  
  330. static void Find_Text3( string name, StreamWriter f2)
  331. { int myInt;
  332. bool isNumerical = true;
  333. try
  334. {
  335. using (StreamReader f = new StreamReader(name))
  336. {
  337.  
  338.  
  339. string s;
  340. while ((s = f.ReadLine()) != null)
  341. {
  342. string[] splitLine = s.Split(' ');
  343.  
  344. for (int i = 0; i < splitLine.Length; i++)
  345. {
  346.  
  347. isNumerical = int.TryParse(splitLine[i], out myInt);
  348. if (isNumerical)
  349.  
  350.  
  351. break;
  352.  
  353.  
  354. }
  355. if (!isNumerical)
  356. {
  357. Console.WriteLine(s);
  358. f2.WriteLine(s);
  359.  
  360. }
  361.  
  362. }
  363.  
  364. }
  365. }
  366. catch (IOException e)
  367. {
  368. Console.WriteLine("Error:" + e.Message);
  369. return;
  370. }
  371. }
  372.  
  373. static void Main(string[] args)
  374. {
  375. FileStream f = new FileStream("test.dat", FileMode.Create);
  376. Write_(f);
  377. FileStream f1 = new FileStream("test.dat", FileMode.Open);
  378.  
  379. Read_(f1);
  380. var uniq = list.Distinct();
  381. list = uniq.ToList();
  382.  
  383. Save("finish.dat");
  384. Load("finish.dat");
  385. Console.WriteLine();
  386. foreach (var item in list_f)
  387. {
  388. Console.Write(item + " ");
  389. }
  390. inp();
  391. out1(arr);
  392. int str;
  393.  
  394. str = list_f.Count / n;
  395.  
  396. Console.WriteLine();
  397. Console.WriteLine("Количество заполненных строк " + str);
  398. int[] max_ = new int[str];
  399. for (int i = 0; i < str; i++)
  400. {
  401. max_[i] = 1;
  402. }
  403.  
  404. int rez =find(max_, str);
  405. reverse(rez);
  406. Console.WriteLine();
  407. out1(arr);
  408. Create_BF("toy.dat");
  409. string buf; int k;
  410. Console.WriteLine();
  411. Console.WriteLine("Введите стоимость");
  412. buf = Console.ReadLine();
  413. k = int.Parse(buf);
  414. Toy("toy.dat",k);
  415. Write_text("Text.dat");
  416. Console.WriteLine("Введите заданное число");
  417. buf = Console.ReadLine();
  418. int b = int.Parse(buf);
  419. bool ok = Find_Text(b, "Text.dat");
  420. Console.WriteLine(ok);
  421. Write_text2("Text2.dat");
  422. Console.WriteLine("Введите заданное число");
  423. buf = Console.ReadLine();
  424. b = int.Parse(buf);
  425. int res = Find_Text2(b, "Text2.dat");
  426. Console.WriteLine($"Сумма:{res}");
  427. Write_text2("Text3.dat");
  428. StreamWriter f_ = new StreamWriter("Text3_.dat");
  429. Console.WriteLine();
  430. Find_Text3("Text3.dat", f_);
  431. f.Close();
  432. }
  433. }
  434. }
  435.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement