Advertisement
danii-2000

Untitled

Dec 10th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include "My_Exeption.h"
  2. #include <iostream>
  3. #include <cctype>
  4. #include <regex>
  5. #include <exception>
  6.  
  7.  
  8. int fun1() throw (exeption_n)
  9. {
  10.     bool fl = false;
  11.     int x = 0;
  12.     cin >> x;
  13.     fl = true;
  14.     if (!cin.good() || cin.peek() != '\n')
  15.     {
  16.         throw exeption_n(2);
  17.     }
  18.     if (x<0)
  19.     {
  20.         throw exeption_n(3);
  21.     }
  22.     return x;
  23. }
  24.  
  25. char* fun2() throw (exeption_n)
  26. {
  27.     bool fl = false;
  28.     char* str;
  29.     str = new char[80];
  30.     fgets(str,sizeof(str),stdin);//ограничиваем ввод размера нашего буфера
  31.     fl = true;
  32.     for (int i = 0; i<sizeof(str); i++)
  33.     {
  34.         if (false == regex_match(str, regex("[[:alpha:]]+")))
  35.         {
  36.             throw exeption_n(1);
  37.         }
  38.     }
  39.     return str;
  40. }
  41. char* fun3()throw(exeption_n)
  42. {
  43.     bool fl = false;
  44.     char *str;
  45.     str = new char[80];
  46.     for (int i = 0; i < sizeof(str); i++)
  47.     {
  48.         if (!((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) || (str[i] >= '0'&&str[i] <= '9'))
  49.             throw exeption_n(4);
  50.     }
  51.     return str;
  52. }
  53.  
  54. exeption_n::exeption_n(int _number)
  55. {
  56.     this->number = _number;
  57.     switch (_number)
  58.     {
  59.     case 1:
  60.     {
  61.         this->name = " Ожидался ввод только английских букв";
  62.         break;
  63.     }
  64.     case 2:
  65.     {
  66.         this->name = " Ожидался ввод только целочисленного типа";
  67.         break;
  68.     }
  69.     case 3:
  70.     {
  71.         this->name = " Введено отрицательное число";
  72.         break;
  73.     }
  74.     case 4:
  75.     {
  76.         this->name = "Введите число или букву";
  77.         break;
  78.     }
  79.     case 88:
  80.     {
  81.         this->name = " Необработанное исключение";
  82.         break;
  83.     }
  84.     case 77:
  85.     {
  86.         this->name = " error";
  87.         break;
  88.     }
  89.     }
  90.  
  91. }
  92.  
  93. exeption_n::exeption_n(const exeption_n & tmp)
  94. {
  95.     this->number = tmp.number;
  96.     this->name = tmp.name;
  97. }
  98.  
  99.  
  100. exeption_n::~exeption_n()
  101. {
  102. }
  103.  
  104. void operator << (std::ostream & os, exeption_n & tmp)
  105. {
  106.     os << "Код исключения:" << tmp.number << endl << "Ошибка:" << tmp.name << endl << endl;
  107. }
  108. char enter_int_char()
  109. {
  110.     bool fl = false;
  111.     char* str;
  112.     str = new char[80];
  113.     do {
  114.         try
  115.         {
  116.             str= fun3();
  117.             fl = true;
  118.         }
  119.             catch (exeption_n &ex)
  120.             {
  121.                 cout << ex;
  122.                 fl = false;
  123.                 cin.clear();
  124.                 rewind(stdin);
  125.                 cin.sync();
  126.             }
  127.             catch (...)
  128.             {
  129.                 cout << "catch(...)" << endl;
  130.                 fl = false;
  131.                 cin.clear();
  132.                 cin.sync();
  133.             }
  134.         } while (fl == false);
  135.         return *str;
  136.     }
  137. int enter_int()
  138. {
  139.     bool fl = false;
  140.     int k = 0;
  141.     do
  142.     {
  143.         try
  144.         {
  145.             k = fun1();
  146.             fl = true;
  147.         }
  148.  
  149.         catch (exeption_n &ex)
  150.         {
  151.             cout << ex;
  152.             fl = false;
  153.             cin.clear();
  154.             rewind(stdin);
  155.             cin.sync();
  156.         }
  157.         catch (...)
  158.         {
  159.             cout << "catch(...)" << endl;
  160.             fl = false;
  161.             cin.clear();
  162.             cin.sync();
  163.         }
  164.     } while (fl == false);
  165.     return k;
  166. }
  167.  
  168. char* enter_char()
  169. {
  170.     bool fl = false;
  171.     char* str;
  172.     str = new char[80];
  173.     do
  174.     {
  175.         try
  176.         {
  177.             str = fun2();
  178.             fl = true;
  179.         }
  180.         catch (exeption_n &ex)
  181.         {
  182.             cout << ex;
  183.             fl = false;
  184.             cin.clear();
  185.             rewind(stdin);
  186.             cin.sync();
  187.         }
  188.         catch (...)
  189.         {
  190.             cout << "catch(...)" << endl;
  191.             fl = false;
  192.             cin.clear();
  193.             rewind(stdin);
  194.             cin.sync();
  195.         }
  196.     } while (fl == false);
  197.     return str;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement