Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ООП_7.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #define _CRT_SECURE_NO_WARNINGS
- #include <fstream>
- #include <iostream>
- #include <conio.h>
- #include <iomanip>
- #include <math.h>
- #include <string.h>
- #include <clocale>
- #include <algorithm>
- using namespace std;
- class InvalidFile
- {
- char* message;
- public:
- InvalidFile(char* m)
- {
- message = new char[sizeof(m)];
- strcpy(message, m);
- }
- char* getMessage()
- {
- return message;
- }
- ~InvalidFile()
- {
- delete[] message;
- }
- };
- class MyFile
- {
- char filename[50];
- public:
- MyFile(char name[50])
- {
- strcpy(filename, name);
- }
- void openFiletoWrite(ofstream& f)
- {
- f.open(filename, std::fstream::out);
- if (!f.is_open())
- {
- char* mes;
- mes = new char[sizeof("Файл не октрыт!")];
- strcpy(mes, "Файл не октрыт!");
- throw new InvalidFile(mes);
- }
- }
- void openFiletoRead(ifstream& f)
- {
- f.open(filename, std::fstream::in);
- if (!f.is_open())
- {
- char* mes;
- mes = new char[sizeof("Файл не октрыт!")];
- strcpy(mes, "Файл не октрыт!");
- throw new InvalidFile(mes);
- }
- }
- void fileOutput(ifstream& f)
- {
- char buff[100];
- try
- {
- cout << setw(30) << "Вывод из файла" << endl;
- while (!f.eof())
- {
- f.getline(buff, 100);
- cout << buff << endl;
- }
- }
- catch (...)
- {
- char* mes;
- mes = new char[sizeof("Ошибка вывода!")];
- strcpy(mes, "Ошибка вывода!");
- throw new InvalidFile(mes);
- }
- }
- };
- class InvalidSymbol_Vector
- {
- char* message;
- public:
- InvalidSymbol_Vector()
- {
- char m[] = { "Обработка ошиок. Необходимо ввести символы в диапозоне 0-9" };
- message = new char[sizeof(m)];
- strcpy(message, m);
- }
- char* getMessage()
- {
- return message;
- }
- ~InvalidSymbol_Vector()
- {
- delete[] message;
- }
- };
- string toBinary(unsigned x) {
- string sbin;
- if (x > 0) {
- while (x != 0) {
- sbin = (x % 2 == 0 ? '0' : '1') + sbin;
- x /= 2;
- }
- }
- else {
- sbin = "0";
- }
- return sbin;
- }
- void f(char filename[50])
- {
- ofstream t1;
- MyFile* opfile = new MyFile(filename);
- opfile->openFiletoWrite(t1);
- cout << "Введите цифру с символом 0 - 9 без остальных символов: " << endl;
- char y[12];
- cin >> y;
- int step=0;
- while (y && (y[step] != NULL)) {
- cout << setfill('%') << setw(12) << dec << setprecision(4) << (int)y[step] <<""<< endl;
- if ((tolower(y[step]) < '0') || (tolower(y[step]) > '9'))
- {
- throw new InvalidSymbol_Vector();
- }
- step++;
- }
- int yInt = atoi(y);
- float x = sqrt(yInt);
- cout << setfill('%') << setw(12) << "Входной параметр y=:" << endl;
- cout << setfill('%') << setw(12) << yInt << endl;
- cout << setfill('%')<< setw(12) << "Выходной параметр x=:"<<endl;
- cout << setfill('%') << setw(12) << dec << setprecision(4) << x << endl;
- t1 << setfill('%') << setw(12) << yInt << endl;
- t1 << setfill('%') << setw(12) << dec << setprecision(4) << x << endl;
- cout << setfill(' ') << endl;
- t1.close();
- delete opfile;
- }
- int main()
- {
- setlocale(LC_CTYPE, "rus");
- char filename[50];
- while (1)
- {
- try
- {
- cout.width(50);
- cout << "введите имя файла для записи" << endl;
- cin >> filename;
- f(filename);
- cout << "введите имя файла для чтения" << endl;
- cin >> filename;
- MyFile* opfile = new MyFile(filename);
- ifstream t2;
- opfile->openFiletoRead(t2);
- opfile->fileOutput(t2);
- delete opfile;
- t2.close();
- }
- catch (InvalidSymbol_Vector* s)
- {
- cout << s->getMessage() << endl;
- }
- catch (InvalidFile* f)
- {
- cout << f->getMessage() << endl;
- }
- char n;
- cout.width(50);
- cout << "нажмите любую клавишу для продолжения . . ." << endl;
- cout.width(20);
- cout << right << "Введите '0' - выход" << endl;
- n = _getch();
- if (n == '0')
- {
- return(0);
- }
- }
- }
- // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
- // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
- // Советы по началу работы
- // 1. В окне обозревателя решений можно добавлять файлы и управлять ими.
- // 2. В окне Team Explorer можно подключиться к системе управления версиями.
- // 3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
- // 4. В окне "Список ошибок" можно просматривать ошибки.
- // 5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
- // 6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
Advertisement
Add Comment
Please, Sign In to add comment