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* increaseArray(double* oldArray, unsigned int size, double newElement) {
- double* newArray = new double[size];
- if (newArray == NULL)
- return NULL;
- for (unsigned int i = 0; i < size - 1; i++)
- newArray[i] = oldArray[i];
- newArray[size - 1] = newElement;
- delete[] oldArray;
- return newArray;
- }
- double* createFile(unsigned int &n) {
- system("cls");
- while (true) {
- cout << "Введите количество элементов массива" << endl;
- cin >> n;
- if (!cin.fail())
- break;
- cout << "Неверное значение" << endl << endl;
- }
- double* arr = new double[n];
- double a;
- if (arr == NULL)
- return NULL;
- bool failed;
- for (unsigned int i = 0; i < n; i++) {
- cout << "Введите значение " << i + 1 << " элемента массива: ";
- cin >> a;
- while (cin.fail()) {
- cout << "Введите корректное значение " << i + 1 << " элемента массива: ";
- cin >> a;
- }
- arr[i] = a;
- }
- char c;
- string FNAME;
- ofstream fout;
- while (!fout.is_open()) {
- cout << "Введите имя файла : ";
- cin >> FNAME;
- fout.open(FNAME, ios::_Nocreate | ios::binary);
- if (fout.is_open()) {
- cout << "Обнаружен файл с таким именем, разрешить перезаписать? (y - да, n - нет)" << endl;
- cin >> c;
- switch (c) {
- case 'y':
- fout.close();
- fout.open(FNAME, ios::trunc | ios::binary);
- break;
- case 'n':
- fout.close();
- fout.open(FNAME, ios::app | ios::binary);
- 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) {
- n = 0;
- system("cls");
- string FNAME;
- ifstream fin;
- double a;
- double* arr = new double[0];
- if (arr == NULL)
- return NULL;
- while (!fin.is_open()) {
- cout << "Введите имя файла: ";
- cin >> FNAME;
- fin.open(FNAME, ios::binary);
- }
- while (fin.read((char*)&a, sizeof(a))) {
- if (!fin.fail()) {
- n++;
- arr = increaseArray(arr, n, a);
- if (arr == NULL)
- return NULL;
- }
- else
- return NULL;
- }
- fin.close();
- return arr;
- }
- void printArray(double* arr, unsigned int size) {
- cout << setw(10) << "Номера: ";
- for (unsigned int i = 0; i < size; i++) {
- cout << setw(outputWidth) << i + 1 << " ";
- }
- 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 double[size];
- 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);
- cout << endl;
- }
- 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);
- cout << "Готово" << endl;
- system("pause");
- break;
- case '2':
- arr = loadFile(n);
- cout << "Готово" << endl;
- system("pause");
- break;
- case '3':
- printArray(arr, n);
- system("pause");
- break;
- case '4':
- createAndPrintArray(arr, n);
- system("pause");
- break;
- case '5':
- return 0;
- break;
- default:
- break;
- }
- if (arr == NULL) {
- cout << "Повреждены значения в файле или проблемы с памятью" << endl;
- break;
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement