Advertisement
Kentoo

M.B#2

Feb 8th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 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.  
  8. #define outputWidth 8
  9.  
  10. using namespace std;
  11.  
  12. void menuOutput() {
  13.     cout << "1. Создать файл с исходными данными" << endl
  14.         << "2. Загрузить исходные данные" << endl
  15.         << "3. Вывести исходные данные" << endl
  16.         << "4. Обработать данные" << endl
  17.         << "5. Завершить работу с программой" << endl;
  18. }
  19.  
  20. double* increaseArray(double* oldArray, unsigned int size, double newElement) {
  21.     double* newArray = new double[size];
  22.  
  23.     if (newArray == NULL)
  24.         return NULL;
  25.  
  26.     for (unsigned int i = 0; i < size - 1; i++)
  27.         newArray[i] = oldArray[i];
  28.     newArray[size - 1] = newElement;
  29.  
  30.     delete[] oldArray;
  31.  
  32.     return newArray;
  33. }
  34.  
  35. double* createFile(unsigned int &n) {
  36.     system("cls");
  37.  
  38.     while (true) {
  39.         cout << "Введите количество элементов массива" << endl;
  40.         cin >> n;
  41.         if (!cin.fail())
  42.             break;
  43.         cout << "Неверное значение" << endl << endl;
  44.     }
  45.  
  46.     double* arr = new double[n];
  47.     double a;
  48.  
  49.     if (arr == NULL)
  50.         return NULL;
  51.     bool failed;
  52.     for (unsigned int i = 0; i < n; i++) {
  53.         cout << "Введите значение " << i + 1 << " элемента массива: ";
  54.         cin >> a;
  55.         while (cin.fail()) {
  56.             cout << "Введите корректное значение " << i + 1 << " элемента массива: ";
  57.             cin >> a;
  58.         }
  59.         arr[i] = a;
  60.     }
  61.  
  62.     char c;
  63.     string FNAME;
  64.     ofstream fout;
  65.  
  66.     while (!fout.is_open()) {
  67.         cout << "Введите имя файла : ";
  68.         cin >> FNAME;
  69.         fout.open(FNAME, ios::_Nocreate | ios::binary);
  70.         if (fout.is_open()) {
  71.             cout << "Обнаружен файл с таким именем, разрешить перезаписать? (y - да, n - нет)" << endl;
  72.             cin >> c;
  73.             switch (c) {
  74.             case 'y':
  75.                 fout.close();
  76.                 fout.open(FNAME, ios::trunc | ios::binary);
  77.                 break;
  78.             case 'n':
  79.                 fout.close();
  80.                 fout.open(FNAME, ios::app | ios::binary);
  81.                 break;
  82.             default:
  83.                 break;
  84.             }
  85.         }
  86.         else {
  87.             fout.close();
  88.             fout.open(FNAME, ios::binary);
  89.         }
  90.     }
  91.     for (unsigned int i = 0; i < n; i++)
  92.         fout.write((char*)&arr[i], sizeof(arr[i]));
  93.     fout.close();
  94.     return arr;
  95. }
  96.  
  97. double* loadFile(unsigned int &n) {
  98.     n = 0;
  99.     system("cls");
  100.     string FNAME;
  101.     ifstream fin;
  102.  
  103.     double a;
  104.     double* arr = new double[0];
  105.  
  106.     if (arr == NULL)
  107.         return NULL;
  108.  
  109.     while (!fin.is_open()) {
  110.         cout << "Введите имя файла: ";
  111.         cin >> FNAME;
  112.         fin.open(FNAME, ios::binary);
  113.     }
  114.  
  115.     while (fin.read((char*)&a, sizeof(a))) {
  116.         if (!fin.fail()) {
  117.             n++;
  118.             arr = increaseArray(arr, n, a);
  119.             if (arr == NULL)
  120.                 return NULL;
  121.         }
  122.         else
  123.             return NULL;
  124.     }
  125.     fin.close();
  126.     return arr;
  127. }
  128.  
  129. void printArray(double* arr, unsigned int size) {
  130.     cout << setw(10) << "Номера: ";
  131.     for (unsigned int i = 0; i < size; i++) {
  132.         cout << setw(outputWidth) << i + 1 << " ";
  133.     }
  134.     cout << endl;
  135.  
  136.     cout << setw(10) << "Элементы: ";
  137.     for (unsigned int i = 0; i < size; i++) {
  138.         cout << setw(outputWidth) << arr[i] << " ";
  139.     }
  140.     cout << endl;
  141. }
  142.  
  143. void createAndPrintArray(double* y, unsigned int size) {
  144.     double* f = new double[size];
  145.     for (unsigned int i = 0; i < size; i++) {
  146.         f[i] = ((1 - y[i]) / (y[i] * y[i])) + ((y[i] <= 2) ? (2) : (3)) * y[i];
  147.     }
  148.     cout << endl << "Массив y" << endl;
  149.     printArray(y, size);
  150.     cout << endl << "Массив f" << endl;
  151.     printArray(f, size);
  152.     cout << endl;
  153. }
  154.  
  155. int main() {
  156.     SetConsoleCP(1251);
  157.     SetConsoleOutputCP(1251);
  158.  
  159.     double* arr;
  160.     unsigned int n = 0;
  161.     char c;
  162.  
  163.     while (true) {
  164.         system("cls");
  165.         menuOutput();
  166.         cin >> c;
  167.         switch (c) {
  168.         case '1':
  169.             arr = createFile(n);
  170.             cout << "Готово" << endl;
  171.             system("pause");
  172.             break;
  173.         case '2':
  174.             arr = loadFile(n);
  175.             cout << "Готово" << endl;
  176.             system("pause");
  177.             break;
  178.         case '3':
  179.             printArray(arr, n);
  180.             system("pause");
  181.             break;
  182.         case '4':
  183.             createAndPrintArray(arr, n);
  184.             system("pause");
  185.             break;
  186.         case '5':
  187.             return 0;
  188.             break;
  189.         default:
  190.             break;
  191.         }
  192.         if (arr == NULL) {
  193.             cout << "Повреждены значения в файле или проблемы с памятью" << endl;
  194.             break;
  195.         }
  196.     }
  197.     system("pause");
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement