Advertisement
MadCortez

Untitled

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