#include <iostream>
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) {
抽象数据类型复数和有理数的代码实现
最新推荐文章于 2022-09-10 19:02:35 发布