#include <iostream.h>
#include <cmath>
Class Complex
{public:
Friend IStream & operator >> (IStream &, Complex &); The extract operator overloads the friend function declaration.
Friend Ostream & operator << (ostream &,complex &); The insert operator overloads the Friend function declaration.
Complex operator + (complex &); The operator "+" overloads the function declaration.
Complex operator-(complex &); Operator "-" overloaded function declaration.
Complex operator * (complex &); The operator "*" overloads the function declaration.
Complex operator/(complex &); Operator "/" overloaded function declaration.
void Bijiao (complex &,complex &); The comparison function Bijiao declaration.
Private
Double A; Defines a double private data member a variable.
Double b; Defines a double private data member a variable.
};
Complex Complex::operator + (complex &t)//out-of-class definition operator "+" overloaded functions.
{//Complex number addition implementation process.
T.A=A+T.A;
t.b=b+t.b;
return t;
}
Complex Complex::operator-(complex &t)//out-of-class definition operator "-" overloaded functions.
The {//plural subtraction implementation process.
T.A=A-T.A;
t.b=b-t.b;
return t;
}
Complex Complex::operator * (complex &t)//out-of-class definition operator "*" overloaded functions.
The {//plural multiplication implementation process.
Double S;
S=a*t.b+t.a*b;
t.a=a*t.a-b*t.b;
T.b=s;
return t;
}
Complex Complex::operator/(complex &t)//out-of-class definition operator "/" overloaded function.
{//Plural division implementation process.
Double s,v;
V=-A*T.B+B*T.A;
s=t.a*t.a+t.b*t.b;
T.a= (a*t.a+b*t.b)/s;
T.b=v;
return t;
}
void Complex::bijiao (complex &z1,complex &z2)//out-of-class definition Bijiao function.
{//complex number comparison implementation process.
Double d1,d2;
D1=pow (z1.a*z1.a+z1.b*z1.b,0.5);
D2=pow (z2.a*z2.a+z2.b*z2.b,0.5);
if (D1>D2) cout<< "|z1|>|z2|" <<endl;
if (D1<D2) cout<< "|z1|<|z2|" <<endl;
if (D1==D2) cout<< "|z1|=|z2|" <<endl;
}
IStream & operator >> (IStream &input, complex &t)//Define Extract operator overload function.
{//Input object implementation process.
input>>t.a>>t.b;
return input;
}
Ostream & operator << (ostream &output, complex &t)
{//Output object implementation process.
if (t.b<0) output<<t.a<<t.b<< "I" <<endl;
if (t.b>=0) output<<t.a<< "+" <<t.b<< "I" <<endl;
return output;
}
int main ()//defines the main function.
{
Complex z1,z2,z3; Defines the Z1,z2,z3 object for the complex class.
Char str;
cout<< "Calculates the arithmetic and comparative operations of two complex numbers: Z1=a+bi,z2=c+di;" <<endl;
cout<< "Please enter: (a) _ (b) _ (operator) _ (c) _ (d) write the numbers in the;(). "<<endl;
The cout<< "operator" includes "+,-, *,/,?", "_" as a space. "<<endl;
while (CIN>>Z1>>STR>>Z2)
{
if (str== ' + ') {z3=z1+z2;cout<< "z1+z2=" <<Z3;}
if (str== '-') {z3=z1-z2;cout<< "z1-z2=" <<Z3;}
if (str== ' * ') {z3=z1*z2;cout<< "z1*z2=" <<Z3;}
if (str== '/') {z3=z1/z2;cout<< "z1/z2=" <<Z3;}
if (str== '? ') Z3.bijiao (Z1,Z2);
}
return 0;
}
Complex four and comparison operations