Advertisement
plarmi

hwcpp_1_1

Jun 14th, 2023
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Fraction {
  6. private:
  7.     int numerator;      // числитель
  8.     int denominator;    // знаменатель
  9.  
  10. public:
  11.     Fraction(int num, int denom) : numerator(num), denominator(denom) {}
  12.  
  13.     void input() {
  14.         cout << "Введите числитель: ";
  15.         cin >> numerator;
  16.         cout << "Введите знаменатель: ";
  17.         cin >> denominator;
  18.     }
  19.  
  20.     void output() const {
  21.         cout << numerator << "/" << denominator;
  22.     }
  23.  
  24.     Fraction operator+(const Fraction& other) const {
  25.         int num = numerator * other.denominator + other.numerator * denominator;
  26.         int denom = denominator * other.denominator;
  27.         return Fraction(num, denom);
  28.     }
  29.  
  30.     Fraction operator-(const Fraction& other) const {
  31.         int num = numerator * other.denominator - other.numerator * denominator;
  32.         int denom = denominator * other.denominator;
  33.         return Fraction(num, denom);
  34.     }
  35.  
  36.     Fraction operator*(const Fraction& other) const {
  37.         int num = numerator * other.numerator;
  38.         int denom = denominator * other.denominator;
  39.         return Fraction(num, denom);
  40.     }
  41.  
  42.     Fraction operator/(const Fraction& other) const {
  43.         int num = numerator * other.denominator;
  44.         int denom = denominator * other.numerator;
  45.         return Fraction(num, denom);
  46.     }
  47. };
  48.  
  49. int main() {
  50.     Fraction f1(1, 2);
  51.     Fraction f2(3, 4);
  52.  
  53.     Fraction sum = f1 + f2;
  54.     Fraction diff = f1 - f2;
  55.     Fraction prod = f1 * f2;
  56.     Fraction quot = f1 / f2;
  57.  
  58.     cout << "Сумма дробей: ";
  59.     sum.output();
  60.     cout << endl;
  61.  
  62.     cout << "Разность дробей: ";
  63.     diff.output();
  64.     cout << endl;
  65.  
  66.     cout << "Произведение дробей: ";
  67.     prod.output();
  68.     cout << endl;
  69.  
  70.     cout << "Частное дробей: ";
  71.     quot.output();
  72.     cout << endl;
  73.  
  74.     return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement