Advertisement
MadCortez

Untitled

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