Advertisement
believe_me

Untitled

Apr 5th, 2022
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <iomanip>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. typedef vector<vector<int>> TSquare;
  11.  
  12.  
  13. int inputDoubleEvenNumber(const int MIN_NUMBER, const int MAX_NUMBER) {
  14.     bool IsIncorrect;
  15.     int Number;
  16.     string Input = "";
  17.  
  18.     do {
  19.         getline(cin, Input);
  20.         IsIncorrect = false;
  21.         try {
  22.             Number = stoi(Input);
  23.         }
  24.         catch (invalid_argument ex) {
  25.             cout << "Введите число:\n";
  26.             IsIncorrect = true;
  27.         }
  28.         catch (out_of_range ex) {
  29.             cout << "Число не должно быть меньше " << MIN_NUMBER << " и больше, чем "
  30.                 << MAX_NUMBER << "\n";
  31.             IsIncorrect = true;
  32.         }
  33.         if (!IsIncorrect && (Number <  MIN_NUMBER || Number > MAX_NUMBER)) {
  34.             cout << "Число не должно быть меньше " << MIN_NUMBER << " и больше, чем "
  35.                 << MAX_NUMBER << "\n";
  36.             IsIncorrect = true;
  37.         }
  38.         if (!IsIncorrect && ((Number % 4) != 0)) {
  39.             cout << "Число должно делиться на 4.\n";
  40.             IsIncorrect = true;
  41.         }
  42.     } while (IsIncorrect);
  43.  
  44.     return Number;
  45. }
  46.  
  47.  
  48. int inputIntegerNumber(const int MIN_NUMBER, const int MAX_NUMBER) {
  49.     bool IsIncorrect;
  50.     int Number;
  51.     string Input = "";
  52.  
  53.     do {
  54.         getline(cin, Input);
  55.         IsIncorrect = false;
  56.         try {
  57.             Number = stoi(Input);
  58.         }
  59.         catch (invalid_argument ex) {
  60.             cout << "Введите число:\n";
  61.             IsIncorrect = true;
  62.         }
  63.         catch (out_of_range ex) {
  64.             cout << "Число не должно быть меньше " << MIN_NUMBER << " и больше, чем "
  65.                 << MAX_NUMBER << "\n";
  66.             IsIncorrect = true;
  67.         }
  68.         if (!IsIncorrect && (Number <  MIN_NUMBER || Number > MAX_NUMBER)) {
  69.             cout << "Число не должно быть меньше " << MIN_NUMBER << " и больше, чем "
  70.                 << MAX_NUMBER << "\n";
  71.             IsIncorrect = true;
  72.         }
  73.     } while (IsIncorrect);
  74.  
  75.     return Number;
  76. }
  77.  
  78.  
  79. bool checkPermissionForWriting(const string& PathToFile) {
  80.     bool Flag;
  81.     ofstream FileOut(PathToFile);
  82.     if (FileOut.is_open()) {
  83.         FileOut.close();
  84.         Flag = true;
  85.     }
  86.     else {
  87.         cout << "Файл не доступен для записи.\n";
  88.         Flag = false;
  89.     }
  90.     return Flag;
  91. }
  92.  
  93.  
  94. bool checkExtension(const string& PathToFile) {
  95.     const string extension = "txt";
  96.     bool Flag;
  97.  
  98.     if (PathToFile.length() > 4 && PathToFile.substr(PathToFile.length() - 3) == extension) {
  99.         Flag = true;
  100.     }
  101.     else {
  102.         cout << "Неверное расширение.\n";
  103.         Flag = false;
  104.     }
  105.  
  106.     return Flag;
  107. }
  108.  
  109.  
  110. const string inputPathToFileForWriting() {
  111.     string PathToFile;
  112.  
  113.     do {
  114.         cout << "Введите путь к файлу для записи: \n";
  115.         getline(cin, PathToFile);
  116.     } while (!checkExtension(PathToFile) || !checkPermissionForWriting(PathToFile));
  117.  
  118.     return PathToFile;
  119. }
  120.  
  121.  
  122. int findSum(TSquare& MagicSquare) {
  123.     int Sum = 0;
  124.     int Size = MagicSquare.size();
  125.  
  126.     for (int j = 0; j < Size; j++)
  127.         Sum += MagicSquare[0][j];
  128.     return Sum;
  129. }
  130.  
  131.  
  132. void saveToFile(TSquare MagicSquare){
  133.     if (MagicSquare.size() != 1) {
  134.         string path = inputPathToFileForWriting();
  135.         ofstream fileOut;
  136.         fileOut.open(path, ios::trunc);
  137.         int Sum = findSum(MagicSquare);
  138.  
  139.         fileOut << "Магический квадрат:\n";
  140.         for (int i = 0; i < MagicSquare.size(); i++) {
  141.             for (int j = 0; j < MagicSquare[i].size(); j++)
  142.                 fileOut << setw(4) << MagicSquare[i][j] << " ";
  143.             fileOut << '\n';
  144.         }
  145.         fileOut << "Сумма элемнетов на каждой линии: " << to_string(Sum);
  146.  
  147.         fileOut.close();
  148.         cout << "Информация записана в файл.\n";
  149.     }
  150.     else
  151.         cout << "Магический квадрат не построен.\n";
  152. }
  153.  
  154.  
  155. void printSquare(TSquare& Square) {
  156.     for (int i = 0; i < Square.size(); i++) {
  157.         for (int j = 0; j < Square[i].size(); j++)
  158.             cout << setw(4) << Square[i][j] << " ";
  159.         cout << '\n';
  160.     }
  161. }
  162.  
  163.  
  164. void buildSquare(TSquare& StartSquare, int Size) {
  165.     StartSquare.resize(Size);
  166.  
  167.     for (int i = 0; i < Size; i++) {
  168.         StartSquare[i].resize(Size);
  169.     }
  170. }
  171.  
  172.  
  173. void fillLowerPartOfFirstSquare(TSquare& FirstLatinSquare) {
  174.     int Size = FirstLatinSquare.size();
  175.  
  176.     for (int i = (Size / 2); i < Size; i++)
  177.         for (int j = 0; j < Size; j++) {
  178.             if ((j % 2) == 0)
  179.                 FirstLatinSquare[i][j] = Size - i - 1;
  180.             else
  181.                 FirstLatinSquare[i][j] = i;
  182.         }
  183. }
  184.  
  185.  
  186. void fillUpperPartOfFirstSquare(TSquare& FirstLatinSquare) {
  187.     int Size = FirstLatinSquare.size();
  188.  
  189.     for (int i = 0; i < (Size / 2); i++)
  190.         for (int j = 0; j < Size; j++) {
  191.             FirstLatinSquare[i][j] = FirstLatinSquare[Size / 2 + i][Size - j - 1];
  192.         }
  193. }
  194.  
  195.  
  196. void receiveSecondLatinSquare(TSquare& FirstLatinSquare, TSquare& SecondLatinSquare) {
  197.     int Size = FirstLatinSquare.size();
  198.  
  199.     for (int i = (Size - 1); i > -1; i--)
  200.         for (int j = 0; j < Size; j++)
  201.             SecondLatinSquare[j][Size - i - 1] = FirstLatinSquare[i][j];
  202. }
  203.  
  204.  
  205. TSquare receiveResultSquare(TSquare& FirstLatinSquare, TSquare& SecondLatinSquare, TSquare& MagicSquare) {
  206.     int Size = FirstLatinSquare.size();
  207.  
  208.     for (int i = 0; i < Size; i++)
  209.         for (int j = 0; j < Size; j++)
  210.             MagicSquare[i][j] = Size * FirstLatinSquare[i][j] + SecondLatinSquare[i][j] + 1;
  211.  
  212.     return MagicSquare;
  213. }
  214.  
  215.  
  216. void receiveMagicSquare(TSquare& MagicSquare, int* Sum) {
  217.     cout << "Введите порядок магического квадрата: ";
  218.     int Size = inputDoubleEvenNumber(4, 40);
  219.  
  220.     TSquare FirstLatinSquare;
  221.     buildSquare(FirstLatinSquare, Size);
  222.  
  223.     cout << "\n\nЗаполненная нижняя половина первого латинского квадрата:\n\n";
  224.     fillLowerPartOfFirstSquare(FirstLatinSquare);
  225.     printSquare(FirstLatinSquare);
  226.  
  227.     cout << "\n\nПервый латинский квадрат:\n\n";
  228.     fillUpperPartOfFirstSquare(FirstLatinSquare);
  229.     printSquare(FirstLatinSquare);
  230.  
  231.     cout << "\n\nВторой латинский квадрат:\n\n";
  232.     TSquare SecondLatinSquare;
  233.     buildSquare(SecondLatinSquare, Size);
  234.     receiveSecondLatinSquare(FirstLatinSquare, SecondLatinSquare);
  235.     printSquare(SecondLatinSquare);
  236.  
  237.     buildSquare(MagicSquare, Size);
  238.     receiveResultSquare(FirstLatinSquare, SecondLatinSquare, MagicSquare);
  239.     cout << "\n\nМагический квадрат:\n\n";
  240.     printSquare(MagicSquare);
  241.     *Sum = findSum(MagicSquare);
  242.     cout << "\nСумма элементов на каждой линии: " << *Sum << '\n';
  243. }
  244.  
  245.  
  246. int main() {
  247.     system("chcp 1251");
  248.     cout << "Данная программа строит магический квадрат латинским способом.\n";
  249.     bool IsEnd = false;
  250.     int CurrentInstruction;
  251.     TSquare MagicSquare;
  252.     int Sum = 0;
  253.     MagicSquare.resize(1);
  254.  
  255.     cout << "Выберите команду:\n1 - построить магический квадрат;\n2 - сохранить его в файл;\n3 - завершить программу.\n";
  256.     do {
  257.         cout << "Введите команду:\n";
  258.         CurrentInstruction = inputIntegerNumber(1, 3);
  259.         switch (CurrentInstruction) {
  260.         case 1: {
  261.             receiveMagicSquare(MagicSquare, &Sum);
  262.             break;
  263.         }
  264.         case 2: {
  265.             saveToFile(MagicSquare);
  266.             break;
  267.         }
  268.         case 3: {
  269.             IsEnd = true;
  270.             break;
  271.         }
  272.         default: {
  273.             cout << "Неизвестная команда.\n";
  274.         }
  275.         }
  276.     } while (!IsEnd);
  277.  
  278.     cout << "Программа завершена";
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement