Advertisement
MaksNew

Untitled

Dec 18th, 2022
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <filesystem>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. int getArraySize()
  9. {
  10.     int order;
  11.     bool IsNotCorrect;
  12.     string inputLine;
  13.     do
  14.     {
  15.         cout << "Введите размер массива: " << endl;
  16.         IsNotCorrect = false;
  17.         getline(cin, inputLine);
  18.         try
  19.         {
  20.             order = stoi(inputLine);
  21.         }
  22.         catch (...)
  23.         {
  24.             IsNotCorrect = true;
  25.             cout << "Порядок матрицы должен быть положительным числом." << endl;
  26.         }
  27.         if (!IsNotCorrect && (((order < 2) || (order > 10000))))
  28.         {
  29.             cout << "Порядок матрицы должен принадлежать промежутку от 1 до 10000" << endl;
  30.             IsNotCorrect = true;
  31.         }
  32.     } while (IsNotCorrect);
  33.     return order;
  34. }
  35.  
  36. int getArrayElement(int prev)
  37. {
  38.     int element;
  39.     bool IsNotCorrect;
  40.     string inputLine;
  41.     do
  42.     {
  43.         cout << "Введите элемент массива: " << endl;
  44.         IsNotCorrect = false;
  45.         getline(cin, inputLine);
  46.         try
  47.         {
  48.             element = stoi(inputLine);
  49.         }
  50.         catch (...)
  51.         {
  52.             IsNotCorrect = true;
  53.             cout << "Элемент массива должен быть числом." << endl;
  54.         }
  55.         if (!IsNotCorrect && (((element < prev) || (element > 2147000000))))
  56.         {
  57.             cout << "Элемент массива должен быть больше " << prev << " и меньше 2147000000" << endl;
  58.             IsNotCorrect = true;
  59.         }
  60.     } while (IsNotCorrect);
  61.     return element;
  62. }
  63.  
  64. int* сreateArray(int size)
  65. {
  66.     int* arr = new int[size];
  67.     arr[0] = getArrayElement(-2147000000);
  68.     for (int i = 1; i < size; i++)
  69.         arr[i] = getArrayElement(arr[i-1]);
  70.     return arr;
  71. }
  72.  
  73. void printArray(int*& arr, int size)
  74. {
  75.     cout << endl;
  76.     for (int i = 0; i < size; i++) {
  77.         cout << arr[i] << "\t";
  78.     }
  79.     cout << endl;
  80. }
  81.  
  82. void сreateArraysFromFile(int*& a, int& m, int*& b, int& n, string path)
  83. {
  84.     fstream file(path, fstream::in);
  85.     string inputLine;
  86.     stringstream strstream;
  87.  
  88.     getline(file, inputLine);
  89.     m = stoi(inputLine);
  90.     a = new int[m];
  91.  
  92.     getline(file, inputLine);
  93.     strstream << inputLine;
  94.     for (int i = 0; i < m; i++)
  95.     {
  96.         strstream >> a[i];
  97.     }
  98.  
  99.     getline(file, inputLine);
  100.     n = stoi(inputLine);
  101.     b = new int[n];
  102.  
  103.     getline(file, inputLine);
  104.     strstream << inputLine;
  105.     for (int i = 0; i < n; i++)
  106.     {
  107.         strstream >> b[i];
  108.     }
  109. }
  110.  
  111. bool isFileCorrect(string path)
  112. {
  113.     string inputline;
  114.     stringstream strstream;
  115.     int iSize, num, size;
  116.     bool isCorrect = true;
  117.     iSize = 0;
  118.     fstream file(path, std::ios_base::in);
  119.     try
  120.     {
  121.         getline(file, inputline);
  122.         size = stoi(inputline);
  123.         getline(file, inputline);
  124.         strstream << inputline;
  125.         while (!strstream.eof() && iSize < size)
  126.         {
  127.             strstream >> num;
  128.             iSize++;
  129.         }
  130.         if (iSize != size)
  131.             isCorrect = false;
  132.  
  133.         iSize = 0;
  134.         getline(file, inputline);
  135.         size = stoi(inputline);
  136.         getline(file, inputline);
  137.         strstream << inputline;
  138.         while (!strstream.eof() && iSize < size && isCorrect)
  139.         {
  140.             strstream >> num;
  141.             iSize++;
  142.         }
  143.         if (iSize != size)
  144.             isCorrect = false;
  145.     }
  146.     catch (...)
  147.     {
  148.         isCorrect = false;
  149.     }
  150.     return isCorrect;
  151. }
  152.  
  153. string getTaskFilePath()
  154. {
  155.     string path;
  156.     bool isIncorrect = true;
  157.     do
  158.     {
  159.         cout << "Введите абсолютный путь к файлу: " << endl;
  160.         cin >> path;
  161.         if (!filesystem::exists(path))
  162.         {
  163.             cout << "Файл не найден. Проверьте введённый путь." << endl;
  164.         }
  165.         else
  166.         {
  167.             if (isFileCorrect(path))
  168.             {
  169.                 isIncorrect = false;
  170.             }
  171.             else
  172.             {
  173.                 cout << "Проверьте данные в файле. Обратите внимание на размеры массивов и их содержимое." << endl;
  174.             }
  175.         }
  176.     } while (isIncorrect);
  177.     return path;
  178. }
  179.  
  180. string getOutputDirectory()
  181. {
  182.     string patho;
  183.     bool isIncorrect;
  184.     isIncorrect = true;
  185.     do
  186.     {
  187.         cout << "Введите директорию, в которую хотите сохранить результат:" << endl;
  188.         cin >> patho;
  189.         if (filesystem::exists(patho))
  190.         {
  191.             isIncorrect = false;
  192.         }
  193.         else
  194.         {
  195.             cout << "Такой директории не существует. Попробуйте ещё раз." << endl;
  196.         }
  197.  
  198.     } while (isIncorrect);
  199.     return patho;
  200. }
  201.  
  202. void printResultToFile(int*& a, int m, int*& b, int n, int*& c, int k)
  203. {
  204.     string path;
  205.     path = getOutputDirectory();
  206.     ofstream fout;
  207.     fout.open(path + "\output.txt");
  208.     fout << "Для массивов" << endl;
  209.     for (int i = 0; i < m; i++)
  210.     {
  211.         fout << setw(6) << a[i];
  212.     }
  213.     fout << endl;
  214.     for (int i = 0; i < n; i++)
  215.     {
  216.         fout << setw(6) << b[i];
  217.     }
  218.     fout << "Упорядоченный массив без повторений:" << endl;
  219.     for (int i = 0; i < k; i++)
  220.     {
  221.         fout << setw(6) << c[i];
  222.     }
  223.     fout.close();
  224.     cout << "Ответ сохранен по указанному пути.";
  225. }
  226.  
  227. int* calculateArrayByCondition(int*& a, int m, int*& b, int n, int d)
  228. {
  229.     d = 0;
  230.     int i = 0;
  231.     int j = 0;
  232.     int min = 0;
  233.     int* c = new int[m + n];
  234.  
  235.     while ((i < n) && (j < m))
  236.     {
  237.         if (a[i] < b[j])
  238.         {
  239.             c[d++] = a[i++];
  240.         }
  241.         else if (a[i] > b[j])
  242.         {
  243.             c[d++] = b[j++];
  244.         }
  245.         else
  246.         {
  247.             c[d++] = a[i++];
  248.             j++;
  249.         }
  250.     }
  251.     while (i < n)
  252.         c[d++] = a[i++];
  253.     while (j < m)
  254.         c[d++] = b[j++];
  255.  
  256.     bool isContinue = true;
  257.     for (i = 0; i < d; i++)
  258.     {
  259.         for (j = 0; j < d - i; j++)
  260.         {
  261.             if (j + 1 == d)
  262.                 isContinue = false;
  263.             if ((c[j] > c[j + 1]) && (isContinue))
  264.             {
  265.                 min = c[j];
  266.                 c[j] = c[j + 1];
  267.                 c[j + 1] = min;
  268.             }
  269.         }
  270.     }
  271.     return c;
  272. }
  273.  
  274. void workWithFile()
  275. {
  276.     int m,n,k = 0;
  277.     int* a;
  278.     int* b;
  279.     сreateArraysFromFile(a, m, b, n, getTaskFilePath());
  280.     printArray(a, m);
  281.     printArray(b, n);
  282.     int* c = calculateArrayByCondition(a, m, b, n, k);
  283.     cout << "Новый массив:" << endl;
  284.     printArray(c, k);
  285.     printResultToFile(a, m, b, n, c, k);
  286.     delete a, b, c;
  287. }
  288.  
  289. void workWithConsole()
  290. {
  291.     int m, n, k = 0;
  292.     int* a;
  293.     int* b;
  294.     m = getArraySize();
  295.     a = сreateArray(m);
  296.     n = getArraySize();
  297.     b = сreateArray(n);
  298.     printArray(a, m);
  299.     printArray(b, n);
  300.     int* c = calculateArrayByCondition(a, m, b, n, k);
  301.     cout << "Новый массив:" << endl;
  302.     printArray(c, k);
  303.     printResultToFile(a, m, b, n, c, k);
  304.     delete a, b, c;
  305. }
  306.  
  307. void setProgramWorkType()
  308. {
  309.     int answer;
  310.     bool isNotCorrect;
  311.     string inputLine;
  312.     do
  313.     {
  314.         cout << "1 - работать с консолью, 2 - с файлом: " << endl;
  315.         isNotCorrect = false;
  316.         getline(cin, inputLine);
  317.         try
  318.         {
  319.             answer = stoi(inputLine);
  320.         }
  321.         catch (...)
  322.         {
  323.             isNotCorrect = true;
  324.             cout << "Введите число!" << endl;
  325.         }
  326.         if (!isNotCorrect && (((answer < 1) || (answer > 2))))
  327.         {
  328.             cout << "Выбирете один из предложенных вариантов!" << endl;
  329.             isNotCorrect = true;
  330.         }
  331.     } while (isNotCorrect);
  332.     if (answer == 1)
  333.         workWithConsole();
  334.     else
  335.         workWithFile();
  336. }
  337.  
  338. int main()
  339. {
  340.     setlocale(LC_ALL, "ru");
  341.     setProgramWorkType();
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement