Guest User

Untitled

a guest
Jan 16th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. //Написать программу, в которой описана иерархия классов: ошибка в программе
  2. //(«ошибка доступа к памяти», «математическая», «деление на ноль», «переполнение»).
  3. //Наследники должны иметь поля, содержащие дополнительные сведения об ошибке, если такие
  4. //имеются. Продемонстрировать работу всех методов классов , предоставив пользователю выбор
  5. //типа объекта для демонстрации.
  6.  
  7. #include "stdafx.h"
  8. #include <stdio.h>
  9. #include <climits>
  10. class EBaseError{
  11. public:
  12.     virtual void Print() =0;
  13.     virtual void Read() =0;
  14. };
  15. class EAccessViolation: public EBaseError{//ошибка доступа
  16.     void* m_badAddr;
  17. public:
  18.     void Print();
  19.     void Read();
  20.     EAccessViolation(void* badAddr);
  21. };
  22. class EMathError: public EBaseError{};//мат ошибка
  23. class EZeroDivide: public EMathError{//деление на ноль
  24.     double m_divident;
  25. public:
  26.     void Print();
  27.     void Read();
  28.     EZeroDivide(const double &divident);
  29. };
  30. class EOverflow: public EMathError{//переполнение
  31.     int m_operand1, m_operand2;
  32. public:
  33.     void Print();
  34.     void Read();
  35.     EOverflow(const int &operand1, const int &operand2);
  36. };
  37. EAccessViolation::EAccessViolation(void* badAddr){
  38.     m_badAddr = badAddr;
  39. }
  40. void EAccessViolation::Print(){
  41.     printf("Access violation read of address %p!", m_badAddr);
  42. }
  43. void EAccessViolation::Read(){
  44. }
  45. EZeroDivide::EZeroDivide(const double &divident){
  46.     m_divident = divident;
  47. }
  48. void EZeroDivide::Print(){
  49.     printf("There was a try to divide %lf by zero!", m_divident);
  50. }
  51. void EZeroDivide::Read(){
  52. }
  53. EOverflow::EOverflow(const int &operand1, const int &operand2){
  54.     m_operand1 = operand1;
  55.     m_operand2 = operand2;
  56. }
  57. void EOverflow::Print(){
  58.     printf("There was an overflow during some operation between %d and %d!", m_operand1, m_operand2);
  59. }
  60. void EOverflow::Read(){
  61. }
  62. void disposeError(EBaseError** e){
  63.     if(*e!=NULL) delete *e;
  64.     *e = NULL;
  65. }
  66. int main(){
  67.     EBaseError* e = NULL;
  68.     char c = 0, tmp;
  69.     while(c!=27){
  70.         printf("\n deistvie:\n 1 - oshibka dostupa\n");
  71.         printf(" 2 - podelit' 2 4isla\n 3 - umno 2 4isla\n");
  72.         printf(" 0 - exit\n");
  73.         scanf("%c", &c);
  74.         switch(c) {
  75.         case '1': e = new EAccessViolation((void*) &c); break;
  76.         case '2':
  77.             double a, b;
  78.             printf("\nvvedite delimoe");
  79.             scanf("%lf", &a);
  80.             printf("\ndelitel'");
  81.             scanf("%lf", &b);
  82.             if (b==0.0) e = new EZeroDivide(a);
  83.             else printf("\n otvet = %lf", a/b);
  84.             break;
  85.         case '3':
  86.             int i, j;
  87.             long long res;
  88.             printf("\n 1mn");
  89.             scanf("%d", &i);
  90.             printf("\n 2mn");
  91.             scanf("%d", &j);
  92.             res = i;
  93.  
  94.             res = i*j;
  95.             if (res>INT_MAX || res<INT_MIN) e = new
  96.                 EOverflow(i, j);
  97.             else printf("\n otvet = %d", res);
  98.             break;
  99.         case '0' :c = 27;
  100.         }
  101.         scanf("%c", &tmp);//считываем Enter оставшийся в буфере ввода после предыдущего scanf
  102.         if(e != NULL) {
  103.             printf("\n");
  104.             e->Print();
  105.             printf("\n");
  106.         }
  107.         disposeError(&e);
  108.     }
  109.     return 0;
  110. };
Add Comment
Please, Sign In to add comment