啥也不说了直接上代码
小哥我的态度很好吧
//2、利用操作符重载给出一个完整的复数类的定义。
#include <iostream>
using namespace std;
class Complex{
double real;
double imag;
public:
Complex(){real = 0;imag = 0;}
Complex (double r,double i){real = r;imag = i;}
Complex operator +(Complex &c);
Complex operator -(Complex &c);
Complex operator *(Complex &c);
Complex operator /(Complex &c);
void show();
};
Complex Complex::operator+(Complex &c){
double r = real+c.real;
double i = imag+c.imag;
return Complex(r,i);
}
Complex Complex::operator-(Complex&c){
double r = real-c.real;
double i = imag-c.imag;
return Complex(r,i);
}
Complex Complex::operator*(Complex &c){
double r = real*c.real-imag*c.imag;
double i = imag*c.real+real*c.imag;
return Complex(r,i);
}
Complex Complex::operator/(Complex &c){
if (c.imag==0&&c.real==0)
{
cout<<"请输入正确的除数"<<endl;
return Complex(0,0);
}else{
double r = (real*c.real+imag*c.imag)/(c.real*c.real+c.imag*c.imag);
double i = (imag*c.real-real*c.imag)/(c.real*c.real+c.imag*c.imag);
return Complex(r,i);
}
}
void Complex::show(){
cout<<real<<"+"<<imag<<"i"<<endl;;
}
void main(){
Complex a(1,2),b(3,4),c;
c = a/b;
c.show();
}