#include <iostream>
class Fraction {
private:
int numerator;
int denominator;
public:
Fraction() {
numerator = 0;
denominator = 1;
}
Fraction(int num, int den) {
numerator = num;
denominator = den;
void inputFraction() {
std::cout << "Введіть чисельник: ";
std::cin >> numerator;
std::cout << "Введіть знаменник: ";
std::cin >> denominator;
void displayFraction() {
std::cout << numerator << "/" << denominator << std::endl;
Fraction add(const Fraction& other) const {
Fraction result;
result.numerator = numerator * other.denominator + other.numerator * denominator;
result.denominator = denominator * other.denominator;
return result;
Fraction subtract(const Fraction& other) const {
result.numerator = numerator * other.denominator - other.numerator * denominator;
Fraction multiply(const Fraction& other) const {
result.numerator = numerator * other.numerator;
Fraction divide(const Fraction& other) const {
result.numerator = numerator * other.denominator;
result.denominator = denominator * other.numerator;
};
int main() {
Fraction fraction1, fraction2;
fraction1.inputFraction();
fraction2.inputFraction();
fraction1.displayFraction();
fraction2.displayFraction();
Fraction sum = fraction1.add(fraction2);
std::cout << "Сума дробів: ";
sum.displayFraction();
Fraction difference = fraction1.subtract(fraction2);
std::cout << "Різниця дробів: ";
difference.displayFraction();
Fraction product = fraction1.multiply(fraction2);
std::cout << "Добуток дробів: ";
product.displayFraction();
Fraction quotient = fraction1.divide(fraction2);
std::cout << "Частка дробів: ";
quotient.displayFraction();
return 0;
#include <iostream>
class Fraction {
private:
int numerator;
int denominator;
public:
Fraction() {
numerator = 0;
denominator = 1;
}
Fraction(int num, int den) {
numerator = num;
denominator = den;
}
void inputFraction() {
std::cout << "Введіть чисельник: ";
std::cin >> numerator;
std::cout << "Введіть знаменник: ";
std::cin >> denominator;
}
void displayFraction() {
std::cout << numerator << "/" << denominator << std::endl;
}
Fraction add(const Fraction& other) const {
Fraction result;
result.numerator = numerator * other.denominator + other.numerator * denominator;
result.denominator = denominator * other.denominator;
return result;
}
Fraction subtract(const Fraction& other) const {
Fraction result;
result.numerator = numerator * other.denominator - other.numerator * denominator;
result.denominator = denominator * other.denominator;
return result;
}
Fraction multiply(const Fraction& other) const {
Fraction result;
result.numerator = numerator * other.numerator;
result.denominator = denominator * other.denominator;
return result;
}
Fraction divide(const Fraction& other) const {
Fraction result;
result.numerator = numerator * other.denominator;
result.denominator = denominator * other.numerator;
return result;
}
};
int main() {
Fraction fraction1, fraction2;
fraction1.inputFraction();
fraction2.inputFraction();
fraction1.displayFraction();
fraction2.displayFraction();
Fraction sum = fraction1.add(fraction2);
std::cout << "Сума дробів: ";
sum.displayFraction();
Fraction difference = fraction1.subtract(fraction2);
std::cout << "Різниця дробів: ";
difference.displayFraction();
Fraction product = fraction1.multiply(fraction2);
std::cout << "Добуток дробів: ";
product.displayFraction();
Fraction quotient = fraction1.divide(fraction2);
std::cout << "Частка дробів: ";
quotient.displayFraction();
return 0;
}