Advertisement
Margoshinka

files

Oct 22nd, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 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. {
  9. class Program
  10.  
  11. {
  12. static int n = 0;
  13. static private Random rnd = new Random();
  14. static List <int> list = new List<int>();
  15. static List<int> list_f = new List<int>();
  16. static int[,] arr = new int[40, 40];
  17.  
  18.  
  19. static void Write_(FileStream f)
  20. {
  21. try
  22. {
  23. using (f)
  24. {
  25. using (BinaryWriter fout = new BinaryWriter(f))
  26. {
  27.  
  28.  
  29. Console.WriteLine("Исходный файл:");
  30. int a;
  31. int n = rnd.Next(5, 20);
  32. for (int i = 1; i <= n; i++)
  33. {
  34.  
  35.  
  36.  
  37. a = rnd.Next(-10, 10);
  38. Console.Write(a + " ");
  39. fout.Write(a);
  40. }
  41.  
  42. }
  43.  
  44. }
  45. }
  46. catch(IOException e)
  47. {
  48. Console.WriteLine("Error:" + e.Message);
  49. return;
  50. }
  51.  
  52. }
  53. static void Read_(FileStream f)
  54. {
  55. int a;
  56.  
  57. try
  58. {
  59. using (f)
  60. {
  61. using (BinaryReader F = new BinaryReader(f))
  62. {
  63. for (; F.BaseStream.Position < F.BaseStream.Length;)
  64. {
  65. a = F.ReadInt32();
  66.  
  67. list.Add(a);
  68.  
  69. }
  70.  
  71. }
  72.  
  73. }
  74. }
  75. catch (FileNotFoundException e)
  76. {
  77. Console.WriteLine("Проверьте правильность имени файла" + e.Message);
  78. return;
  79. }
  80. catch (Exception e)
  81. {
  82. Console.WriteLine("Error:" + e.Message);
  83. return;
  84. }
  85.  
  86. }
  87. static void Save(string fileName)
  88. {
  89. BinaryFormatter bf = new BinaryFormatter();
  90. using (Stream writer = new FileStream(fileName, FileMode.Create))
  91. {
  92. bf.Serialize(writer, list);
  93. }
  94. }
  95. static void Load(string fileName)
  96. {
  97. BinaryFormatter bf = new BinaryFormatter();
  98. using (Stream reader = new FileStream(fileName, FileMode.Open))
  99. {
  100. list_f = (List<int>)bf.Deserialize(reader);
  101. }
  102. }
  103. static void inp()
  104. {
  105. string buf;
  106. Console.WriteLine();
  107. Console.WriteLine("Введите n");
  108. buf = Console.ReadLine();
  109. n = int.Parse(buf);
  110. int k = 0;
  111. for (int i=0; i< n; i++)
  112. {
  113. for (int j=0; j<n; j++)
  114. {
  115. if (k <= list_f.Count - 1)
  116. arr[i, j] = list[k];
  117. else break;
  118. k++;
  119. }
  120. }
  121. }
  122. static void out1(int[,] arr) //вывод массива
  123. {
  124. for (int i = 0; i < n; i++)
  125. {
  126. for (int j = 0; j < n; j++)
  127. {
  128. Console.Write("{0}\t", arr[i, j]);
  129. }
  130. Console.WriteLine();
  131. }
  132. }
  133. static int find(int [] max_, int str)
  134. {
  135. for (int i = 0; i < str; i++)
  136. {
  137.  
  138. for (int j = 0; j < n; j++)
  139. {
  140.  
  141.  
  142. max_[i] *= arr[i,j];
  143. }
  144.  
  145. }
  146. Console.WriteLine();
  147. for (int i = 0; i < str; i++)
  148.  
  149. Console.WriteLine(max_[i]);
  150.  
  151. int max = Array.IndexOf(max_, max_.Max());
  152. return max;
  153. }
  154. static void reverse(int rez)
  155. {
  156. for (int i = 0; i < n; i++)
  157. {
  158.  
  159. for (int j = 0; j < n; j++)
  160. {
  161.  
  162.  
  163. arr[i,j] = arr[rez, j];
  164. }
  165.  
  166. }
  167. }
  168.  
  169. static void Main(string[] args)
  170. {
  171. FileStream f = new FileStream("test.dat", FileMode.Create);
  172. Write_(f);
  173. FileStream f1 = new FileStream("test.dat", FileMode.Open);
  174.  
  175. Read_(f1);
  176. var uniq = list.Distinct();
  177. list = uniq.ToList();
  178.  
  179. Save("finish.dat");
  180. Load("finish.dat");
  181. Console.WriteLine();
  182. foreach (var item in list_f)
  183. {
  184. Console.Write(item + " ");
  185. }
  186. inp();
  187. out1(arr);
  188. int str;
  189.  
  190. str = list_f.Count / n;
  191.  
  192. Console.WriteLine();
  193. Console.WriteLine("Количество заполненных строк " + str);
  194. int[] max_ = new int[str];
  195. for (int i = 0; i < str; i++)
  196. {
  197. max_[i] = 1;
  198. }
  199.  
  200. int rez =find(max_, str);
  201. reverse(rez);
  202. Console.WriteLine();
  203. out1(arr);
  204.  
  205.  
  206. }
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement