Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "iostream"
- using namespace std;
- // Класс дробь. В полях знаменатель и числитель. Методы для ввода, сложения, вычитания, умножения, деления, вывода
- class Fraction
- {
- public:
- void Set(int numerator, int denominator)
- {
- numer = numerator;
- denom = denominator;
- }
- void Mult(Fraction second)
- {
- numer = numer * second.numer;
- denom = denom * second.denom;
- Reduce();
- }
- void Div(Fraction second)
- {
- numer = numer * second.denom;
- denom = denom * second.numer;
- Reduce();
- }
- void Sum(Fraction second)
- {
- Fraction result;
- if (denom != second.denom)
- {
- result.denom = Adjust(second);
- }
- else
- {
- result.denom = denom;
- }
- result.numer = numer + second.numer;
- numer = result.numer;
- denom = result.denom;
- }
- void Substr(Fraction second)
- {
- Fraction result;
- if (denom != second.denom)
- {
- result.denom = Adjust(second);
- }
- else
- {
- result.denom = denom;
- }
- result.numer = numer - second.numer;
- numer = result.numer;
- denom = result.denom;
- }
- void Print()
- {
- cout << numer << '/' << denom << endl;
- }
- void Reduce()
- {
- int bigger = numer > denom ? numer : denom;
- int smaller = numer < denom ? numer : denom;
- for (int i = smaller; i >= 1; i--)
- {
- if (bigger % i == 0 && smaller % i == 0)
- {
- numer /= i;
- denom /= i;
- return;
- }
- }
- }
- private:
- int numer;
- int denom;
- int Adjust(Fraction& second)
- {
- // GCD - greatest common divisor
- Fraction GCDdenom;
- GCDdenom.numer = denom > second.denom ? denom : second.denom;
- GCDdenom.denom = denom < second.denom ? denom : second.denom;
- GCDdenom.Reduce();
- int GDC = GCDdenom.numer * GCDdenom.denom;
- numer *= GDC / denom;
- second.numer *= GDC / second.denom;
- return GDC;
- }
- };
- int main()
- {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement