SimpleTable.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace matModelirovanie
  8. {
  9. internal class SimpleTable
  10. {
  11. //= { { 2, -1, 1, 1, 1},{-4, 2, -1, 1, 2 },{ 3, 0, 1, 1, 5} }
  12. //= { { 2, -1, 1, 1, 1},{-4, 2, -1, 1, 2 },{ 3, 0, 1, 1, 5} }
  13. decimal[,] simpleTable;
  14. decimal[,] tableAll;
  15. decimal[] celFunction;
  16. string typeTask;
  17. public string TypeTask { get => typeTask; set => typeTask = value; }
  18. public SimpleTable()
  19. {
  20. InputSystem();
  21. OutputSystem();
  22. TypeTask = Console.ReadLine();
  23. }
  24. public void InputSystem()
  25. {
  26. Console.WriteLine("Введите количество уравнений в системе");
  27. int countYrav = Convert.ToInt32(Console.ReadLine());
  28. Console.WriteLine("Введите количество основых переменных");
  29. int countXValue = Convert.ToInt32(Console.ReadLine());
  30. celFunction = new decimal[countXValue];
  31. for (int i = 0; i < countXValue; i++)
  32. {
  33. Console.WriteLine($"Введите значение аргумента x{i + 1} в целевой функции:");
  34. celFunction[i] = Convert.ToDecimal(Console.ReadLine());
  35. }
  36. int countI = 1;
  37. simpleTable = new decimal[countYrav, countXValue + 2];
  38. for (int i = 0; i < simpleTable.GetLength(0); i++)
  39. {
  40. int countJ = 1;
  41. for (int j = 0; j < simpleTable.GetLength(1) - 2; j++)
  42. {
  43. Console.WriteLine($"Введите значение коэффициент переменной x{countJ++} уравнения {countI}");
  44. simpleTable[i, j] = Convert.ToDecimal(Console.ReadLine());
  45. }
  46. countI++;
  47. }
  48. for (int i = 0; i < simpleTable.GetLength(0); i++)
  49. {
  50. simpleTable[i, simpleTable.GetLength(1) - 2] = 1;
  51. }
  52. countI = 1;
  53. for (int i = 0; i < simpleTable.GetLength(0); i++)
  54. {
  55. Console.WriteLine($"Введите значение уравнения {countI}");
  56. simpleTable[i, simpleTable.GetLength(1) - 1] = Convert.ToDecimal(Console.ReadLine());
  57. }
  58. }
  59. private void OutputSystem()
  60. {
  61. int countBasicVariables = simpleTable.GetLength(1) - 1;
  62. Console.WriteLine($"Система уравнений:\n");
  63. for (int i = 0; i < simpleTable.GetLength(0); i++)
  64. {
  65. int countJ = 1;
  66. for (int j = 0; j < simpleTable.GetLength(1); j++)
  67. {
  68. if (j == simpleTable.GetLength(1) - 1)
  69. {
  70. Console.Write($" = {simpleTable[i, j]}");
  71. }
  72. else if (j == simpleTable.GetLength(1) - 2)
  73. {
  74. Console.Write($"{simpleTable[i, j]}x{countBasicVariables}"); countBasicVariables++;
  75. }
  76. else Console.Write($"{simpleTable[i, j]}x{countJ} + "); countJ++;
  77. }
  78. Console.WriteLine();
  79. }
  80. StartOutputTable();
  81. }
  82. private void StartOutputTable()
  83. {
  84. tableAll = new decimal[simpleTable.GetLength(0) + 2, ((simpleTable.GetLength(1) - 2) + simpleTable.GetLength(0)) + 2];
  85. for (int i = 1; i <= tableAll.GetLength(1) - 2; i++)
  86. {
  87. tableAll[0, i] = i;
  88. }
  89. int countBasis = simpleTable.GetLength(1) - 2;
  90. for (int i = 1; i <= tableAll.GetLength(0) - 2; i++)
  91. {
  92. tableAll[i, 0] = ++countBasis;
  93. }
  94. InputDateInTable();
  95. }
  96. private void PrintTable()
  97. {
  98. Console.WriteLine();
  99. for (int i = 0; i < tableAll.GetLength(0); i++)
  100. {
  101. for (int j = 0; j < tableAll.GetLength(1); j++)
  102. {
  103. Console.Write("{0,8:0.0}", tableAll[i, j]);
  104. }
  105. Console.WriteLine();
  106. }
  107. }
  108. private void InputDateInTable()
  109. {
  110. int countBasis = simpleTable.GetLength(1) - 1;
  111. for (int i = 1; i <= simpleTable.GetLength(0); i++)
  112. {
  113. for (int j = 1; j <= simpleTable.GetLength(1); j++)
  114. {
  115. if (j == simpleTable.GetLength(1) - 1)
  116. {
  117. tableAll[i, countBasis] = simpleTable[i - 1, j - 1];
  118. countBasis++;
  119. }
  120. else if (j == simpleTable.GetLength(1))
  121. {
  122. tableAll[i, tableAll.GetLength(1) - 1] = simpleTable[i - 1, j - 1];
  123. }
  124. else
  125. {
  126. tableAll[i, j] = simpleTable[i - 1, j - 1];
  127. }
  128. }
  129. }
  130. for (int i = 1; i < simpleTable.GetLength(1) - 1; i++)
  131. {
  132. tableAll[tableAll.GetLength(0) - 1, i] = 0 - celFunction[i - 1];
  133. }
  134. PrintTable();
  135. }
  136. public bool CheckMinTask()
  137. {
  138. for (int i = 1; i < tableAll.GetLength(1) - 1; i++)
  139. {
  140. if (tableAll[tableAll.GetLength(0) - 1, i] > 0) return true;
  141. }
  142. return false;
  143. }
  144. public bool CheckMaxTask()
  145. {
  146. for (int i = 1; i < tableAll.GetLength(1) - 1; i++)
  147. {
  148. if (tableAll[tableAll.GetLength(0) - 1, i] < 0) return true;
  149. }
  150. return false;
  151. }
  152. public void StartWorkWithTable()
  153. {
  154. decimal maxElem = 0;
  155. int maxElemIndex = 0;
  156. decimal minElem = 100;
  157. int minElemIndex = 0;
  158. var arrayCelFunction = new Dictionary<decimal, decimal>();
  159. if (typeTask == "Минимум")
  160. {
  161. for (int i = 1; i < tableAll.GetLength(1) - 1; i++)
  162. {
  163. if (tableAll[tableAll.GetLength(0) - 1, i] > maxElem)
  164. {
  165. maxElem = tableAll[tableAll.GetLength(0) - 1, i];
  166. maxElemIndex = i;
  167. }
  168. }
  169. for (int i = 1; i < tableAll.GetLength(1) - 1; i++)
  170. {
  171. if (tableAll[i, maxElemIndex] != 0 && tableAll[i, tableAll.GetLength(1) - 1] / tableAll[i, maxElemIndex] > 0)
  172. {
  173. arrayCelFunction.Add(i, tableAll[i, tableAll.GetLength(1) - 1] / tableAll[i, maxElemIndex]);
  174. }
  175. }
  176. Dictionary<decimal, decimal>.ValueCollection valueColl = arrayCelFunction.Values;
  177. decimal minElemInColumn = valueColl.Min();
  178. int key = Convert.ToInt32(arrayCelFunction.FirstOrDefault(x => x.Value == minElemInColumn).Key);
  179. decimal controlElement = tableAll[key, maxElemIndex];
  180. WorkWithTable(controlElement, key, maxElemIndex);
  181. }
  182. else if (typeTask == "Максимум")
  183. {
  184. for (int i = 1; i < tableAll.GetLength(1) - 1; i++)
  185. {
  186. if (tableAll[tableAll.GetLength(0) - 1, i] < minElem)
  187. {
  188. minElem = tableAll[tableAll.GetLength(0) - 1, i];
  189. minElemIndex = i;
  190. }
  191. }
  192. for (int i = 1; i < tableAll.GetLength(0) - 1; i++)
  193. {
  194. if (tableAll[i, minElemIndex] != 0 && tableAll[i, tableAll.GetLength(1) - 1] / tableAll[i, minElemIndex] > 0)
  195. {
  196. arrayCelFunction.Add(i, tableAll[i, tableAll.GetLength(1) - 1] / tableAll[i, minElemIndex]);
  197. }
  198. }
  199. Dictionary<decimal, decimal>.ValueCollection valueColl = arrayCelFunction.Values;
  200. decimal minElemInColumn = valueColl.Min();
  201. int key = Convert.ToInt32(arrayCelFunction.FirstOrDefault(x => x.Value == minElemInColumn).Key);
  202. decimal controlElement = tableAll[key, minElemIndex];
  203. WorkWithTable(controlElement, key, minElemIndex);
  204. }
  205. }
  206. private void WorkWithTable(decimal controlElement, int indexRowControlElement, int indexColumnControlElement)
  207. {
  208. tableAll[indexRowControlElement, 0] = tableAll[0, indexColumnControlElement];
  209. for (int i = 1; i < tableAll.GetLength(1); i++)
  210. {
  211. tableAll[indexRowControlElement, i] /= controlElement;
  212. }
  213. for (int i = 1; i < tableAll.GetLength(0); i++)
  214. {
  215. if (i == indexRowControlElement) continue;
  216. decimal valueToNull = tableAll[indexRowControlElement, indexColumnControlElement] * -(tableAll[i, indexColumnControlElement]);
  217. for (global::System.Int32 j = 1; j < tableAll.GetLength(1); j++)
  218. {
  219. tableAll[i, j] += tableAll[indexRowControlElement, j] * valueToNull;
  220. }
  221. }
  222. PrintTable();
  223. }
  224. }
  225. }