Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Fraction {
- private:
- int numerator; // числитель
- int denominator; // знаменатель
- public:
- Fraction(int num, int denom) : numerator(num), denominator(denom) {}
- void input() {
- cout << "Введите числитель: ";
- cin >> numerator;
- cout << "Введите знаменатель: ";
- cin >> denominator;
- }
- void output() const {
- cout << numerator << "/" << denominator;
- }
- Fraction operator+(const Fraction& other) const {
- int num = numerator * other.denominator + other.numerator * denominator;
- int denom = denominator * other.denominator;
- return Fraction(num, denom);
- }
- Fraction operator-(const Fraction& other) const {
- int num = numerator * other.denominator - other.numerator * denominator;
- int denom = denominator * other.denominator;
- return Fraction(num, denom);
- }
- Fraction operator*(const Fraction& other) const {
- int num = numerator * other.numerator;
- int denom = denominator * other.denominator;
- return Fraction(num, denom);
- }
- Fraction operator/(const Fraction& other) const {
- int num = numerator * other.denominator;
- int denom = denominator * other.numerator;
- return Fraction(num, denom);
- }
- };
- int main() {
- Fraction f1(1, 2);
- Fraction f2(3, 4);
- Fraction sum = f1 + f2;
- Fraction diff = f1 - f2;
- Fraction prod = f1 * f2;
- Fraction quot = f1 / f2;
- cout << "Сумма дробей: ";
- sum.output();
- cout << endl;
- cout << "Разность дробей: ";
- diff.output();
- cout << endl;
- cout << "Произведение дробей: ";
- prod.output();
- cout << endl;
- cout << "Частное дробей: ";
- quot.output();
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement