Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <tchar.h>
- using namespace std;
- class fraction
- {
- private:
- int num; //числитель
- int den; //знаменатель
- double des; //десятичная
- public:
- int getnum()
- {
- return num;
- }
- int getden()
- {
- return den;
- }
- double getdes()
- {
- return des;
- }
- void setnum(int _num)
- {
- if (_num > 0)
- {
- num = _num;
- }
- }
- void setden(int _den)
- {
- if (_den > 0)
- {
- den = _den;
- }
- }
- void setdes()
- {
- des =(double) this->num / this->den ;
- }
- void print()
- {
- cout << this->num << '/' << this->den << endl;
- }
- //конструторы
- fraction ()
- {
- num = 0;
- den = 0;
- }
- fraction(int _n)
- {
- num = _n;
- den = 1;
- }
- fraction(int _n, int _d)
- {
- if (_d > 0)
- {
- num = _n;
- den = _d;
- }
- }
- //арифм операторы
- fraction operator*(fraction other)
- {
- int i = this->num * other.num;
- int d = this->den * other.den;
- fraction rez(i,d);
- return rez;
- }
- fraction operator/(fraction other)
- {
- int i = this->num * other.den;
- int d = this->den * other.num;
- fraction rez(i, d);
- return rez;
- }
- fraction operator+(fraction other)
- {
- int tnum = this->num * other.den;
- int onum = other.num * this->den;
- int aden = this->den * other.den;
- int anum = tnum + onum;
- fraction rez(anum , onum);
- return rez;
- }
- fraction operator-(fraction other)
- {
- int tnum = this->num * other.den;
- int onum = other.num * this->den;
- int aden = this->den * other.den;
- int anum = tnum - onum;
- fraction rez(anum , onum);
- return rez;
- }
- //логические операторы
- fraction operator==(fraction other)
- {
- return (this->den == other.den ? (this->num == other.num ? true : false) : false);
- }
- fraction operator!=(fraction other)
- {
- return (this->den != other.den ? (this->num != other.num ? true : false) : false);
- }
- fraction operator>(fraction other)
- {
- return (this->den == other.den ? (this->num == other.num ? true : false) : false);
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- fraction b(3);
- fraction c(2, 4);
- c.setdes();
- cout << c.getdes() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement