Advertisement
Kentoo

M#1

Jun 22nd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <iomanip>
  6. #include <sstream>
  7. #include <Windows.h>
  8.  
  9. #define outputWidth 8
  10. using namespace std;
  11.  
  12.  
  13. void menuOutput() {
  14.     cout << "1. Создать файл с исходными данными" << endl
  15.         << "2. Загрузить исходные данные" << endl
  16.         << "3. Вывести исходные данные" << endl
  17.         << "4. Обработать данные" << endl
  18.         << "5. Завершить работу с программой" << endl;
  19. }
  20.  
  21. double* createArray(unsigned int size) {
  22.     double* newArray = new(std::nothrow) double[size];
  23.  
  24.     if (newArray == nullptr)
  25.         return nullptr;
  26.  
  27.     return newArray;
  28. }
  29.  
  30. double* createFile(unsigned int &n) {
  31.     system("cls");
  32.  
  33.     string s;
  34.     int q;
  35.     bool failed = false;
  36.     while (true) {
  37.         cin.clear();
  38.         cin.ignore(255, '\n');
  39.         cout << "Введите количество элементов массива" << endl;
  40.         cin >> s;
  41.         try {
  42.             n = stoi(s);
  43.             if (to_string(n).length() == s.length())
  44.                 break;
  45.             cout << "Неверное значение" << endl << endl;
  46.         }
  47.         catch (exception e) {
  48.             cout << "Неверное значение" << endl << endl;
  49.         }
  50.     }
  51.  
  52.     double* arr = new(std::nothrow) double[n];
  53.  
  54.     if (arr == nullptr)
  55.         return nullptr;
  56.  
  57.     cout << endl;
  58.     int i = 0;
  59.     while (i < n) {
  60.         cin.clear();
  61.         cin.ignore(255, '\n');
  62.         cout << "Введите значение " << i + 1 << " элемента массива: ";
  63.         while (true) {
  64.             try {
  65.                 cin >> s;
  66.                 q = stoi(s);
  67.                 if (to_string(q).length() == s.length()) {
  68.                     arr[i] = q;
  69.                     break;
  70.                 }
  71.                 cout << "Введите правильное значение " << i + 1 << " элемента массива: ";
  72.             }
  73.             catch (exception e) {
  74.                 cout << "Введите правильное значение " << i + 1 << " элемента массива: ";
  75.             }
  76.         }
  77.         i++;
  78.     }
  79.  
  80.     char c;
  81.     string FNAME;
  82.     ofstream fout;
  83.  
  84.     while (!fout.is_open()) {
  85.         cout << endl << "Введите имя файла : ";
  86.         cin >> FNAME;
  87.         fout.open(FNAME, ios::_Nocreate | ios::binary);
  88.         if (fout.is_open()) {
  89.             cout << "Обнаружен файл с таким именем, разрешить перезаписать? (y - да, n - нет)" << endl;
  90.             cin >> c;
  91.  
  92.             while (c != 'y' && c != 'Y' && c != 'у' && c != 'У' && c != 'n' && c != 'N') {
  93.                 cout << "Введен неправильный ответ, попробуйте еще раз" << endl;
  94.                 cin >> c;
  95.             }
  96.  
  97.             switch (c) {
  98.             case 'y':
  99.             case 'Y':
  100.             case 'у':
  101.             case 'У':
  102.                 fout.close();
  103.                 fout.open(FNAME, ios::trunc | ios::binary);
  104.                 break;
  105.             case 'n':
  106.             case 'N':
  107.                 fout.close();
  108.                 break;
  109.             default:
  110.                 break;
  111.             }
  112.         }
  113.         else {
  114.             fout.close();
  115.             fout.open(FNAME, ios::binary);
  116.         }
  117.     }
  118.     fout.write((char*)arr, n * sizeof(double));
  119.     fout.close();
  120.     return arr;
  121. }
  122.  
  123. double* loadFile(unsigned int &n) {
  124.     system("cls");
  125.     cout.clear();
  126.     cin.ignore(255, '\n');
  127.     n = 0;
  128.  
  129.     string FNAME;
  130.     ifstream fin;
  131.     int t;
  132.     double a;
  133.     bool failed = false;
  134.     while (!fin.is_open()) {
  135.         if (failed)
  136.             cout << "Неудачная попытка открытия" << endl;
  137.         cout << "Введите имя файла: ";
  138.         getline(cin, FNAME);
  139.         fin.open(FNAME, ios::binary);
  140.         failed = true;
  141.     }
  142.     fin.seekg(0, ios::end);
  143.     t = fin.tellg();
  144.     n = t / sizeof(double);
  145.     fin.seekg(0, ios::beg);
  146.     double* arr = createArray(n);
  147.  
  148.     if (arr == nullptr) {
  149.         fin.close();
  150.         return nullptr;
  151.     }
  152.     fin.clear();
  153.     fin.read((char*)arr, n * sizeof(arr[0]));
  154.     fin.close();
  155.     return arr;
  156. }
  157.  
  158. void printArray(double* arr, unsigned int size) {
  159.     cout << setw(10) << "Номера: ";
  160.     for (unsigned int i = 0; i < size; i++) {
  161.         cout << setw(outputWidth) << i << " ";
  162.     }
  163.     cout << endl;
  164.  
  165.     cout << setw(10) << "Элементы: ";
  166.     for (unsigned int i = 0; i < size; i++) {
  167.         cout << setw(outputWidth) << arr[i] << " ";
  168.     }
  169.     cout << endl;
  170. }
  171. void calculate(double* y, unsigned int size) {
  172.     double max = y[0];
  173.     double min = y[0];
  174.     double t;
  175.     for (int i = 0; i < size; i++) {
  176.         if (y[i] > max)
  177.             max = y[i];
  178.         if (y[i] < min)
  179.             min = y[i];
  180.     }
  181.     t = (max + min) / 2;
  182.     for (int i = 0; i < size; i++) {
  183.         if (y[i] < 0)
  184.             y[i] = t;
  185.     }
  186.     printArray(y, size);
  187. }
  188.  
  189. int main() {
  190.     SetConsoleCP(1251);
  191.     SetConsoleOutputCP(1251);
  192.  
  193.     double *arr;
  194.     unsigned int n = 0;
  195.     char c;
  196.  
  197.     while (true) {
  198.         system("cls");
  199.         menuOutput();
  200.         cin >> c;
  201.         switch (c) {
  202.         case '1':
  203.             arr = createFile(n);
  204.             break;
  205.         case '2':
  206.             arr = loadFile(n);
  207.             break;
  208.         case '3':
  209.             if (n > 0) {
  210.                 system("cls");
  211.                 cout << "Исходный массив" << endl;
  212.                 printArray(arr, n);
  213.             }
  214.             else {
  215.                 system("cls");
  216.                 cout << "Не введены данные" << endl;
  217.             }
  218.             break;
  219.         case '4':
  220.             system("cls");
  221.             if (n > 0) {
  222.                 cout << endl << "Исходный массив" << endl;
  223.                 printArray(arr, n);
  224.                 cout << endl << "Обработанный массив" << endl;
  225.                 calculate(arr, n);
  226.             }
  227.             else
  228.                 cout << "Не введены данные" << endl;
  229.             break;
  230.         case '5':
  231.             return 0;
  232.             break;
  233.         default:
  234.             system("cls");
  235.             cout << "Неверный ввод" << endl;
  236.             break;
  237.         }
  238.         if ((arr == nullptr && n > 0)) {
  239.             cout << "Повреждены значения в файле или проблемы с памятью" << endl;
  240.             n = 0;
  241.         }
  242.         if (c == '2') {
  243.             cout << "Загруженный массив" << endl;
  244.             printArray(arr, n);
  245.             cout << endl;
  246.         }
  247.         system("pause");
  248.     }
  249.     delete[] arr;
  250.     system("pause");
  251.     return 0;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement