Advertisement
RemigiuszP

Untitled

Jan 19th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Pieniadze
  6. {
  7.     public:
  8.  
  9.         int zlote;
  10.         void SetZlote(int zlote)
  11.         {
  12.             this->zlote = zlote;
  13.         }
  14.         int GetZlote()
  15.         {
  16.             return this->zlote;
  17.         }
  18.  
  19.         int grosze;
  20.         void SetGrosze(int grosze)
  21.         {
  22.             this->grosze = grosze;
  23.         }
  24.         int GetGrosze()
  25.         {
  26.             return this->grosze;
  27.         }
  28.  
  29.  
  30.  
  31.          Pieniadze(int my_grosze = 0, int my_zlote = 0)
  32.         {
  33.             if(my_grosze < 0 || my_zlote < 0)
  34.             {
  35.                 string w = "Pieniadze nie moga byc ujemne!\n";
  36.                 throw w;
  37.             }
  38.             else
  39.             {
  40.                 SetZlote(my_zlote + my_grosze / 100);
  41.                 SetGrosze(my_grosze % 100);
  42.             }
  43.         }
  44.  
  45.  
  46.         Pieniadze operator +(int toAdd)
  47.         {
  48.             return Pieniadze(((grosze + toAdd) % 100), (zlote + (grosze + toAdd) / 100));
  49.         }
  50.  
  51.         Pieniadze operator -(int toSubtract)
  52.         {
  53.             return Pieniadze(((grosze - toSubtract) % 100), (zlote - (grosze + toSubtract) / 100));
  54.         }
  55.  
  56.        Pieniadze operator - (Pieniadze &p)
  57.        {
  58.             return Pieniadze(grosze - p.grosze, zlote - p.zlote);
  59.        }
  60.  
  61.  
  62.         Pieniadze operator *(int toMultiply)
  63.         {
  64.             return Pieniadze((grosze * toMultiply) % 100, zlote * toMultiply + (grosze * toMultiply) / 100);
  65.         }
  66.  
  67.         Pieniadze operator /(int toDivide)
  68.         {
  69.             return Pieniadze((grosze / toDivide) % 100, zlote / toDivide + (grosze / toDivide) / 100);
  70.         }
  71.  
  72.         Pieniadze operator +(Pieniadze &toAdd)
  73.         {
  74.             return Pieniadze((grosze + toAdd.grosze) % 100, zlote + toAdd.zlote + (grosze + toAdd.grosze) / 100);
  75.         }
  76.  
  77.  
  78.  
  79.         friend ostream & operator<< (ostream &wyjscie, const Pieniadze &s);
  80.  
  81.         operator int()
  82.         {
  83.             return zlote;
  84.         }
  85.  
  86.  
  87.         void Ustaw(int grosze = 0, int zlote = 0)
  88.         {
  89.             SetZlote(zlote + grosze / 100);
  90.             SetGrosze(grosze % 100);
  91.         }
  92.  
  93.         void Wypisz()
  94.         {
  95.             float kwota = zlote + int(grosze / 100) + (grosze % 100) / 100;
  96.             cout << kwota;
  97.         }
  98. };
  99.  
  100. ostream & operator<< (ostream &wyjscie, const Pieniadze &p)
  101. {
  102.    return wyjscie << p.zlote << " zl " << p.grosze << "gr" << endl;
  103. }
  104.  
  105. int main()
  106. {
  107.    Pieniadze a = Pieniadze(100);
  108.    Pieniadze b(200);
  109.  
  110.  
  111.  
  112.    try
  113.    {
  114.        cout << a - b;
  115.    }
  116.    catch(string e)
  117.    {
  118.        cout << "Pojawil sie wyjatek! " << e << endl;
  119.    }
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement