Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Lesson3
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int[,] matrixA = new int[3, 3];
  10. int rows = matrixA.GetLength(0);
  11. int cols = matrixA.GetLength(1);
  12. int result;
  13.  
  14. string[] userInput;
  15.  
  16.  
  17. Console.Write("Работа с матрицей." +
  18. "\nДля экспериментов необходима матрица 3х3." +
  19. "\nЕсли хотите сами заполнить матрицу, введите \"Да\", иначе заполним случайными числами: ");
  20.  
  21. bool isManual = Console.ReadLine().ToLower() == "да";
  22.  
  23. Console.Clear();
  24.  
  25. bool isInputCorrect = false;
  26. Random random = new Random();
  27.  
  28. if (isManual)
  29. {
  30. Console.WriteLine($"Введите {matrixA.Length} целых чисел через пробел:");
  31. userInput = Console.ReadLine().Split(' ');
  32.  
  33. isInputCorrect = matrixA.Length == userInput.Length;
  34.  
  35. if (isInputCorrect)
  36. {
  37. int currentRow;
  38. int currentCol;
  39.  
  40. for (int i = 0; i < matrixA.Length; i++)
  41. {
  42. currentRow = i / rows;
  43. currentCol = i % rows;
  44.  
  45. matrixA[currentRow, currentCol] = Convert.ToInt32(userInput[i]);
  46. }
  47. }
  48. else
  49. {
  50. Console.WriteLine("Введено неверное число символов. Матрица заполнена случайными числами.");
  51.  
  52. for (int i = 0; i < rows; i++)
  53. {
  54. for (int j = 0; j < cols; j++)
  55. {
  56. matrixA[i, j] = random.Next(0, 10);
  57. }
  58. }
  59. }
  60. }
  61. else
  62. {
  63. Console.WriteLine("Матрица заполнена случайными числами.");
  64.  
  65. for (int i = 0; i < rows; i++)
  66. {
  67. for (int j = 0; j < cols; j++)
  68. {
  69. matrixA[i, j] = random.Next(0, 10);
  70. }
  71. }
  72. }
  73.  
  74. Console.WriteLine("Работаем с матрицей:");
  75.  
  76. for (int i = 0; i < rows; i++)
  77. {
  78. for (int j = 0; j < cols; j++)
  79. {
  80. Console.Write($"{matrixA[i, j]}\t");
  81. }
  82. Console.WriteLine();
  83. }
  84.  
  85. result = 0;
  86. for (int j = 0; j < cols; j++)
  87. {
  88. result += matrixA[1, j];
  89. }
  90. Console.WriteLine($"Сумма чисел второй строки {result}.");
  91.  
  92. result = 1;
  93. for (int i = 0; i < rows; i++)
  94. {
  95. result *= matrixA[i, 0];
  96. }
  97. Console.WriteLine($"Произведение чисел первого столбца {result}.");
  98.  
  99. char sign;
  100. string dimension;
  101. int lineNumber;
  102. int numbersInLine = 0;
  103. int maxLineNumber = 0;
  104.  
  105. const char signPlus = '+';
  106. const char signMultiplication = '*';
  107.  
  108. const string rowName = "строка";
  109. const string colName = "столбец";
  110.  
  111. bool quit;
  112.  
  113. do
  114. {
  115. Console.WriteLine("\nЧто посчитаем дальше?");
  116.  
  117. do
  118. {
  119. Console.Write("Что будем делать с числами (\"+\", \"*\")?: ");
  120. sign = Console.ReadLine()[0];
  121.  
  122. isInputCorrect = sign == signPlus ||
  123. sign == signMultiplication;
  124.  
  125. if (!isInputCorrect)
  126. {
  127. Console.WriteLine("Введен неверный знак, попробуйте еще раз.");
  128. }
  129.  
  130. } while (!isInputCorrect);
  131.  
  132. do
  133. {
  134. Console.Write("С числами в строке или столбце будем производить действия (\"строка\", \"столбец\"): ");
  135. dimension = Console.ReadLine().ToLower();
  136.  
  137. switch (dimension)
  138. {
  139. case rowName:
  140. numbersInLine = cols;
  141. maxLineNumber = rows - 1;
  142. isInputCorrect = true;
  143. break;
  144.  
  145. case colName:
  146. numbersInLine = rows;
  147. maxLineNumber = cols - 1;
  148. isInputCorrect = true;
  149. break;
  150.  
  151. default:
  152. Console.WriteLine("Введена неверная команда, попробуйте еще раз");
  153. isInputCorrect = false;
  154. break;
  155. }
  156. } while (!isInputCorrect);
  157.  
  158. do
  159. {
  160. Console.Write($"Введите номер линии, с числами которой работаем - {dimension} №: ");
  161. lineNumber = Convert.ToInt32(Console.ReadLine().ToLower());
  162.  
  163. if (lineNumber <= maxLineNumber)
  164. {
  165. Console.WriteLine($"\nРаботаем с числами из {dimension} № {lineNumber}" +
  166. $"\nПрименяем операцию \"{sign}\"\n");
  167. isInputCorrect = true;
  168. }
  169. else
  170. {
  171. isInputCorrect = false;
  172. Console.WriteLine("Введен неверный номер, попробуйте еще раз.");
  173. }
  174.  
  175. } while (!isInputCorrect);
  176.  
  177. result = 0;
  178.  
  179. switch (sign)
  180. {
  181. case signPlus:
  182. if (dimension == rowName)
  183. {
  184. for (int i = 0; i < numbersInLine; i++)
  185. {
  186. result += matrixA[lineNumber, i];
  187. }
  188. }
  189. else
  190. {
  191. for (int i = 0; i < numbersInLine; i++)
  192. {
  193. result += matrixA[i, lineNumber];
  194. }
  195. }
  196. break;
  197.  
  198. case signMultiplication:
  199. result = 1;
  200. if (dimension == rowName)
  201. {
  202. for (int i = 0; i < numbersInLine; i++)
  203. {
  204. result *= matrixA[lineNumber, i];
  205. }
  206. }
  207. else
  208. {
  209. for (int i = 0; i < numbersInLine; i++)
  210. {
  211. result *= matrixA[i, lineNumber];
  212. }
  213. }
  214. break;
  215.  
  216. default:
  217. Console.WriteLine("Ошибка!");
  218. break;
  219. }
  220.  
  221. Console.WriteLine($"Результат операции {result}\n");
  222.  
  223. Console.Write("Чтобы посчитать еще введите \"да\", при вводе других символов программа завершится");
  224. quit = Console.ReadLine().ToLower() != "да";
  225.  
  226. } while (!quit);
  227. }
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement