Savelyev_Vyacheslav

OOP L7

Aug 8th, 2021
1,436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 KB | None | 0 0
  1. // ООП_7.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3. #define _CRT_SECURE_NO_WARNINGS
  4.  
  5. #include <fstream>
  6. #include <iostream>
  7. #include <conio.h>
  8. #include <iomanip>
  9. #include <math.h>
  10. #include <string.h>
  11. #include <clocale>
  12. #include <algorithm>
  13. using namespace std;
  14.  
  15. class InvalidFile
  16. {
  17.     char* message;
  18. public:
  19.     InvalidFile(char* m)
  20.     {
  21.         message = new char[sizeof(m)];
  22.         strcpy(message, m);
  23.     }
  24.     char* getMessage()
  25.     {
  26.         return message;
  27.     }
  28.     ~InvalidFile()
  29.     {
  30.         delete[] message;
  31.     }
  32. };
  33.  
  34.  
  35. class MyFile
  36. {
  37.     char filename[50];
  38. public:
  39.     MyFile(char name[50])
  40.     {
  41.         strcpy(filename, name);
  42.     }
  43.     void openFiletoWrite(ofstream& f)
  44.     {
  45.         f.open(filename, std::fstream::out);
  46.         if (!f.is_open())
  47.         {
  48.             char* mes;
  49.             mes = new char[sizeof("Файл не октрыт!")];
  50.             strcpy(mes, "Файл не октрыт!");
  51.             throw new InvalidFile(mes);
  52.         }
  53.     }
  54.     void openFiletoRead(ifstream& f)
  55.     {
  56.         f.open(filename, std::fstream::in);
  57.         if (!f.is_open())
  58.         {
  59.             char* mes;
  60.             mes = new char[sizeof("Файл не октрыт!")];
  61.             strcpy(mes, "Файл не октрыт!");
  62.             throw new InvalidFile(mes);
  63.         }
  64.     }
  65.     void fileOutput(ifstream& f)
  66.     {
  67.         char buff[100];
  68.         try
  69.         {
  70.             cout << setw(30) << "Вывод из файла" << endl;
  71.             while (!f.eof())
  72.             {
  73.                 f.getline(buff, 100);
  74.                 cout << buff << endl;
  75.             }
  76.         }
  77.         catch (...)
  78.         {  
  79.             char* mes;
  80.             mes = new char[sizeof("Ошибка вывода!")];
  81.             strcpy(mes, "Ошибка вывода!");
  82.             throw new InvalidFile(mes);
  83.    
  84.         }
  85.     }
  86. };
  87.  
  88.  
  89.  
  90. class InvalidSymbol_Vector
  91. {
  92.     char* message;
  93. public:
  94.     InvalidSymbol_Vector()
  95.     {
  96.         char m[] = { "Обработка ошиок. Необходимо ввести символы в диапозоне 0-9" };
  97.         message = new char[sizeof(m)];
  98.         strcpy(message, m);
  99.     }
  100.     char* getMessage()
  101.     {
  102.         return message;
  103.     }
  104.     ~InvalidSymbol_Vector()
  105.     {
  106.         delete[] message;
  107.     }
  108.  
  109. };
  110.  
  111. string toBinary(unsigned x) {
  112.     string sbin;
  113.  
  114.     if (x > 0) {
  115.         while (x != 0) {
  116.             sbin = (x % 2 == 0 ? '0' : '1') + sbin;
  117.             x /= 2;
  118.         }
  119.     }
  120.     else {
  121.         sbin = "0";
  122.     }
  123.  
  124.     return sbin;
  125. }
  126.  
  127.  
  128. void f(char filename[50])
  129. {
  130.     ofstream t1;
  131.     MyFile* opfile = new MyFile(filename);
  132.     opfile->openFiletoWrite(t1);
  133.     cout << "Введите цифру с символом 0 - 9 без остальных символов: " << endl;
  134.     char y[12];
  135.     cin >> y;
  136.     int step=0;
  137.     while (y && (y[step] != NULL)) {
  138.         cout << setfill('%') << setw(12) << dec << setprecision(4) << (int)y[step] <<""<< endl;
  139.         if ((tolower(y[step]) < '0') || (tolower(y[step]) > '9'))
  140.             {
  141.             throw new InvalidSymbol_Vector();
  142.             }
  143.             step++;
  144.     }
  145.  
  146.     int yInt = atoi(y);
  147.     float x = sqrt(yInt);
  148.    
  149.  
  150.     cout << setfill('%') << setw(12) << "Входной параметр y=:" << endl;
  151.     cout << setfill('%') << setw(12) << yInt << endl;
  152.     cout << setfill('%')<< setw(12) << "Выходной параметр x=:"<<endl;
  153.     cout << setfill('%') << setw(12) << dec << setprecision(4) << x << endl;
  154.     t1 << setfill('%') << setw(12) << yInt << endl;
  155.     t1 << setfill('%') << setw(12) << dec << setprecision(4) << x << endl;
  156.  
  157.     cout << setfill(' ') << endl;
  158.     t1.close();
  159.     delete opfile;
  160.  
  161. }
  162.  
  163. int main()
  164. {
  165.     setlocale(LC_CTYPE, "rus");
  166.     char filename[50];
  167.     while (1)
  168.     {
  169.         try
  170.         {
  171.             cout.width(50);
  172.             cout << "введите имя файла для записи" << endl;
  173.             cin >> filename;
  174.             f(filename);
  175.             cout << "введите имя файла для чтения" << endl;
  176.             cin >> filename;
  177.             MyFile* opfile = new MyFile(filename);
  178.             ifstream t2;
  179.             opfile->openFiletoRead(t2);
  180.             opfile->fileOutput(t2);
  181.             delete opfile;
  182.             t2.close();
  183.         }
  184.         catch (InvalidSymbol_Vector* s)
  185.         {
  186.             cout << s->getMessage() << endl;
  187.         }
  188.         catch (InvalidFile* f)
  189.         {
  190.             cout << f->getMessage() << endl;
  191.         }
  192.         char n;
  193.         cout.width(50);
  194.         cout << "нажмите любую клавишу для продолжения . . ." << endl;
  195.         cout.width(20);
  196.         cout << right << "Введите '0' - выход" << endl;
  197.         n = _getch();
  198.         if (n == '0')
  199.         {
  200.             return(0);
  201.         }
  202.     }
  203. }
  204.  
  205.  
  206.  
  207. // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"
  208. // Отладка программы: F5 или меню "Отладка" > "Запустить отладку"
  209.  
  210. // Советы по началу работы
  211. //   1. В окне обозревателя решений можно добавлять файлы и управлять ими.
  212. //   2. В окне Team Explorer можно подключиться к системе управления версиями.
  213. //   3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения.
  214. //   4. В окне "Список ошибок" можно просматривать ошибки.
  215. //   5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода.
  216. //   6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл.
  217.  
Advertisement
Add Comment
Please, Sign In to add comment