Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Cfraction
- {
- private:
- short up;
- short down;
- short integer;
- public:
- Cfraction ()
- {
- setInteger(0);
- setUp (0);
- setDown (1);
- }
- Cfraction (short _up, short _down)
- {
- setInteger(0);
- setUp (_up);
- setDown (_down);
- }
- void setUp (short _up) {up = _up;}
- void setDown (short _down) {down = _down;}
- void setInteger (short _int) {integer = _int;}
- void Print ()
- {
- Check ();
- cout << integer << " " << up << "/" << down << endl;
- }
- void Check ()
- {
- for (int i = 0; i < 3; i++)
- {
- for (int j = 1000; j > 0; j--)
- {
- if (up % j == 0 && down % j == 0)
- {
- up /= j;
- down /= j;
- }
- }
- }
- if (up >= down)
- {
- integer = up/down;
- up%=down;
- }
- if (up == 0)
- down = 1;
- }
- void Convert (Cfraction &fr)
- {
- fr.up += fr.integer * fr.down;
- fr.integer = 0;
- }
- /////////////+\\\\\\\\\\\\\
- Cfraction operator + (Cfraction &fr)
- {
- Convert (fr);
- Cfraction tmp;
- tmp.down = this->down * fr.down;
- tmp.up = (this->down * fr.up) + (this->up * fr.down);
- return tmp;
- }
- Cfraction operator + (int _int)
- {
- Cfraction rgt;
- rgt.integer = _int;
- this->Convert (rgt);
- Cfraction tmp;
- tmp.down = this->down * rgt.down;
- tmp.up = (this->down * rgt.up) + (this->up * rgt.down);
- return tmp;
- }
- friend Cfraction operator + (int _int, Cfraction &fr)
- {
- Cfraction tmp;
- Cfraction lft;
- lft.integer = _int;
- lft.up += lft.integer * lft.down;
- lft.integer = 0;
- tmp.down = lft.down * fr.down;
- tmp.up = (lft.down * fr.up) + (lft.up * fr.down);
- return tmp;
- }
- /////////////*\\\\\\\\\\\\\
- Cfraction operator * (Cfraction &fr)
- {
- Convert (fr);
- Cfraction tmp;
- tmp.up = this->up * fr.up;
- tmp.down = this->down * fr.down;
- return tmp;
- }
- Cfraction operator * (int _int)
- {
- Cfraction tmp;
- Cfraction fr;
- fr.integer = _int;
- Convert (fr);
- tmp.up = this->up * fr.up;
- tmp.down = this->down * fr.down;
- return tmp;
- }
- friend Cfraction operator * (int _int, Cfraction &fr)
- {
- Cfraction tmp;
- Cfraction lft;
- lft.integer = _int;
- lft.up += lft.integer * lft.down;
- lft.integer = 0;
- tmp.up = lft.up * fr.up;
- tmp.down = lft.down * fr.down;
- return tmp;
- }
- //////////////-\\\\\\\\\\\\\\
- Cfraction operator - (Cfraction &fr)
- {
- Convert (fr);
- Cfraction tmp;
- tmp.down = this->down * fr.down;
- tmp.up = (this->down * fr.up) - (this->up * fr.down);
- return tmp;
- }
- Cfraction operator - (int _int)
- {
- Cfraction tmp;
- Cfraction fr;
- fr.integer = _int;
- Convert (fr);
- tmp.down = this->down * fr.down;
- tmp.up = (this->down * fr.up) - (this->up * fr.down);
- return tmp;
- }
- friend Cfraction operator - (int _int, Cfraction &fr)
- {
- Cfraction tmp;
- Cfraction lft;
- lft.integer = _int;
- lft.Convert(lft);
- tmp.down = lft.down * fr.down;
- tmp.up = (lft.down * fr.up) - (lft.up * fr.down);
- return tmp;
- }
- /////////// / \\\\\\\\\\\\\\\\\\\\\\\
- Cfraction operator / (Cfraction &fr)
- {
- Convert (fr);
- Cfraction tmp;
- tmp.up = this->up * fr.down;
- tmp.down = this->down * fr.up;
- return tmp;
- }
- Cfraction operator / (int _int)
- {
- Cfraction tmp;
- Cfraction fr;
- fr.integer = _int;
- Convert (fr);
- tmp.up = this->up * fr.down;
- tmp.down = this->down * fr.up;
- return tmp;
- }
- friend Cfraction operator / (int _int, Cfraction fr)
- {
- Cfraction tmp;
- Cfraction lft;
- lft.integer = _int;
- lft.Convert(lft);
- tmp.up = lft.up * fr.down;
- tmp.down = lft.down * fr.up;
- return tmp;
- }
- Cfraction operator ++ ()
- {
- this->integer++;
- return *this;
- }
- Cfraction operator -- ()
- {
- this->integer--;
- return *this;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement