Advertisement
Kentoo

Y#1

May 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.88 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include "windows.h"
  4. #include <string>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <sstream>
  8.  
  9. #define outputWidth 10
  10.  
  11. using namespace std;
  12.  
  13. void menuOutput() {
  14.     cout << "1. Создать файл с исходными данными" << endl
  15.         << "2. Загрузить исходные данные" << endl
  16.         << "3. Вывести исходные данные" << endl
  17.         << "4. Обработать данные" << endl
  18.         << "5. Завершить работу с программой" << endl;
  19. }
  20.  
  21. double** createMatrix(unsigned int rows, unsigned int columns) {
  22.     double** newMatrix = new(std::nothrow) double*[rows];
  23.  
  24.     if (newMatrix == nullptr)
  25.         return nullptr;
  26.  
  27.     for (unsigned int i = 0; i < rows; i++) {
  28.         newMatrix[i] = new double[columns];
  29.         if (newMatrix[i] == nullptr || newMatrix == nullptr)
  30.             return nullptr;
  31.     }
  32.  
  33.     return newMatrix;
  34. }
  35.  
  36. double** createFile(unsigned int &n, unsigned int &m) {
  37.     system("cls");
  38.  
  39.     string s;
  40.     double a;
  41.  
  42.     while (true) {
  43.         cin.clear();
  44.         cin.ignore(255, '\n');
  45.         cout << "Введите количество строк матрицы" << endl;
  46.         cin >> s;
  47.         try {
  48.             n = stoi(s);
  49.             a = stod(s);
  50.             if (a == n)
  51.                 break;
  52.             cout << "Неверное значение" << endl << endl;
  53.         }
  54.         catch (exception e) {
  55.             cout << "Неверное значение" << endl << endl;
  56.         }
  57.     }
  58.     while (true) {
  59.         cin.clear();
  60.         cin.ignore(255, '\n');
  61.         cout << "Введите количество столбцов матрицы" << endl;
  62.         cin >> s;
  63.         try {
  64.             m = stoi(s);
  65.             a = stod(s);
  66.             if (a == m)
  67.                 break;
  68.             cout << "Неверное значение" << endl << endl;
  69.         }
  70.         catch (exception e) {
  71.             cout << "Неверное значение" << endl << endl;
  72.         }
  73.     }
  74.     a = 0;
  75.  
  76.     double** matr = createMatrix(n, m);
  77.  
  78.     if (matr == nullptr)
  79.         return nullptr;
  80.  
  81.     cout << endl;
  82.     bool failed = false;
  83.     for (unsigned int i = 0; i < n; i++) {
  84.         for (unsigned int j = 0; j < m; j++) {
  85.             cout << "Введите значение " << j << " элемента " << i << " строки матрицы: ";
  86.             cin >> s;
  87.             try {
  88.                 if (s.at(0) == '.')
  89.                     s = '0' + s;
  90.                 a = stod(s);
  91.             }
  92.             catch (exception e) {
  93.                 failed = true;
  94.             }
  95.             while (failed) {
  96.                 cout << "Введите корректное значение " << j << " элемента " << i << " строки матрицы: ";
  97.                 cin >> s;
  98.                 try {
  99.                     if (s.at(0) == '.')
  100.                         s = '0' + s;
  101.                     a = stod(s);
  102.                     failed = false;
  103.                 }
  104.                 catch (exception e) {
  105.                     failed = true;
  106.                 }
  107.             }
  108.             matr[i][j] = a;
  109.         }
  110.         cout << endl;
  111.     }
  112.  
  113.     char c;
  114.     string FNAME;
  115.     ofstream fout;
  116.  
  117.     while (!fout.is_open()) {
  118.         cout << endl << "Введите имя файла : ";
  119.         cin >> FNAME;
  120.         fout.open(FNAME, ios::_Nocreate);
  121.         if (fout.is_open()) {
  122.             cout << "Обнаружен файл с таким именем, разрешить перезаписать? (y - да, n - нет)" << endl;
  123.             cin >> c;
  124.  
  125.             while (c != 'y' && c != 'Y' && c != 'у' && c != 'У' && c != 'n' && c != 'N') {
  126.                 cout << "Введен неправильный ответ, попробуйте еще раз" << endl;
  127.                 cin >> c;
  128.             }
  129.  
  130.             switch (c) {
  131.             case 'y':
  132.             case 'Y':
  133.             case 'у':
  134.             case 'У':
  135.                 fout.close();
  136.                 fout.open(FNAME, ios::trunc);
  137.                 break;
  138.             case 'n':
  139.             case 'N':
  140.                 fout.close();
  141.                 break;
  142.             default:
  143.                 break;
  144.             }
  145.         }
  146.         else {
  147.             fout.close();
  148.             fout.open(FNAME);
  149.         }
  150.     }
  151.     for (unsigned int i = 0; i < n; i++) {
  152.         for (unsigned int j = 0; j < m; j++)
  153.             fout << matr[i][j] << ' ';
  154.         fout << endl;
  155.     }
  156.     fout.close();
  157.     return matr;
  158. }
  159.  
  160. double** loadFile(unsigned int &n, unsigned int &m) {
  161.     system("cls");
  162.     unsigned int temp = m;
  163.     n = 0;
  164.     m = 0;
  165.  
  166.     string FNAME, s;
  167.     stringstream buf, res;
  168.     ifstream fin;
  169.  
  170.     double a;
  171.  
  172.     while (!fin.is_open()) {
  173.         cout << "Введите имя файла: ";
  174.         cin >> FNAME;
  175.         fin.open(FNAME);
  176.     }
  177.     bool broken = false;
  178.     n = 0;
  179.     while (!fin.eof() && !broken) {
  180.         getline(fin, s);
  181.  
  182.         while (s.find("  ") != s.npos)
  183.             s.replace(s.find("  "), 2, " ");
  184.  
  185.         while (s.find(",") != s.npos)
  186.             s.replace(s.find(","), 1, ".");
  187.         if (s.size() > 0) {
  188.             buf.str(string());
  189.             buf.clear();
  190.  
  191.             buf << s;
  192.             m = 0;
  193.             while (buf >> s) {
  194.                 try {
  195.                     a = stod(s);
  196.                     m++;
  197.                 }
  198.                 catch (exception e) {
  199.                     if (strcmp(e.what(), "invalid stod argument") == 0)
  200.                         cout << "Обнаружен недопустимый символ при вводе значения из " << n + 1 << " строки и " << ((m == 0) ? (m + 1) : (m)) << " столбца" << endl
  201.                         << "Недопустимый ввод -> " << s << endl;
  202.                     if (strcmp(e.what(), "stod argument out of range") == 0)
  203.                         cout << "Обнаружено переполнение типа при вводе значения из " << n + 1 << " строки и " << ((m == 0) ? (m + 1) : (m)) << " столбца" << endl;
  204.                     broken = true;
  205.                     break;
  206.                 }
  207.                 buf.ignore();
  208.                 if (!isdigit((unsigned char)s.at(s.size() - 1))) {
  209.                     cout << "Обнаружен недопустимый символ при вводе значения из " << n + 1 << " строки и " << ((m == 0) ? (m + 1) : (m)) << " столбца" << endl
  210.                         << "Недопустимый ввод -> " << s << endl;
  211.                     broken = true;
  212.                     break;
  213.                 }
  214.                 res << a << ' ';
  215.             }
  216.             if (temp == 0)
  217.                 temp = m;
  218.             if (m != temp) {
  219.                 cout << "Неверное количество значений в " << n << " строке" << endl;
  220.                 broken = true;
  221.             }
  222.             n++;
  223.         }
  224.     }
  225.     fin.close();
  226.     if (!broken) {
  227.         double** matr = createMatrix(n, m);
  228.  
  229.         if (matr == nullptr)
  230.             return nullptr;
  231.  
  232.         unsigned int i = 0;
  233.         for (unsigned int i = 0; i < n; i++)
  234.             for (unsigned int j = 0; j < m; j++) {
  235.                 res >> a;
  236.                 if (!res.fail()) {
  237.                     matr[i][j] = a;
  238.                 }
  239.                 else {
  240.                     for (unsigned int i = 0; i < n; i++)
  241.                         delete[] matr[i];
  242.                     delete[] matr;
  243.                     return nullptr;
  244.                 }
  245.             }
  246.         return matr;
  247.     }
  248.     return nullptr;
  249. }
  250.  
  251. void printMatrix(double** matr, unsigned int rows, unsigned int columns) {
  252.     cout.precision(3);
  253.     for (unsigned int i = 0; i < rows; i++) {
  254.         for (unsigned int j = 0; j < columns; j++)
  255.             cout << setw(outputWidth) << matr[i][j] << " ";
  256.         cout << endl;
  257.     }
  258.     cout << endl;
  259. }
  260.  
  261. boolean createAndPrintMatrix(double** f, unsigned int rows, unsigned int columns) {
  262.     int t = 0;
  263.     for (int j = 0; j < columns; j++)
  264.         if (f[0][j] > f[1][j])
  265.             t++;
  266.     if (t == 0)
  267.         cout << "Подходящих столбцов не обнаружено" << endl;
  268.     else {
  269.         short *arr = new(std::nothrow) short[t];
  270.         if (arr == nullptr)
  271.             return true;
  272.         else {
  273.             t = 0;
  274.             for (int i = 0; i < columns; i++)
  275.                 if (f[0][i] > f[1][i]) {
  276.                     arr[t] = i;
  277.                     t++;
  278.                 }
  279.             double srar;
  280.             for (int j = 0; j < columns; j++) {
  281.                 srar = 0;
  282.                 for (int i = 0; i < rows; i++)
  283.                     srar += f[i][j];
  284.                 srar /= rows;
  285.                 for (int i = 0; i < rows; i++)
  286.                     f[i][j] = srar;
  287.             }
  288.             cout << "Составленные массив" << endl;
  289.             for (int i = 0; i < t; i++)
  290.                 cout << arr[i] << ' ';
  291.             cout << endl << "Обработанная матрица" << endl;
  292.             printMatrix(f, rows, columns);
  293.             cout << endl;
  294.         }
  295.     }
  296.     return false;
  297. }
  298.  
  299. int main() {
  300.     SetConsoleCP(1251);
  301.     SetConsoleOutputCP(1251);
  302.  
  303.     double** matr = nullptr;
  304.     unsigned int n = 0, m = 0;
  305.     char c;
  306.  
  307.     while (true) {
  308.         system("cls");
  309.         menuOutput();
  310.         cin >> c;
  311.         switch (c) {
  312.         case '1':
  313.             matr = createFile(n, m);
  314.             break;
  315.         case '2':
  316.             matr = loadFile(n, m);
  317.             if (n == 0 && m == 0 && matr != nullptr) {
  318.                 cout << "Введена пустая матрица" << endl;
  319.                 matr = nullptr;
  320.             }
  321.             break;
  322.         case '3':
  323.             if (n > 0 && m > 0) {
  324.                 system("cls");
  325.                 cout << "Исходная матрица" << endl;
  326.                 printMatrix(matr, n, m);
  327.             }
  328.             break;
  329.         case '4':
  330.             if (matr != nullptr) {
  331.                 system("cls");
  332.                 if (createAndPrintMatrix(matr, n, m))
  333.                     matr = nullptr;
  334.             }
  335.             break;
  336.         case '5':
  337.             return 0;
  338.             break;
  339.         default:
  340.             break;
  341.         }
  342.  
  343.         if (matr == nullptr && n > 0) {
  344.             cout << "Повреждены значения в файле или проблемы с памятью" << endl;
  345.             break;
  346.         }
  347.  
  348.         if (c == '1' || c == '2' || c == '3' || c == '4' || c == '5')
  349.             cout << endl << "Готово" << endl << endl;
  350.         else
  351.             cout << endl << "Введен неправильный ответ, попробуйте еще раз" << endl << endl;
  352.  
  353.         if (c == '2' && matr != nullptr) {
  354.             cout << "Загруженная матрица" << endl;
  355.             printMatrix(matr, n, m);
  356.             cout << endl;
  357.         }
  358.  
  359.         system("pause");
  360.     }
  361.     if (matr != nullptr) {
  362.         for (unsigned int i = 0; i < n; i++) {
  363.             try {
  364.                 delete[] matr[i];
  365.             }
  366.             catch (exception e) {
  367.             }
  368.         }
  369.         delete[] matr;
  370.     }
  371.     system("pause");
  372.     return 0;
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement