怎么实现复数矩阵的加减乘除,共轭转置,利用函数重载
#include <iostream>
#include<cstdio>
using namespace std;
class Complex
{
public:
Complex(double r = 0,double i = 0)//构造函数
{
real=r;
imag=i;
}
~Complex()
{
}
friend Complex operator+(Complex &c1,Complex &c2); //重载为友员函数
friend Complex operator*(Complex &c1,Complex &c2);
Complex operator -(Complex&);//重载为成员函数
Complex operator /(Complex&);
friend istream& operator>>(istream&, Complex&);
friend ostream& operator<<(ostream&, Complex&);
friend bool operator==(Complex &c1,Complex &c2);
friend bool operator!=(Complex &c1,Complex &c2);
void display( );
private:
double real;
double 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);
}
Complex Complex::operator-(Complex &c)
{
return Complex(real-c.real,imag-c.imag);
}
Complex Complex::operator/(Complex &c)
{
return Complex(real/c.real,imag/c.imag);
}
istream& operator>>( istream& in, Complex &c )
{
in >> c.real >> c.imag;
return in;
}
ostream& operator<<( ostream& out, Complex &c )
{
out << c.real << "+" << c.imag << "i\n";
return out;
}
bool operator == (Complex &c1,Complex &c2)
{
if(c1.real==c2.real&&c1.imag==c2.imag)
{
return true;
}
else
{
return false;
}
}
bool operator != (Complex &c1,Complex &c2)
{
if(c1.real!=c2.real||c1.imag!=c2.imag)
{
return true;
}
else
{
return false;
}
}
void Complex::display( )
{
cout<<real<< "+" <<imag<<"i\n"<<endl;
}
int Menu()
{
int t;
cout << endl;
cout<<"=================="<<endl;
cout<<"1.输入复数"<<endl;
cout<<"2.查看输入的复数"<<endl;
cout<<"3.复数相加"<<endl;
cout<<"4.复数相减"<<endl;
cout<<"5.复数相乘"<<endl;
cout<<"6.复数相除"<<endl;
cout<<"7.输出结果"<<endl;
cout<<"0.退出"<<endl;
cout<<"=================="<<endl;
cout<<"请选择(0-7):";
cin>>t;
return t;
}
int main()
{
int iChoice =1;
Complex c1,c2,c3,c4;
while (iChoice!=0)
{
iChoice = Menu();
switch (iChoice)
{
case 1:
{
cout<<"请输入一个复数:"<<endl;
cin>>c1;
getchar();
break;
}
case 2:
{
//c1.display();
cout<<c1;
break;
}
case 3:
{
cout<<"原有的复数:"<<endl;
cout<<c1;
cout<<"请再输入一个复数相加:"<<endl;
cin>>c2;
getchar();
c3=c1+c2;
break;
}
case 4:
{
cout<<"原有的复数:"<<endl;
cout<<c1;
cout<<"请再输入一个复数相减:"<<endl;
cin>>c2;
getchar();
c3=c1-c2;
break;
}
case 5:
{
cout<<"原有的复数:"<<endl;
cout<<c1;
cout<<"请再输入一个复数相乘:"<<endl;
cin>>c2;
getchar();
c3=c1*c2;
break;
}
case 6:
{
cout<<"原有的复数:"<<endl;
cout<<c1;
cout<<"请再输入一个复数相除:"<<endl;
cin>>c2;
getchar();
c3=c1/c2;
break;
}
case 7:
{
cout<<"运算的结果:"<<endl;
cout<<c3;
break;
}
case 0:
{
break;
}
}
}
return 0;
}