Advertisement
neron476

tipe_file_unendended

Dec 18th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace bat_try
  9. {
  10. class Program
  11. {
  12. struct Student
  13. {
  14. const int NUM_SEMESTER = 8;
  15. public string fio;
  16. public int grade;
  17. public int group;
  18. public double[] marks;
  19. public DateTime birthDate;
  20. public Student(string info)
  21. {
  22. marks = new double[NUM_SEMESTER];
  23.  
  24. string[] formated = info.Split(new string[] { " | " }, StringSplitOptions.RemoveEmptyEntries);
  25. fio = formated[0];
  26. // Разбор даты
  27. birthDate = GetDateFromString(formated[1]);
  28. marks = GetArrayFromString(formated[2]);
  29. grade = Convert.ToByte(formated[3]);
  30. group = Convert.ToByte(formated[4]);
  31. }
  32.  
  33. private static DateTime GetDateFromString(string date)
  34. {
  35. DateTime resultingDate;
  36. string[] dateString = date.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  37. resultingDate =
  38. new DateTime(Convert.ToInt16(dateString[2]), Convert.ToInt16(dateString[1]), Convert.ToInt16(dateString[0]));
  39. return resultingDate;
  40. }
  41.  
  42. private static double[] GetArrayFromString(string marks)
  43. {
  44. double[] result = new double[NUM_SEMESTER];
  45. string[] arrayString = marks.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  46. for (int i = 0; i < NUM_SEMESTER; ++i)
  47. {
  48. result[i] = Convert.ToDouble(arrayString[i]);
  49.  
  50. }
  51. return result;
  52. }
  53. public void BinaryWrite(BinaryWriter BWriter)
  54. {
  55. BWriter.Write(this.fio);
  56. BWriter.Write(this.grade);
  57. BWriter.Write(this.group);
  58. for (int i = 0; i < NUM_SEMESTER; i++)
  59. {
  60. BWriter.Write(this.marks[i]);
  61. }
  62. BWriter.Write(this.birthDate.Day);
  63. BWriter.Write(this.birthDate.Month);
  64. BWriter.Write(this.birthDate.Year);
  65.  
  66. }
  67. public void BinaryRead(BinaryReader BReader)
  68. {
  69. this.fio = BReader.ReadString();
  70. this.grade = BReader.ReadInt32();
  71. this.group = BReader.ReadInt32();
  72. this.marks = new double[NUM_SEMESTER];
  73. for (int i = 0; i < NUM_SEMESTER; i++)
  74. {
  75. this.marks[i] = BReader.ReadDouble();
  76. }
  77. this.birthDate = new DateTime(BReader.ReadInt32(), BReader.ReadInt32(), BReader.ReadInt32());
  78. }
  79. public void Print()
  80. {
  81. Console.Write(fio + ", {0}, ", birthDate.ToLongDateString());
  82. foreach (var cM in marks)
  83. Console.Write("{0} ", cM);
  84. Console.Write(", {0}, {1}\n", grade, group);
  85.  
  86. }
  87. }
  88. public static void Main(string[] args)
  89. {
  90.  
  91. string[] file = File.ReadAllLines("Students.txt");
  92. Student[] studs = new Student[file.Length];
  93. for (int i = 0; i < file.Length; ++i)
  94. studs[i] = new Student(file[i]);
  95. const string fileName = "Students.dat";
  96. BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Append));
  97. foreach (var pStudent in studs)
  98. {
  99. pStudent.BinaryWrite(writer);
  100. pStudent.Print();
  101. }
  102. writer.Close();
  103. // Запись в бинарный файл окончена
  104. BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open));
  105. Student[] StudList = new Student[studs.Length];
  106. for (int i = 0; i < studs.Length; i++)
  107. {
  108. StudList[i].BinaryRead(reader);
  109. StudList[i].Print();
  110. }
  111.  
  112. reader.Close();
  113. // Чтение из файла оконченно
  114. string[] LastNs = new string[StudList.Count()];
  115. for (int i = 0; i < StudList.Count(); i++)
  116. {
  117. string[] formated = StudList[i].fio.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  118. LastNs[i] = formated[0];
  119. }
  120. bool similar = false;
  121. for (int i = 0; i < LastNs.Count(); i++)
  122. {
  123. if (LastNs[i] == "0")
  124. continue;
  125. for (int j = 0; j < LastNs.Count(); j++)
  126. {
  127. if (i == j)
  128. continue;
  129. if (LastNs[i] == LastNs[j])
  130. {
  131. similar = true;
  132. studs[j].Print();
  133. LastNs[j] = "0";
  134. }
  135. }
  136. if (similar == true)
  137. {
  138. studs[i].Print();
  139. LastNs[i] = "0";
  140. similar = false;
  141. }
  142. }
  143. Console.ReadLine();
  144.  
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement