抽象数据类型复数和有理数的代码实现

#include 
using namespace std;

class Complex {
    private:
    double real;
    double imag;

    public:
    Complex(double r = 0, double i = 0);
    double get_real(void);
    double get_imag(void);
    void set_real(double r);
    void set_imag(double i);
    void print(void);
    friend Complex operator+(Complex &c1, Complex &c2);
    friend Complex operator-(Complex &c1, Complex &c2);
    friend Complex operator*(Complex &c1, Complex &c2);
    friend Complex operator/(Complex &c1, Complex &c2);
    friend bool operator==(Complex &c1, Complex &c2);
};

Complex::Complex(double r, double i) {
    real = r;
    imag = i;
}

double Complex::get_real(void) {
    return real;
}

double Complex::get_imag(void) {
    return imag;
}

void Complex::set_real(double r) {
    real = r;
}

void Complex::set_imag(double i) {
    imag = i;
}

void Complex::print(void) {
    cout << real;

    if (imag > 0) {
        cout << "+";
    }

    if (imag != 0) {
        cout << imag << 'i' << endl;
    }
}

Complex operator+(Complex &c1, Complex &c2) {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

Complex operator-(Complex &c1, Complex &c2) {
    return Complex(c1.real - c2.real, c1.imag - c2.imag);
}

Complex operator*(Complex &c1, Complex &c2) {
    return Complex(c1.real * c2.real - c1.imag * c2.imag,
                    c1.real * c2.imag + c1.imag * c2.real); 
}

Complex operator/(Complex &c1, Complex &c2) {
    double temp = 1 / (c2.real * c2.real + c2.imag * c2.imag);

    return Complex((c1.real * c2.real + c1.imag * c2.imag) * temp,
                    (c1.imag * c2.real - c1.real * c2.imag) * temp);
}

bool operator==(Complex &c1, Complex &c2) {
    return (0 == c1.real - c2.real) && (0 == c1.imag - c2.imag);
}

int main(void) {
    Complex c1(2.3, 4.7), c2(-23.3, 99.2323);

    cout << "c1 + c2 = " ; (c1 + c2).print();
    cout << "c1 - c2 = " ; (c1 - c2).print();
    cout << "c1 * c2 = " ; (c1 * c2).print();
    cout << "c1 / c2 = " ; (c1 / c2).print();
    c1 == c2 ? (cout << "c1 == c2" ) : (cout << "c1 != c2") << endl;

    return 0;
}

抽象数据类型复数和有理数的代码实现_第1张图片

#include 
using namespace std;
#include 

class Rational {
    private:
    pair fraction;
    int gcd(int x, int y) {
    if (0 == x % y) {
        return y;
    }

    return gcd(y, x % y);
}

    public:
    Rational(int numerator, int denominator);
    pair get_rational(void);
    void set_numerator(int numerator);
    void set_denominator(int denominator);
    void print(void);
    friend Rational operator+(Rational &r1, Rational &r2);
    friend Rational operator-(Rational &r1, Rational &r2);
    friend Rational operator*(Rational &r1, Rational &r2);
    friend Rational operator/(Rational &r1, Rational &r2);
    friend bool operator==(Rational &r1, Rational &r2);
};

Rational::Rational(int numerator, int denominator) {
    if (0 != denominator) {
        fraction = pair(numerator, denominator);
    } else {
        fraction = pair(1, 1);
    }
}

pair Rational::get_rational(void) {
    return fraction;
}

void Rational::set_numerator(int numerator) {
    fraction.first = numerator;
}

void Rational::set_denominator(int denominator) {
    if (0 != denominator) {
        fraction.second = denominator;
    }
}

void Rational::print(void) {
    if (0 == 1- fraction.second) {
        cout << fraction.first << endl;
        return;
    }

    if (0 == fraction.first) {
        cout << '0' << endl;
        return;
    }

    // 将分数最简化
    int _gcd = gcd(fraction.first, fraction.second);
    int a = fraction.first / _gcd;
    int b = fraction.second / _gcd;

    if ((a ^ b) < 0) {
        a < 0 ? (cout << a << " / " << b << endl) 
                : (cout << -a << " / " << -b << endl) ;
        return;
    } else {
        a > 0 ? (cout << a << " / " << b << endl) 
                : (cout << -a << " / " << -b << endl);
    }
}

Rational operator+(Rational &r1, Rational &r2) {
    return Rational(r1.fraction.first * r2.fraction.second + 
                    r1.fraction.second * r2.fraction.first,
                    r1.fraction.second * r2.fraction.second);
}

Rational operator-(Rational &r1, Rational &r2) {
    return Rational(r1.fraction.first * r2.fraction.second - 
                    r1.fraction.second * r2.fraction.first,
                    r1.fraction.second * r2.fraction.second);
}

Rational operator*(Rational &r1, Rational &r2) {
    return Rational(r1.fraction.first * r2.fraction.first,
                    r1.fraction.second * r2.fraction.second);
}

Rational operator/(Rational &r1, Rational &r2) {
    if (0 == r2.fraction.first) {
        return Rational(0, 0);
    }

    return Rational(r1.fraction.first * r2.fraction.second,
                    r1.fraction.second * r2.fraction.first);
}

bool operator==(Rational &r1, Rational &r2) {
    return (0 == r1.fraction.first * r2.fraction.second - 
                    r1.fraction.second * r2.fraction.first);
}

int main(void) {
    Rational r1 = Rational(98, 63);
    Rational r2 = Rational(-7, 53);

    cout << "r1 = " ; r1.print();
    cout << "r2 = " ; r2.print();
    if (r1 == r2) { 
        cout << "r1 == r2" << endl;
    } else {
        cout << "r1 != r2" << endl;
    }
    cout << "r1 + r2 = " ; (r1 + r2).print();
    cout << "r1 - r2 = " ; (r1 - r2).print();
    cout << "r1 * r2 = " ; (r1 * r2).print();
    cout << "r1 / r2 = " ; (r1 / r2).print();

    return 0;
}

抽象数据类型复数和有理数的代码实现_第2张图片

你可能感兴趣的:(数据结构,C++)