Advertisement
MadCortez

Untitled

Mar 17th, 2021
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int inputValue(int min, int max);
  8. int userInputFromConsole();
  9. double** userInputFromFile(string path);
  10. bool checkPath(string path);
  11. string userInputPath();
  12. int inputMethod();
  13. void printInConsole(double opr);
  14. string userOutputPath();
  15. void printInFile(double opr, string path);
  16. int outputMethod();
  17. void start();
  18. void printTask();
  19.  
  20. int inputValue(int min, int max) {
  21.     int currentValue;
  22.     bool isNotValid = true;
  23.     do {
  24.         cin >> currentValue;
  25.         if (currentValue >= min && currentValue <= max)
  26.             isNotValid = false;
  27.         else
  28.             cout << "Введите число в заданном диапазоне\n";
  29.     } while (isNotValid);
  30.     return currentValue;
  31. }
  32.  
  33. int userInputFromConsole() {
  34.     const int MIN_SIZE = 2;
  35.     const int MAX_SIZE = 100;
  36.     int num;
  37.     cout << "Введите порядок матрицы в диапазоне " << MIN_SIZE << ".." << MAX_SIZE << ": ";
  38.     num = inputValue(MIN_SIZE, MAX_SIZE);
  39.     return num;
  40. }
  41.  
  42. double** userInputMatrixFromConsole(int num) {
  43.     const int MIN_VALUE = -10000;
  44.     const int MAX_VALUE = 10000;
  45.     cout << "Введите элементы матрицы в диапазоне " << MIN_VALUE << ".." << MAX_VALUE << ": " << endl;
  46.     double** matrix = new double*[num];
  47.     for (int i = 0; i < num; i++) {
  48.         matrix[i] = new double[num];
  49.         for (int j = 0; j < num; j++)
  50.             matrix[i][j] = inputValue(MIN_VALUE, MAX_VALUE);
  51.     }
  52.     return matrix;
  53. }
  54.  
  55. double** userInputFromFile(string path) {
  56.     int num;
  57.     ifstream file(path);
  58.     file.open(path);
  59.     file.clear();
  60.     file >> num;
  61.     double** matrix = new double*[num];
  62.     for (int i = 0; i < num; i++) {
  63.         matrix[i] = new double[num];
  64.         for (int j = 0; j < num; j++)
  65.             file >> matrix[i][j];
  66.     }
  67.     file.close();
  68.     return matrix;
  69. }
  70.  
  71. bool checkPath(string path) {
  72.     ifstream file(path);
  73.     if (file.is_open()) {
  74.         cout << path << " найден" << endl;
  75.         return true;
  76.     }
  77.     else {
  78.         cout << path << " не найден" << endl;
  79.         return false;
  80.     }
  81. }
  82.  
  83. string userInputPath() {
  84.     string path;
  85.     bool isNotValid = false;
  86.     do {
  87.         cout << "Введите абсолютный путь к файлу с входными данными" << endl;
  88.         cin >> path;
  89.     } while (!checkPath(path));
  90.     return path;
  91. }
  92.  
  93. int inputMethod() {
  94.     int method;
  95.     cout << "Каким способом хотите ввести данные?" << endl;
  96.     cout << "1 - с помощью консоли" << endl;
  97.     cout << "2 - с помощью файла" << endl;
  98.     do {
  99.         cin >> method;
  100.         if (method != 1 && method != 2)
  101.             cout << "Введите 1 или 2" << endl;
  102.     } while (method != 2 && method != 1);
  103.     return method;
  104. }
  105.  
  106. void printInConsole(double opr) {
  107.         cout << opr;
  108. }
  109.  
  110. string userOutputPath() {
  111.     string path;
  112.     cout << "Введите абсолютный путь к файлу для вывода результата" << endl;
  113.     cin >> path;
  114.     cout << "Результат работа помещён в файл";
  115.     return path;
  116. }
  117.  
  118. void printInFile(double opr, string path) {
  119.     ofstream file(path);
  120.     file << opr;
  121. }
  122.  
  123. int outputMethod() {
  124.     int method;
  125.     cout << "Куда хотите вывести результат?" << endl;
  126.     cout << "1 - в консоль" << endl;
  127.     cout << "2 - в файл" << endl;
  128.     do {
  129.         cin >> method;
  130.         if (method != 1 && method != 2)
  131.             cout << "Введите 1 или 2" << endl;
  132.     } while (method != 2 && method != 1);
  133.     return method;
  134. }
  135.  
  136. double** userInput() {
  137.     int num;
  138.     double** matrix;
  139.     short method = inputMethod();
  140.     if (method == 1) {
  141.         num = userInputFromConsole();
  142.         matrix = userInputMatrixFromConsole(num);
  143.     }
  144.     else {
  145.         string path = userInputPath();
  146.         matrix = userInputFromFile(path);
  147.     }
  148.     return matrix;
  149. }
  150.  
  151. void printResult(double opr) {
  152.     short method = outputMethod();
  153.     if (method == 1)
  154.         printInConsole(opr);
  155.     else {
  156.         string path = userOutputPath();
  157.         printInFile(opr, path);
  158.     }
  159. }
  160.  
  161. int calcZnak(int colPer) {
  162.     if (colPer % 2 == 0)
  163.         return 1;
  164.     else
  165.         return -1;
  166. }
  167.  
  168. void per(int k, int n, double** matrix, int colPer) {
  169.     double z = abs(matrix[k][k]);
  170.     int i = k;
  171.     colPer = 0;
  172.     for (int j = k + 1; j < n; j++)
  173.         if (abs(matrix[j][k]) > z) {
  174.             z = abs(matrix[j][k]);
  175.             i = j;
  176.         }
  177.     if (i > k) {
  178.         colPer++;
  179.         for (int j = k; j < n; j++) {
  180.             z = matrix[i][j];
  181.             matrix[i][j] = matrix[k][j];
  182.             matrix[k][j] = z;
  183.         }
  184.     }
  185. }
  186.  
  187. double calcOpr(int size, double** matrix, double det) {
  188.     det = 1;
  189.     int colPer = 0;
  190.     for (int i = 0; i < size; i++) {
  191.         if (matrix[i][i] == 0)
  192.             per(i, size, matrix, colPer);
  193.         det = calcZnak(colPer) * det * matrix[i][i];
  194.         for (int j = i + 1; j < size; j++) {
  195.             double r = matrix[j][i] / matrix[i][i];
  196.             for (int k = i; k < size; k++)
  197.                 matrix[j][k] = matrix[j][k] - r * matrix[i][k];
  198.         }
  199.     }
  200.     return det;
  201. }
  202.  
  203. void printTask() {
  204.     cout << "Данная программа вычисляет определитель матрицы" << endl;
  205. }
  206.  
  207. void start() {
  208.     printTask();
  209.     double** matrix = userInput();
  210.     double opr = calcOpr(_msize(matrix) / sizeof(matrix[0]), matrix, 0);
  211.     printResult(opr);
  212. }
  213.  
  214. int main()
  215. {
  216.     setlocale(LC_ALL, "Russian");
  217.     start();
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement