#include <bits/stdc++.h>
using namespace std;
class Complex{
private:
double real;
double imag;
public:
Complex();
Complex(double real);
Complex(double real,double imag);
double getReal();
double getImag();
void setReal(double real);
void setImag(double imag);
Complex add(Complex other);
Complex sub(Complex other);
Complex multi(Complex other);
Complex divide(Complex other);
double abs();
string toString();
};
Complex::Complex(){
this->real=0;
this->imag=0;
}
Complex::Complex(double real){
this->real=real;
this->imag=0;
}
Complex::Complex(double real,double imag){
this->real=real;
this->imag=imag;
}
double Complex::getReal(){
return real;
}
double Complex::getImag(){
return imag;
}
void Complex::setReal(double real){
this->real=real;
}
void Complex::setImag(double imag){
this->imag=imag;
}
Complex Complex::add(Complex other){
return Complex(this->getReal() + other.getReal(),this->getImag() + other.getImag());
}
Complex Complex::sub(Complex other){
return Complex(this->getReal() - other.getReal(),this->getImag() - other.getImag());
}
Complex Complex::multi(Complex other){
double real;
double imag;
real=this->getReal()*other.getReal()-this->getImag()*other.getImag();
imag=this->getReal()*other.getImag()+this->getImag()*other.getReal();
return Complex(real,imag);
}
Complex Complex::divide(Complex other){
double real;
double imag;
real=(this->getReal()*other.getReal()+this->getImag()*other.getImag())/(pow(other.getImag(),2)+pow(other.getReal(),2));
imag=(this->getImag()*other.getReal()-this->getReal()*other.getImag())/(pow(other.getImag(),2)+pow(other.getReal(),2));
return Complex(real,imag);
}
double Complex::abs(){
return sqrt(pow(this->getImag(),2)+pow(this->getReal(),2));
}
string Complex::toString(){
string res,tmp;
stringstream stream;
stream<<this->getReal();
stream>>tmp;
res.append(tmp);
res.append("+i");
stream.clear();
stream<<this->getImag();
stream>>tmp;
res.append(tmp);
return res;
}
int main()
{
Complex a(2,3);
Complex b(4,-5);
Complex c;
c=a.add(b);
cout<<c.toString()<<endl;
c = a.sub(b);
cout<<c.toString()<<endl;
c = a.multi(b);
cout<<c.toString()<<endl;
c = a.divide(b);
cout<<c.toString()<<endl;
double res = a.abs();
cout<<res<<endl;
return 0;
}
nice
哪家公司的,写代码从来不写注释的吗?
相当棒!