Advertisement
believe_me

Untitled

May 27th, 2022
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. #include <list>
  6. #include <iomanip>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int inputIntegerNumber(const int MIN_NUMBER, const int MAX_NUMBER) {
  13.     bool IsIncorrect;
  14.     int Number;
  15.     string Input = "";
  16.  
  17.     do {
  18.         getline(cin, Input);
  19.         IsIncorrect = false;
  20.         try {
  21.             Number = stoi(Input);
  22.         }
  23.         catch (invalid_argument ex) {
  24.             cout << "Введите число:\n";
  25.             IsIncorrect = true;
  26.         }
  27.         catch (out_of_range ex) {
  28.             cout << "Число не должно быть меньше " << MIN_NUMBER << " и больше, чем "
  29.                 << MAX_NUMBER << "\n";
  30.             IsIncorrect = true;
  31.         }
  32.         if (!IsIncorrect && (Number <  MIN_NUMBER || Number > MAX_NUMBER)) {
  33.             cout << "Число не должно быть меньше " << MIN_NUMBER << " и больше, чем "
  34.                 << MAX_NUMBER << "\n";
  35.             IsIncorrect = true;
  36.         }
  37.     } while (IsIncorrect);
  38.  
  39.     return Number;
  40. }
  41.  
  42. bool checkFile(const string PathToFile) {
  43.     bool Flag = true;
  44.     int CurrentNumber;
  45.     ifstream InputFile(PathToFile);
  46.     while ((Flag) && (!InputFile.eof())) {
  47.         try {
  48.             InputFile >> CurrentNumber;
  49.         }
  50.         catch (invalid_argument ex) {
  51.             cout << "Некорректные данные в файле.\n";
  52.             Flag = false;
  53.         }
  54.         catch (out_of_range ex) {
  55.             cout << "Некорректные данные в файле.\n";
  56.             Flag = false;
  57.         }
  58.         if (Flag && (CurrentNumber < 1 || CurrentNumber > 20)) {
  59.             cout << "Некорректные данные в файле.\n";
  60.             Flag = false;
  61.         }
  62.     }
  63.     InputFile.close();
  64.     return Flag;
  65. }
  66.  
  67. bool checkPermissionForWriting(const string& PathToFile) {
  68.     bool Flag;
  69.     ofstream FileOut(PathToFile);
  70.  
  71.     if (FileOut.is_open()) {
  72.         FileOut.close();
  73.         Flag = true;
  74.     }
  75.     else {
  76.         cout << "Файл не доступен для записи.\n";
  77.         Flag = false;
  78.     }
  79.  
  80.     return Flag;
  81. }
  82.  
  83.  
  84. bool checkExtension(const string& PathToFile) {
  85.     const string extension = "txt";
  86.     bool Flag;
  87.  
  88.     if (PathToFile.length() > 4 && PathToFile.substr(PathToFile.length() - 3) == extension) {
  89.         Flag = true;
  90.     }
  91.     else {
  92.         cout << "Неверное расширение.\n";
  93.         Flag = false;
  94.     }
  95.  
  96.     return Flag;
  97. }
  98.  
  99. bool checkPermissionForReading(const string& PathToFile) {
  100.     bool Flag;
  101.     ifstream FileIn(PathToFile);
  102.     if (FileIn.is_open()) {
  103.         FileIn.close();
  104.         Flag = true;
  105.     }
  106.     else {
  107.         cout << "Файл не доступен для чтения.\n";
  108.         Flag = false;
  109.     }
  110.     return Flag;
  111. }
  112.  
  113. const string inputPathToFileForReading() {
  114.     string PathToFile;
  115.     do {
  116.         cout << "Введите путь к файлу для чтения:\n";
  117.         getline(cin, PathToFile);
  118.     } while (!checkExtension(PathToFile) || !checkPermissionForReading(PathToFile) || !checkFile(PathToFile));
  119.     return PathToFile;
  120. }
  121.  
  122. const string inputPathToFileForWriting() {
  123.     string PathToFile;
  124.     do {
  125.         cout << "Введите путь к файлу для записи: \n";
  126.         getline(cin, PathToFile);
  127.     } while (!checkExtension(PathToFile) || !checkPermissionForWriting(PathToFile));
  128.     return PathToFile;
  129. }
  130.  
  131. typedef vector<list<int>> TListArray;
  132. typedef vector<vector<int>> TMatrix;
  133.  
  134.  
  135. void printListArray(TListArray& ListArray) {
  136.     cout << "Списки инцидентности:\n";
  137.     for (int i = 0; i < ListArray.size(); i++) {
  138.         cout << i + 1 << ": ";
  139.         for (auto Iterator : ListArray[i])
  140.             cout << "->" << Iterator;
  141.         cout << '\n';
  142.     }
  143. }
  144.  
  145. void writeIncidentMatrix(TMatrix& IncidentMatrix, ofstream& FileOut) {
  146.     FileOut << "Матрица инциденций:\n" << "   ";
  147.     for (int i = 0; i < IncidentMatrix[0].size(); i++) {
  148.         FileOut << setw(2) << (char)(i + 97) << ' ';
  149.     }
  150.     FileOut << '\n';
  151.     for (int i = 0; i < IncidentMatrix.size(); i++) {
  152.         FileOut << i + 1 << ": ";
  153.         for (int j = 0; j < IncidentMatrix[i].size(); j++)
  154.             FileOut << setw(2) << IncidentMatrix[i][j] << " ";
  155.         FileOut << '\n';
  156.     }
  157.     FileOut << '\n';
  158. }
  159.  
  160. void saveToFile(TMatrix& IncidentMatrix) {
  161.     char Answer;
  162.     cout << "Введите '1', если хотите сохранить в файл.\n";
  163.     cin >> Answer;
  164.     cin.ignore(256, '\n');
  165.     if (Answer == '1') {
  166.         string PathToFile = inputPathToFileForWriting();
  167.         ofstream FileOut(PathToFile);
  168.         writeIncidentMatrix(IncidentMatrix, FileOut);
  169.         cout << "Матрица сохранена в файл.\n";
  170.     }
  171.     cout << "Программа завершена.";
  172. }
  173.  
  174. void printIncidentMatrix(TMatrix& IncidentMatrix) {
  175.     cout << "Матрица инциденций:\n" << "   ";
  176.     for (int i = 0; i < IncidentMatrix[0].size(); i++) {
  177.         cout << setw(2) << (char)(i + 97) << ' ';
  178.     }
  179.     cout << '\n';
  180.     for (int i = 0; i < IncidentMatrix.size(); i++) {
  181.         cout << i + 1 << ": ";
  182.         for (int j = 0; j < IncidentMatrix[i].size(); j++)
  183.             cout << setw(2) << IncidentMatrix[i][j] << " ";
  184.         cout << '\n';
  185.     }
  186.     cout << '\n';
  187. }
  188.  
  189. void addVertex(TListArray &ListArray, int Index, int VertexNumber) {
  190.     ListArray[Index].push_back(VertexNumber);
  191. }
  192.  
  193. vector<int> getVertexNumber(string Buffer) {
  194.     vector<int> VertexNumbers;
  195.     string Number;
  196.     int j;
  197.     for (int i = 1; i < Buffer.size(); i++) {
  198.         if (Buffer[i] != ' ') {
  199.             j = i;
  200.             while ((Buffer[j] != ' ') && j < Buffer.size())
  201.                 Number += Buffer[j++];
  202.             if (Number != "")
  203.                 VertexNumbers.push_back(stoi(Number));
  204.             i = j;
  205.             Number = "";
  206.         }
  207.     }
  208.     return VertexNumbers;
  209. }
  210.  
  211. void readFormFile(TListArray& ListArray, string PathToFile) {
  212.     int NumberOfVertices, VertexNumber, i;
  213.     string Buffer = "";
  214.     vector<int> VertexNumbers;
  215.     i = -1;
  216.     ifstream FileIn(PathToFile);
  217.     FileIn >> NumberOfVertices;
  218.     ListArray.resize(NumberOfVertices);
  219.     while (!FileIn.eof()) {
  220.         getline(FileIn, Buffer);
  221.         VertexNumbers = getVertexNumber(Buffer);
  222.         for (int j = 0; j < VertexNumbers.size(); j++)
  223.             addVertex(ListArray, i, VertexNumbers[j]);
  224.         i++;
  225.     }
  226.     FileIn.close();
  227. }
  228.  
  229. int inputVertexNumber(int WrongNumber, int MaxIndexOfVertices) {
  230.     bool IsIncorrect = true;
  231.     int VertexNumber;
  232.     do {
  233.         cout << "Введите номер следующей инцидентной вершины: ";
  234.         VertexNumber = inputIntegerNumber(1, MaxIndexOfVertices);
  235.         if (VertexNumber == WrongNumber) {
  236.             IsIncorrect = true;
  237.             cout << "Текущая вершина не может быть инцидентна данной.\n";
  238.         }
  239.         else{
  240.             IsIncorrect = false;
  241.         }
  242.         cout << '\n';
  243.     } while (IsIncorrect);
  244.     return VertexNumber;
  245. }
  246.  
  247. void receiveListReprezentation(TListArray& ListArray) {
  248.     int NumberOfVertices, VertexNumber, CurrentNumberOfVertices;
  249.     cout << "Введите количество вершин:\n";
  250.     NumberOfVertices = inputIntegerNumber(1, 10);
  251.     ListArray.resize(NumberOfVertices);
  252.     for (int i = 0; i < NumberOfVertices; i++) {
  253.         cout << "Введите количество вершин, смежных с вершиной " << i + 1 << ":\n";
  254.         CurrentNumberOfVertices = inputIntegerNumber(0, NumberOfVertices - 1);
  255.         for (int j = 0; j < CurrentNumberOfVertices; j++) {
  256.             VertexNumber = inputVertexNumber(i + 1, NumberOfVertices);
  257.             addVertex(ListArray, i, VertexNumber);
  258.         }
  259.     }
  260. }
  261.  
  262. void receiveGraph(TListArray& ListArray) {
  263.     char Answer = 'q';
  264.     cout << "Введите '1', если хотите счиать из файла. '2' - из консоли.\n";
  265.     do {
  266.         cin >> Answer;
  267.         cin.ignore(256, '\n');
  268.         if (Answer == '1') {
  269.             string PathToFile = inputPathToFileForReading();
  270.             readFormFile(ListArray, PathToFile);
  271.             cout << "Граф получен.\n";
  272.         }
  273.         else
  274.             if (Answer == '2') {
  275.                 receiveListReprezentation(ListArray);
  276.                 cout << "Граф получен.\n";
  277.             }
  278.             else
  279.                 cout << "Введите '1' или '2'.\n";
  280.     } while ((Answer != '1') && (Answer != '2'));
  281. }
  282.  
  283. void resizeIncidentMatrix(TMatrix& IncidentMatrix, int NumberOfColumns) {
  284.     for (int i = 0; i < IncidentMatrix.size(); i++) {
  285.         IncidentMatrix[i].resize(NumberOfColumns);
  286.     }
  287. }
  288.  
  289. TMatrix receiveAdjacencyMatrix(TListArray& ListArray){
  290.     TMatrix AdjacencyMatrix(ListArray.size(), vector<int>(ListArray.size(), 0));
  291.     for (int i = 0; i < ListArray.size(); i++) {
  292.         for (auto const& Iterator : ListArray[i]) {
  293.             AdjacencyMatrix[i][Iterator - 1] = 1;
  294.         }
  295.     }
  296.     return AdjacencyMatrix;
  297. }
  298.  
  299. TMatrix receiveIncidentMatrix(TListArray& ListArray) {
  300.     TMatrix AdjacencyMatrix = receiveAdjacencyMatrix(ListArray);
  301.     TMatrix IncidentMatrix(ListArray.size(), vector<int>((ListArray.size() * (ListArray.size() - 1) / 2), 0));
  302.     int ColCount = 0;
  303.     for (int i = 0; i < AdjacencyMatrix.size(); i++) {
  304.         for (int j = 0; j < AdjacencyMatrix.size(); j++) {
  305.             if (AdjacencyMatrix[i][j] == 1) {
  306.                 IncidentMatrix[j][ColCount] = 1;
  307.                 if (AdjacencyMatrix[j][i] == 1)
  308.                     IncidentMatrix[i][ColCount] = 1;
  309.                 else
  310.                     IncidentMatrix[i][ColCount] = -1;
  311.                 AdjacencyMatrix[j][i] = 0;
  312.                 ColCount++;
  313.             }
  314.         }
  315.     }
  316.     resizeIncidentMatrix(IncidentMatrix, ColCount);
  317.     return IncidentMatrix;
  318. }
  319.  
  320. int main() {
  321.     system("chcp 1251");
  322.     TListArray ListArray;
  323.     receiveGraph(ListArray);
  324.     printListArray(ListArray);
  325.     TMatrix IncidentMatrix = receiveIncidentMatrix(ListArray);
  326.     printIncidentMatrix(IncidentMatrix);
  327.     saveToFile(IncidentMatrix);
  328.     return 0;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement