Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include "windows.h"
- #include <string>
- #include <fstream>
- #include <iomanip>
- #define outputWidth 8
- using namespace std;
- void menuOutput() {
- cout << "1. Создать файл с исходными данными" << endl
- << "2. Загрузить исходные данные" << endl
- << "3. Вывести исходные данные" << endl
- << "4. Обработать данные" << endl
- << "5. Завершить работу с программой" << endl;
- }
- double* createArray(unsigned int size) {
- double* newArray = new(std::nothrow) double[size];
- if (newArray == nullptr)
- return nullptr;
- return newArray;
- }
- double* createFile(unsigned int &n) {
- system("cls");
- string s;
- double a;
- while (true) {
- cin.clear();
- cin.ignore(255, '\n');
- cout << "Введите количество элементов массива" << endl;
- cin >> s;
- try {
- n = stoi(s);
- a = stod(s);
- if (a == n)
- break;
- cout << "Неверное значение" << endl << endl;
- }
- catch (exception e) {
- cout << "Неверное значение" << endl << endl;
- }
- }
- double* arr = new(std::nothrow) double[n];
- a = 0;
- if (arr == nullptr)
- return nullptr;
- cout << endl;
- bool failed;
- for (unsigned int i = 0; i < n; i++) {
- cout << "Введите значение " << i + 1 << " элемента массива: ";
- cin >> s;
- try {
- if (s.at(0) == '.')
- s = '0' + s;
- a = stod(s);
- }
- catch (exception e) {
- failed = true;
- }
- while (failed) {
- cout << "Введите корректное значение " << i + 1 << " элемента массива: ";
- cin >> s;
- try {
- if (s.at(0) == '.')
- s = '0' + s;
- a = stod(s);
- failed = false;
- }
- catch (exception e) {
- failed = true;
- }
- }
- arr[i] = a;
- }
- char c;
- string FNAME;
- ofstream fout;
- while (!fout.is_open()) {
- cout << endl << "Введите имя файла : ";
- cin >> FNAME;
- fout.open(FNAME, ios::_Nocreate | ios::binary);
- if (fout.is_open()) {
- cout << "Обнаружен файл с таким именем, разрешить перезаписать? (y - да, n - нет)" << endl;
- cin >> c;
- while (c != 'y' && c != 'Y' && c != 'у' && c != 'У' && c != 'n' && c != 'N') {
- cout << "Введен неправильный ответ, попробуйте еще раз" << endl;
- cin >> c;
- }
- switch (c) {
- case 'y':
- case 'Y':
- case 'у':
- case 'У':
- fout.close();
- fout.open(FNAME, ios::trunc | ios::binary);
- break;
- case 'n':
- case 'N':
- fout.close();
- break;
- default:
- break;
- }
- }
- else {
- fout.close();
- fout.open(FNAME, ios::binary);
- }
- }
- for (unsigned int i = 0; i < n; i++)
- fout.write((char*)&arr[i], sizeof(arr[i]));
- fout.close();
- return arr;
- }
- double* loadFile(unsigned int &n) {
- system("cls");
- n = 0;
- string FNAME;
- ifstream fin;
- double a;
- while (!fin.is_open()) {
- cout << "Введите имя файла: ";
- cin >> FNAME;
- fin.open(FNAME, ios::binary);
- }
- while (fin.read((char*)&a, sizeof(a)))
- n++;
- fin.close();
- double* arr = createArray(n);
- if (arr == nullptr)
- return nullptr;
- fin.open(FNAME, ios::binary);
- unsigned int i = 0;
- while (fin.read((char*)&a, sizeof(a))) {
- if (!fin.fail()) {
- arr[i] = a;
- i++;
- }
- else {
- fin.close();
- delete[] arr;
- return nullptr;
- }
- }
- return arr;
- }
- void printArray(double* arr, unsigned int size) {
- cout << setw(10) << "Номера: ";
- for (unsigned int i = 0; i < size; i++) {
- cout << setw(outputWidth) << i << " ";
- }
- cout << endl;
- cout << setw(10) << "Элементы: ";
- for (unsigned int i = 0; i < size; i++) {
- cout << setw(outputWidth) << arr[i] << " ";
- }
- cout << endl;
- }
- void createAndPrintArray(double* y, unsigned int size) {
- double* f = new(std::nothrow) double[size];
- if (f == nullptr)
- cout << "Ошибка выделения памяти" << endl;
- else {
- for (unsigned int i = 0; i < size; i++) {
- f[i] = ((1 - y[i]) / (y[i] * y[i])) + ((y[i] <= 2) ? (2) : (3)) * y[i];
- }
- cout << endl << "Массив y" << endl;
- printArray(y, size);
- cout << endl << "Массив f" << endl;
- printArray(f, size);
- }
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- double* arr;
- unsigned int n = 0;
- char c;
- while (true) {
- system("cls");
- menuOutput();
- cin >> c;
- switch (c) {
- case '1':
- arr = createFile(n);
- break;
- case '2':
- arr = loadFile(n);
- break;
- case '3':
- printArray(arr, n);
- break;
- case '4':
- createAndPrintArray(arr, n);
- break;
- case '5':
- return 0;
- break;
- default:
- break;
- }
- if (arr == nullptr && n > 0) {
- cout << "Повреждены значения в файле или проблемы с памятью" << endl;
- break;
- }
- if (c == '1' || c == '2' || c == '3' || c == '4' || c == '5')
- cout << endl << "Готово" << endl << endl;
- else
- cout << endl << "Введен неправильный ответ, попробуйте еще раз" << endl << endl;
- if (c == '2') {
- cout << "Загруженный массив" << endl;
- printArray(arr, n);
- cout << endl;
- }
- system("pause");
- }
- delete[] arr;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement