Guest User

Untitled

a guest
Aug 20th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include "razlomak.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. razlomak::razlomak()
  7. {
  8.     _x=0;
  9.     _y=1;
  10. }
  11.  
  12. razlomak::razlomak(int x, unsigned int y)
  13. {
  14.     _x=x;
  15.     if (y!=0)
  16.         _y=y;
  17.     else
  18.         _y=1;
  19. }
  20.  
  21. razlomak::razlomak(const razlomak &x)
  22. {
  23.     _x=x._x;
  24.     _y=x._y;
  25. }
  26.  
  27. int razlomak::vratiBr(void) const {return _x;}
  28.  
  29. unsigned int razlomak::vratiIm(void) const {return _y;}
  30.  
  31. razlomak razlomak::pomnozi(const razlomak &a) const
  32. {
  33.     return razlomak(_x*a._x, _y*a._y);
  34. }
  35.  
  36. razlomak razlomak::saberi(const razlomak &a) const
  37. {
  38.     return razlomak(_x*a._y+a._x*_y,_y*a._y);
  39. }
  40.  
  41. razlomak razlomak::skrati() const
  42. {
  43.     int x=_x;
  44.     unsigned y=_y;
  45.  
  46.     while (x!=y)
  47.         if (x>y)
  48.             x-=y;
  49.         else
  50.             y-=x;
  51.     y=_y/x;
  52.     x=_x/x;
  53.     return razlomak(x,y);
  54. }
  55.  
  56. razlomak& razlomak::operator= (const razlomak &r)
  57. {
  58.     if (this != &r)
  59.     {
  60.         _x = r._x;
  61.         _y = r._y;
  62.     }
  63.  
  64.     return *this;
  65.  
  66. }
  67.  
  68. istream& operator>> (istream& in, razlomak& r)
  69. {
  70.     char c;
  71.     in >> r._x >> c >> r._y;
  72.     return in;
  73. }
  74.  
  75. ostream& operator<< (ostream& out, const razlomak& r)
  76. {
  77.     out << r._x << "/" << r._y;
  78.     return out;
  79. }
Add Comment
Please, Sign In to add comment