Top.Mail.Ru
Ответы

Программа на с++

Есть задание на дроби.... Программа должна произвести сумму, разность, деление, умножения и сравнение. Проблема в том что в моей работе нет функции сравнения, а как её написать я не знаю. Помогите пожалуйста)

#include <iostream>
using namespace std;
class Rational {
public:
using llong = long long;
Rational(llong n, llong d) : n1(n), d2(d) {
reduce_();
}
Rational add(const Rational& x) { //+
auto n = n1 * x.d2 + d2 * x.n1;
auto d = d2 * x.d2;
return Rational(n, d);
}
Rational sub(const Rational& x) { //-
auto n = n1 * x.d2 - d2 * x.n1;
auto d = d2 * x.d2;
return Rational(n, d);
}
Rational mul(const Rational& x) { //*
auto n = n1 * x.n1;
auto d = d2 * x.d2;
return Rational(n, d);
}
Rational div(const Rational& x) {
auto n = n1 * x.d2;
auto d = d2 * x.n1;
return Rational(n1, d);
Rational com(const Rational& x) {
auto n = n1/x.d2;
auto d = d2/x.n1;
if (n>d) return 1;
else if (n<d) return -1;
else if (n==d) return 0;
}
private:
int n1;
int d2;
void reduce_() {
auto x = gcd(n1, d2); // определение общего делителя
n1 =n1/x;
d2 =d2/x;
if (n1 < 0 && d2 < 0) {
n1 = abs(n1);
d2 = abs(d2);
}
}
int gcd(llong a, llong b) { //работа с чеслителем
a = abs(a);
b = abs(b);
while (a != b) {
if (a > b) swap(a, b);
b = b - a;
}
return a;
}
friend ostream & operator<<(ostream& out, const Rational& x) // если значения <0
{
if (x.n1 < 0 || x.d2 < 0) out << '-';
out << '(' << abs(x.n1) << ", " << abs(x.d2) << ')';
return out;
}
};

int main() {
int m, n, m1, n1;
cout<<"vvedite m: " ;
cin>> m;
cout<< "vvedite n: ";
cin>> n;
cout<<"vvedite m1: " ;
cin>> m1;
cout<< "vvedite n1: ";
cin>> n1;
Rational a(m, n);
Rational b(m1, n1);
auto add = a.add(b);
auto sub = a.sub(b);
auto mul = a.mul(b);
auto div = a.div(b);
cout
<< a << " + " << b << " = " << add << '\n'
<< a << " - " << b << " = " << sub << '\n'
<< a << " * " << b << " = " << mul << '\n'
<< a << " / " << b << " = " << div << '\n';
<< a << " > " << b << " = " << div << '\n';
}
}

По дате
По рейтингу
Аватар пользователя
Новичок

Приводи дроби к наименьшему общему кратному знаменателей и сравнивай числители.

Аватар пользователя
Искусственный Интеллект

Функция сравнения в этом коде есть, но вызывается неправильно

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
 #include <iostream> 
using namespace std; 
 
class Rational { 
public: 
    using llong = long long; 
    Rational(llong n, llong d) : n1(n), d2(d) { 
        reduce_(); 
    } 
 
    Rational add(const Rational& x) { 
        auto n = n1 * x.d2 + d2 * x.n1; 
        auto d = d2 * x.d2; 
        return Rational(n, d); 
    } 
 
    Rational sub(const Rational& x) { 
        auto n = n1 * x.d2 - d2 * x.n1; 
        auto d = d2 * x.d2; 
        return Rational(n, d); 
    } 
 
    Rational mul(const Rational& x) { 
        auto n = n1 * x.n1; 
        auto d = d2 * x.d2; 
        return Rational(n, d); 
    } 
 
    Rational div(const Rational& x) { 
        auto n = n1 * x.d2; 
        auto d = d2 * x.n1; 
        return Rational(n1, d); 
    } 
 
    int com(const Rational& x) { 
        auto n = n1 * x.d2; 
        auto d = d2 * x.n1; 
        if (n > d) return 1; 
        else if (n < d) return -1; 
        else return 0; 
    } 
 
private: 
    int n1; 
    int d2; 
 
    void reduce_() { 
        auto x = gcd(n1, d2); 
        n1 = n1 / x; 
        d2 = d2 / x; 
        if (n1 < 0 && d2 < 0) { 
            n1 = abs(n1); 
            d2 = abs(d2); 
        } 
    } 
 
    int gcd(llong a, llong b) { 
        a = abs(a); 
        b = abs(b); 
        while (a != b) { 
            if (a > b) swap(a, b); 
            b = b - a; 
        } 
        return a; 
    } 
 
    friend ostream& operator<<(ostream& out, const Rational& x) { 
        if (x.n1 < 0 || x.d2 < 0) out << '-'; 
        out << '(' << abs(x.n1) << ", " << abs(x.d2) << ')'; 
        return out; 
    } 
}; 
 
int main() { 
    int m, n, m1, n1; 
    cout << "vvedite m: "; 
    cin >> m; 
    cout << "vvedite n: "; 
    cin >> n; 
    cout << "vvedite m1: "; 
    cin >> m1; 
    cout << "vvedite n1: "; 
    cin >> n1; 
 
    Rational a(m, n); 
    Rational b(m1, n1); 
 
    auto add = a.add(b); 
    auto sub = a.sub(b); 
    auto mul = a.mul(b); 
    auto div = a.div(b); 
     
    cout 
        << a << " + " << b << " = " << add << '\n' 
        << a << " - " << b << " = " << sub << '\n' 
        << a << " * " << b << " = " << mul << '\n' 
        << a << " / " << b << " = " << div << '\n' 
        << "Comparison result: " << a.com(b) << '\n'; 
 
    return 0; 
}