mirea-projects/Second term/Industrial programming technologies/3/2.cpp

46 lines
1015 B
C++
Raw Permalink Normal View History

2024-09-23 23:22:33 +00:00
#include <vector>
#include <numeric>
#include <iostream>
using namespace std;
class Fraction {
public:
int a, b;
Fraction();
Fraction(int a, int b);
void print();
Fraction operator+(const Fraction &fraction) {
return Fraction(this->a * fraction.b + this->b * fraction.a, this->b * fraction.b);
}
Fraction operator*(const Fraction &fraction) {
return Fraction(this->a * fraction.a, this->b * fraction.b);
}
Fraction operator/(const Fraction &fraction) {
return Fraction(this->a * fraction.b, this->b * fraction.a);
}
};
Fraction::Fraction() {
this->a = 0;
this->b = 1;
}
Fraction::Fraction(int a, int b) {
this->a = a;
this->b = b;
if (this->a == 0 && this->b == 0) return;
if (this->a >= this->b) this->a %= this->b;
if (this->a == 0) this->b = 1;
int divisor = gcd(this->a, this->b);
this->a /= divisor;
this->b /= divisor;
}
void Fraction::print() { cout << this->a << "/" << this->b << endl; }