Advertisement
MadCortez

Untitled

Oct 27th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 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. void userInputArrayFromConsole(int n);
  9. void userInputFromConsole();
  10. double** gauss(double** matrix);
  11. void userInputFromFile(string path);
  12. bool checkPath(string path);
  13. void userInputPath();
  14. void inputMethod();
  15. void printWithoutPath(double** matrix);
  16. string userOutputPath();
  17. void printWithPath(double** matrix);
  18. void outputMethod(double** matrix);
  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. void userInputArrayFromConsole(int n) {
  34.     const int MIN_VALUE = -500;
  35.     const int MAX_VALUE = 500;
  36.     double** matrix = new double* [n];
  37.     cout << "Введите коэффициенты в диапазоне " << MIN_VALUE << ".." << MAX_VALUE << ":" << endl;
  38.     for (int i = 0; i < n; i++) {
  39.         matrix[i] = new double[n + 1];
  40.         for (int j = 0; j < n; j++) {
  41.             matrix[i][j] = inputValue(MIN_VALUE, MAX_VALUE);
  42.         }
  43.     }
  44.     cout << "Введите свободные члены в диапазоне " << MIN_VALUE << ".." << MAX_VALUE << ":" << endl;
  45.     for (int i = 0; i < n; i++)
  46.         matrix[i][n] = inputValue(MIN_VALUE, MAX_VALUE);
  47.     outputMethod(gauss(matrix));
  48. }
  49.  
  50. void userInputFromConsole() {
  51.     const int MIN_SIZE = 2;
  52.     const int MAX_SIZE = 20;
  53.     int n;
  54.     cout << "Введите порядок матрицы в диапазоне " << MIN_SIZE << ".." << MAX_SIZE << ": ";
  55.     n = inputValue(MIN_SIZE, MAX_SIZE);
  56.     userInputArrayFromConsole(n);
  57. }
  58.  
  59. double** gauss(double** matrix) {
  60.     int n = _msize(matrix) / sizeof(matrix);
  61.     for (int k = 0; k < n; k++)
  62.         for (int j = k + 1; j < n; j++) {
  63.             double temp = matrix[j][k] / matrix[k][k];
  64.             for (int i = k; i < n; i++)
  65.                 matrix[j][i] = matrix[j][i] - temp * matrix[k][i];
  66.             matrix[j][n] = matrix[j][n] - temp * matrix[k][n];
  67.         }
  68.     return matrix;
  69. }
  70.  
  71. void userInputFromFile(string path) {
  72.     int n;
  73.     ifstream file(path);
  74.     file.open(path);
  75.     file.clear();
  76.     file >> n;
  77.     double** matrix = new double* [n];
  78.     for (int i = 0; i < n; i++) {
  79.         matrix[i] = new double[n + 1];
  80.         for (int j = 0; j < n; j++)
  81.             file >> matrix[i][j];
  82.     }
  83.     for (int i = 0; i < n; i++)
  84.         file >> matrix[i][n];
  85.     file.close();
  86.     outputMethod(gauss(matrix));
  87. }
  88.  
  89. bool checkPath(string path) {
  90.     ifstream file(path);
  91.     if (file.is_open()) {
  92.         cout << path << " найден" << endl;
  93.         return true;
  94.     }
  95.     else {
  96.         cout << path << " не найден" << endl;
  97.         return false;
  98.     }
  99. }
  100.  
  101. void userInputPath() {
  102.     string path;
  103.     bool isNotValid = false;
  104.     do {
  105.         cout << "Введите абсолютный путь к файлу с входными данными" << endl;
  106.         cin >> path;
  107.     } while (!checkPath(path));
  108.     userInputFromFile(path);
  109. }
  110.  
  111. void inputMethod() {
  112.     int method;
  113.     cout << "Каким способом хотите ввести данные?" << endl;
  114.     cout << "1 - с помощью консоли" << endl;
  115.     cout << "2 - с помощью файла" << endl;
  116.     do {
  117.         cin >> method;
  118.         switch (method) {
  119.         case 1: { userInputFromConsole(); break; }
  120.         case 2: { userInputPath(); break; }
  121.         default: cout << "Введите корректный способ ввода";
  122.         }
  123.     } while (method != 2 && method != 1);
  124. }
  125.  
  126. void printWithoutPath(double** matrix) {
  127.     cout << "После «прямого хода»" << endl;
  128.     for (int i = 0; i < _msize(matrix) / sizeof(matrix); i++) {
  129.         for (int j = 0; j < _msize(matrix) / sizeof(matrix); j++)
  130.             cout << matrix[i][j] << " ";
  131.         cout << endl;
  132.     }
  133.     for (int i = 0; i < _msize(matrix) / sizeof(matrix); i++)
  134.         cout << matrix[i][_msize(matrix) / sizeof(matrix)] << endl;
  135. }
  136.  
  137. string userOutputPath() {
  138.     string path;
  139.     cout << "Введите абсолютный путь к файлу для вывода результата" << endl;
  140.     cin >> path;
  141.     cout << "Результат работа помещён в файл";
  142.     return path;
  143. }
  144.  
  145. void printWithPath(double** matrix) {
  146.     string path = userOutputPath();
  147.     ofstream file(path);
  148.     for (int i = 0; i < _msize(matrix) / sizeof(matrix); i++) {
  149.         for (int j = 0; j < _msize(matrix) / sizeof(matrix); j++)
  150.             file << matrix[i][j] << " ";
  151.         file << endl;
  152.     for (int i = 0; i < _msize(matrix) / sizeof(matrix); i++)
  153.         cout << matrix[i][_msize(matrix) / sizeof(matrix)] << endl;
  154.     }
  155.     cout << "Результат работы помещён в файл";
  156. }
  157.  
  158. void outputMethod(double** matrix) {
  159.     int method;
  160.     cout << "Куда хотите вывести результат?" << endl;
  161.     cout << "1 - в консоль" << endl;
  162.     cout << "2 - в файл" << endl;
  163.     do {
  164.         cin >> method;
  165.         switch (method) {
  166.         case 1: { printWithoutPath(matrix); break; }
  167.         case 2: { printWithPath(matrix); break; }
  168.         default: cout << "Введите корректный способ ввода";
  169.         }
  170.     } while (method != 2 && method != 1);
  171. }
  172.  
  173. void printTask() {
  174.     cout << "Данная программа выполняет «прямой ход» в решении системы линейных алгебраических уравнений методом Гаусса" << endl;
  175. }
  176.  
  177. int main()
  178. {
  179.     setlocale(LC_ALL, "Russian");
  180.     printTask();
  181.     inputMethod();
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement