Reshenie.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. using CsvHelper;
  8. using System.Diagnostics;
  9. namespace Bobarik31P
  10. {
  11. public class Reshenie
  12. {
  13. {
  14. Path = path;
  15. }
  16. private (double[], double[], double[,]) readingFile()
  17. {
  18. Debug.Listeners.Add(new TextWriterTraceListener("debug.txt"));
  19. Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
  20. Debug.AutoFlush = true;
  21. if (!Path.Contains(".csv"))
  22. {
  23. double[] spros = new double[0];
  24. double[] predlozhenie = new double[0];
  25. double[,] tarif = new double[0,0];
  26. Console.WriteLine("Неверный формат файла");
  27. Console.ReadKey();
  28. return (spros, predlozhenie, tarif);
  29. }
  30. else
  31. {
  32. try
  33. {
  34. var dataStr = File.ReadAllLines(Path)
  35. .Select(line => line.Split(';').ToArray()
  36. ).ToArray();
  37. double[] spros = new double[dataStr[0].Length];
  38. double[] predlozhenie = new double[dataStr[1].Length];
  39. double[,] tarif = new double[dataStr.Length - 2, dataStr[1].Length];
  40. for (int i = 0; i < 2; i++)
  41. {
  42. for (int j = 0; j < dataStr[i].GetLength(0); j++)
  43. {
  44. if (i == 0)
  45. {
  46. spros[j] = Convert.ToDouble(dataStr[i][j]);
  47. Debug.WriteLine(spros[j]);
  48. }
  49. else
  50. {
  51. predlozhenie[j] = Convert.ToDouble(dataStr[i][j]);
  52. Debug.WriteLine(predlozhenie[j]);
  53. }
  54. }
  55. }
  56. for (int i = 0; i < dataStr.Length - 2; i++)
  57. {
  58. for (int j = 0; j < dataStr[i].Length; j++)
  59. {
  60. tarif[i, j] = Convert.ToDouble(dataStr[i][j]);
  61. }
  62. }
  63. return (spros, predlozhenie, tarif);
  64. }
  65. catch(Exception ex)
  66. {
  67. Console.WriteLine(ex.Message);
  68. }
  69. }
  70. }
  71. private void writingFile(List<List<double?>> itog)
  72. {
  73. using (StreamWriter writer = new StreamWriter("result.csv"))
  74. {
  75. using (CsvWriter csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture))
  76. {
  77. for(int i = 0; i < itog.Count; i++)
  78. {
  79. for(int j = 0; j < itog[i].Count; j++)
  80. {
  81. csv.WriteRecord(itog[i][j].Value);
  82. }
  83. csv.NextRecord();
  84. }
  85. }
  86. }
  87. }
  88. public void MinElement()
  89. {
  90. Trace.Listeners.Add(new TextWriterTraceListener("debug.txt"));
  91. Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
  92. Trace.AutoFlush = true;
  93. double[] spros = readingFile().Item1;
  94. double[] predloahenie = readingFile().Item2;
  95. double[] copy_spros = new double[spros.Length];
  96. spros.CopyTo(copy_spros, 0);
  97. double[] copy_predloahenie = new double[predloahenie.Length];
  98. predloahenie.CopyTo(copy_predloahenie, 0);
  99. double[,] tarif = readingFile().Item3;
  100. double[,] copy_tarif = new double[tarif.GetLength(0), tarif.GetLength(1)];
  101. for(int i = 0; i < copy_tarif.GetLength(0); i++)
  102. {
  103. for(int j = 0; j < copy_tarif.GetLength(1); j++)
  104. {
  105. copy_tarif[i,j] = tarif[i,j];
  106. }
  107. }
  108. List<List<double?>> itog = new List<List<double?>>();
  109. for(int i = 0;i<copy_tarif.GetLength(0); i++)
  110. {
  111. itog.Add(new List<double?>());
  112. for (int j = 0; j < copy_tarif.GetLength(1); j++)
  113. {
  114. itog[i].Add(null);
  115. }
  116. }
  117. bool flag = true;
  118. while (flag)
  119. {
  120. double minTarif = double.MaxValue;
  121. int strokaMinTarifa = -1;
  122. int stolbecMinTarifa = -1;
  123. for (int i = 0; i < copy_tarif.GetLength(0); i++)
  124. {
  125. for(int j = 0; j < copy_tarif.GetLength(1); j++)
  126. {
  127. if (copy_tarif[i, j] < minTarif)
  128. {
  129. minTarif = copy_tarif[i, j];
  130. strokaMinTarifa = i;
  131. stolbecMinTarifa = j;
  132. }
  133. }
  134. }
  135. double razmerPostavki = Math.Min(copy_spros[strokaMinTarifa], copy_predloahenie[stolbecMinTarifa]);
  136. Trace.WriteLine(razmerPostavki);
  137. copy_predloahenie[stolbecMinTarifa] -= razmerPostavki;
  138. Trace.WriteLine(copy_predloahenie[stolbecMinTarifa]);
  139. copy_spros[strokaMinTarifa] -= razmerPostavki;
  140. Trace.WriteLine(copy_spros[strokaMinTarifa]);
  141. if (copy_spros[strokaMinTarifa] == 0)
  142. {
  143. for(int i = 0; i < itog.Count; i++)
  144. {
  145. if (itog[strokaMinTarifa][i] == null)
  146. {
  147. itog[strokaMinTarifa][ i] = 0;
  148. }
  149. copy_tarif[strokaMinTarifa, i] = double.MaxValue;
  150. }
  151. }
  152. if (copy_predloahenie[stolbecMinTarifa] == 0)
  153. {
  154. for (int i = 0; i < itog[0].Count; i++)
  155. {
  156. if (itog[i][stolbecMinTarifa] == null)
  157. {
  158. itog[i][stolbecMinTarifa] = 0;
  159. }
  160. copy_tarif[i, stolbecMinTarifa] = double.MaxValue;
  161. }
  162. }
  163. itog[strokaMinTarifa][stolbecMinTarifa] = razmerPostavki;
  164. bool proverkaOkonchania = true;
  165. for(int i = 0;i < itog.Count;i++)
  166. {
  167. for(int j = 0; j < itog[i].Count; j++)
  168. {
  169. if(itog[i][j] == null) { proverkaOkonchania = false; }
  170. }
  171. }
  172. if (proverkaOkonchania)
  173. {
  174. break;
  175. }
  176. }
  177. writingFile(itog);
  178. }
  179. }
  180. }