Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 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 ClassLibrary1;
  7. using System.IO;
  8. namespace ConsoleApplication9
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Journal journal = new Journal();
  15. int counter = 0;
  16. string line;
  17.  
  18. StreamReader file = new StreamReader("1.txt");
  19. while ((line = file.ReadLine()) != null)
  20. {
  21. System.Console.WriteLine(line);
  22. string[] words = line.Split(new char[] { ' ' });
  23. double sred = 0;
  24. for (int j=1; j<words.Length; j++)
  25. {
  26. sred += double.Parse(words[j]);
  27. }
  28. sred = sred / (words.Length - 1);
  29. Pair<string, double> p = new Pair<string, double>(words[0], sred);
  30. bool a = journal + p;
  31. counter++;
  32. }
  33.  
  34. journal.MySerialize("out.ser");
  35. Journal spisok = Journal.MyDeserialize("out.ser");
  36. foreach (Pair<string, double> t in spisok)
  37. {
  38. Console.WriteLine(t);
  39. }
  40. int n = 3;
  41. Console.WriteLine();
  42. foreach (Pair<string, double> t in spisok.MyItr(n))
  43. {
  44. Console.WriteLine(t);
  45. }
  46.  
  47. file.Close();
  48. System.Console.WriteLine("There were {0} lines.", counter);
  49.  
  50. Console.ReadKey();
  51. }
  52. }
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. using System;
  70. using System.Collections.Generic;
  71. using System.Linq;
  72. using System.Text;
  73. using System.Threading.Tasks;
  74. using System.IO;
  75. using System.Xml.Serialization;
  76.  
  77. namespace ClassLibrary1
  78. {
  79. [Serializable]
  80. public class Journal
  81. {
  82. public List<Pair<string, double>> results
  83. {
  84. get; set;
  85. }
  86.  
  87. public Journal() { results = new List<Pair<string, double>>(); }
  88.  
  89. public static bool operator + (Journal obj1, Pair<string, double> obj2)
  90. {
  91. Journal a = new Journal();
  92. obj1.results.Add(obj2);
  93. return true;
  94. }
  95.  
  96. public IEnumerator<Pair<string, double>> GetEnumerator()
  97. {
  98. List<Pair<string, double>> a = new List<Pair<string, double>>(results);
  99. a.Sort();
  100. a.Reverse();
  101. foreach (Pair<string, double> t in a) {
  102. yield return t;
  103. }
  104. }
  105.  
  106. public IEnumerable<Pair<string, double>> MyItr(int end)
  107. {
  108. List<Pair<string, double>> a = new List<Pair<string, double>>(results);
  109. a.Sort();
  110.  
  111. for (int i = 1; i <= end; i++) {
  112. yield return a[i];
  113. }
  114. }
  115. static XmlSerializer formatter = new XmlSerializer(typeof(Journal));
  116. public void MySerialize(string path)
  117. {
  118. using (FileStream fs = new FileStream(path, FileMode.Create))
  119. {
  120. formatter.Serialize(fs, this);
  121. Console.WriteLine("Объект сериализован");
  122. }
  123. }
  124.  
  125. public static Journal MyDeserialize(string path)
  126. {
  127. Journal newPerson;
  128. using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
  129. {
  130. newPerson = (Journal)formatter.Deserialize(fs);
  131.  
  132. Console.WriteLine("Объект десериализован");
  133. }
  134. return newPerson;
  135. }
  136. }
  137.  
  138. [Serializable]
  139. public class Pair<T, U> : IComparable<Pair<T, U>> where U : IComparable
  140. {
  141. public T item1
  142. {
  143. get; set;
  144. }
  145. public U item2
  146. {
  147. get; set;
  148. }
  149. public Pair()
  150. {
  151. }
  152. public U Uval{
  153. get {
  154. return item2;
  155. }
  156. }
  157. public T Tval
  158. {
  159. get
  160. {
  161. return item1;
  162. }
  163. }
  164. public Pair(T i1, U i2)
  165. {
  166. item1 = i1;
  167. item2 = i2;
  168. }
  169. public int CompareTo(Pair<T,U> obj)
  170. {
  171. return (item2.CompareTo((obj).Uval));
  172. }
  173.  
  174. public override string ToString()
  175. {
  176. return (item1.ToString() + " " + item2.ToString());
  177. }
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement