Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. ostream operator<<(ostream& o, const Operand& obj)
  6. {
  7.     o << obj.numar;
  8. }
  9.  
  10. class Operand
  11. {
  12. public:
  13.     Operand() { numar = 0; }
  14.     Operand(int n) :numar(n){}
  15.     friend Calculator;
  16.     Operand operator+(Operand& obj)
  17.     {
  18.         return Operand(numar + obj.numar);
  19.     }
  20.     Operand operator/(Operand& obj)
  21.     {
  22.         return Operand(numar / obj.numar);
  23.     }
  24.     friend ostream operator<<(ostream& o, const Operand& obj);
  25. private:
  26.     int numar;
  27. };
  28.  
  29. class UC
  30. {
  31. public:
  32.     UC() { nr1 = 0; nr2 = 0; operatie = ""; }
  33.     UC(int n1, int n2, string op): nr1(n1), nr2(n2), operatie(op){}
  34. protected:
  35.     int nr1;
  36.     int nr2;
  37.     string operatie;
  38. };
  39.  
  40. class ALU:public UC
  41. {
  42. public:
  43.     ALU()
  44.     {
  45.         operatie = UC::operatie;
  46.     }
  47.     Operand calcul()
  48.     {
  49.         if (operatie == "+")
  50.             return (nr1 + nr2);
  51.         else
  52.             if (operatie == "/")
  53.                 return (nr1 / nr2);
  54.             else
  55.                 cout << "Operatie incorecta";
  56.     }
  57. private:
  58.     string operatie;
  59. };
  60.  
  61. class UM:public UC, public ALU
  62. {
  63. public:
  64.     friend ALU;
  65.     UM()
  66.     {
  67.         op1 = UC::nr1;
  68.         op2 = UC::nr2;
  69.         rezultat = 0;
  70.     }
  71.     void stocareDate()
  72.     {
  73.         rezultat = calcul();
  74.     }
  75. protected:
  76.     Operand op1;
  77.     Operand op2;
  78.     Operand rezultat;
  79. };
  80.  
  81. class UA: public UM
  82. {
  83. public:
  84.     void Afisare()
  85.     {
  86.         cout << op1 << "+" << op2 << "=" << rezultat;
  87.     }
  88. };
  89.  
  90. class Calculator
  91. {
  92. public:
  93.     Calculator(UC _uc, ALU _alu, UM _um, UA _ua): uc(_uc), alu(_alu), um(_um), ua(_ua){}
  94.     void executare()
  95.     {
  96.         ua.Afisare();
  97.     }
  98. private:
  99.     UC uc;
  100.     ALU alu;
  101.     UM um;
  102.     UA ua;
  103. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement