Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <tchar.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class fraction
  8. {
  9. private:
  10.     int num; //числитель
  11.     int den; //знаменатель
  12.     double des; //десятичная
  13. public:
  14.     int getnum()
  15.     {
  16.         return num;
  17.     }
  18.  
  19.     int getden()
  20.     {
  21.         return den;
  22.     }
  23.  
  24.     double getdes()
  25.     {
  26.         return des;
  27.     }
  28.  
  29.     void setnum(int _num)
  30.     {
  31.         if (_num > 0)
  32.         {
  33.             num = _num;
  34.         }
  35.     }
  36.  
  37.     void setden(int _den)
  38.     {
  39.         if (_den > 0)
  40.         {
  41.             den = _den;
  42.         }
  43.     }
  44.  
  45.     void setdes()
  46.     {
  47.         des =(double)  this->num / this->den ;
  48.     }
  49.    
  50.     void print()
  51.     {
  52.         cout << this->num << '/' << this->den << endl;
  53.     }
  54.  
  55.     //конструторы
  56.  
  57.     fraction ()
  58.     {
  59.         num = 0;
  60.         den = 0;
  61.     }
  62.  
  63.     fraction(int _n)
  64.     {
  65.         num = _n;
  66.         den = 1;
  67.     }
  68.  
  69.     fraction(int _n, int _d)
  70.     {
  71.         if (_d > 0)
  72.         {
  73.             num = _n;
  74.             den = _d;
  75.         }
  76.        
  77.     }
  78.    
  79.  
  80.     //арифм операторы
  81.  
  82.     fraction operator*(fraction other)
  83.     {
  84.         int i = this->num * other.num;
  85.         int d = this->den * other.den;
  86.         fraction rez(i,d);
  87.         return rez;
  88.     }
  89.  
  90.     fraction operator/(fraction other)
  91.     {
  92.         int i = this->num * other.den;
  93.         int d = this->den * other.num;
  94.         fraction rez(i, d);
  95.         return rez;
  96.     }
  97.  
  98.     fraction operator+(fraction other)
  99.     {
  100.  
  101.         int tnum = this->num * other.den;
  102.         int onum = other.num * this->den;
  103.         int aden = this->den * other.den;
  104.         int anum = tnum + onum;
  105.         fraction rez(anum , onum);
  106.         return rez;
  107.     }
  108.  
  109.     fraction operator-(fraction other)
  110.     {
  111.         int tnum = this->num * other.den;
  112.         int onum = other.num * this->den;
  113.         int aden = this->den * other.den;
  114.         int anum = tnum - onum;
  115.         fraction rez(anum , onum);
  116.         return rez;
  117.     }
  118.  
  119.     //логические операторы
  120.  
  121.     fraction operator==(fraction other)
  122.     {
  123.         return (this->den == other.den ? (this->num == other.num ? true : false) : false);
  124.     }
  125.  
  126.     fraction operator!=(fraction other)
  127.     {
  128.         return (this->den != other.den ? (this->num != other.num ? true : false) : false);
  129.     }
  130.  
  131.     fraction operator>(fraction other)
  132.     {
  133.        
  134.         return (this->den == other.den ? (this->num == other.num ? true : false) : false);
  135.     }
  136. };
  137.  
  138.  
  139.  
  140.  
  141. int _tmain(int argc, _TCHAR* argv[])
  142. {
  143.    
  144.     fraction b(3);
  145.     fraction c(2, 4);
  146.     c.setdes();
  147.     cout << c.getdes() << endl;
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement