求助:帮忙改一道C++题
题目要求:(a)修改类,使得可以通过重载>>和<<运算符来分别输入和输出复数(b重载相乘运算符,使得可以按照代数方法来乘两个复数。(c重载==和!=,以允许比较两个复数。...
题目要求:
(a)修改类,使得可以通过重载>>和<<运算符来分别输入和输出复数
(b 重载相乘运算符,使得可以按照代数方法来乘两个复数。
(c 重载= =和!=,以允许比较两个复数。
以下是本人编的 高手请帮忙改一下,运行没有错误再发上来,谢谢了!
#include<iostream>
using namespace std;
class Complex;
istream &operator>>(istream& , Complex& );
ostream &operator<<(ostream& , Complex& );
class Complex
{
private:
double real;
double imag;
public :
friend istream &operator>>(istream& i, Complex& c);
friend ostream &operator<<(ostream& i, Complex& c);
Complex(double r=0.0,double i=0.0){real=r;imag=i;}
Complex operator*(Complex C2);
friend bool operator==(Complex& M,Complex& N);
friend bool operator!=(Complex& M,Complex& N);
void display();
};
istream& operator>>(istream& i, Complex& c)
{
cout<<"input real:";
i>>c.real;
cout<<"input imag:";
i>>c.imag;
return i;
}
ostream& operator<<(ostream& o, Complex& c)
{
o<<"real: "<<c.real;
o<<" imag: "<<c.imag<<endl;
return o;
}
Complex Complex::operator*(Complex C2)
{
return Complex(real*C2.real-imag*C2.imag , real*C2.imag+C2.real*imag);
}
void Complex::operator==(Complex& M,Complex& N)
{
if (M.real=N.real,M.imag=N.imag)
cout<<1<<endl;
else
cout<<0<<endl;
}
void Complex::operator!=(Complex& M;Complex& N)
{
if (M.real!=N.real,M.imag!=N.imag)
cout<<1<<endl;
else
cout<<0<<endl;
}
//void Complex::display()
//{
// cout<<this->imag<<" ";
// cout<<this->real<<endl;
//}
int main()
{
Complex C1(5,4),C2(2,10),C3;
cout<<"C1="<<C1<<endl;//C1.display();
cout<<"C2="<<C2<<endl;//C2.display();
C3=C1*C2;
cout<<"C3=C1*C2="<<C3<<endl;
C1==C2;
cout<<"C1==C2 :"<<C1==C2<<endl;//C3.display();
C1!=C2;
cout<<"C1!=C2 :"<<C1!=C2<<endl;//C3.display();
return 0;
} 展开
(a)修改类,使得可以通过重载>>和<<运算符来分别输入和输出复数
(b 重载相乘运算符,使得可以按照代数方法来乘两个复数。
(c 重载= =和!=,以允许比较两个复数。
以下是本人编的 高手请帮忙改一下,运行没有错误再发上来,谢谢了!
#include<iostream>
using namespace std;
class Complex;
istream &operator>>(istream& , Complex& );
ostream &operator<<(ostream& , Complex& );
class Complex
{
private:
double real;
double imag;
public :
friend istream &operator>>(istream& i, Complex& c);
friend ostream &operator<<(ostream& i, Complex& c);
Complex(double r=0.0,double i=0.0){real=r;imag=i;}
Complex operator*(Complex C2);
friend bool operator==(Complex& M,Complex& N);
friend bool operator!=(Complex& M,Complex& N);
void display();
};
istream& operator>>(istream& i, Complex& c)
{
cout<<"input real:";
i>>c.real;
cout<<"input imag:";
i>>c.imag;
return i;
}
ostream& operator<<(ostream& o, Complex& c)
{
o<<"real: "<<c.real;
o<<" imag: "<<c.imag<<endl;
return o;
}
Complex Complex::operator*(Complex C2)
{
return Complex(real*C2.real-imag*C2.imag , real*C2.imag+C2.real*imag);
}
void Complex::operator==(Complex& M,Complex& N)
{
if (M.real=N.real,M.imag=N.imag)
cout<<1<<endl;
else
cout<<0<<endl;
}
void Complex::operator!=(Complex& M;Complex& N)
{
if (M.real!=N.real,M.imag!=N.imag)
cout<<1<<endl;
else
cout<<0<<endl;
}
//void Complex::display()
//{
// cout<<this->imag<<" ";
// cout<<this->real<<endl;
//}
int main()
{
Complex C1(5,4),C2(2,10),C3;
cout<<"C1="<<C1<<endl;//C1.display();
cout<<"C2="<<C2<<endl;//C2.display();
C3=C1*C2;
cout<<"C3=C1*C2="<<C3<<endl;
C1==C2;
cout<<"C1==C2 :"<<C1==C2<<endl;//C3.display();
C1!=C2;
cout<<"C1!=C2 :"<<C1!=C2<<endl;//C3.display();
return 0;
} 展开
2个回答
展开全部
#include<iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
class Complex;
istream &operator>>(istream& , Complex& );
ostream &operator<<(ostream& , Complex& );
bool operator!=(Complex& M,Complex& N); //应该是V6的问题,这里要声明下
bool operator==(Complex& M,Complex& N);
class Complex
{
private:
double real;
double imag;
public :
friend istream &operator>>(istream& i, Complex& c);
friend ostream &operator<<(ostream& i, Complex& c);
Complex(double r=0.0,double i=0.0){real=r;imag=i;}
Complex operator*(Complex C2);
friend bool operator==(Complex& M,Complex& N);
friend bool operator!=(Complex& M,Complex& N);
void display();
};
istream& operator>>(istream& i, Complex& c)
{
cout<<"input real:";
i>>c.real;
cout<<"input imag:";
i>>c.imag;
return i;
}
ostream& operator<<(ostream& o, Complex& c)
{
o<<"real: "<<c.real;
o<<" imag: "<<c.imag<<endl;
return o;
}
Complex Complex::operator*(Complex C2)
{
return Complex(real*C2.real-imag*C2.imag , real*C2.imag+C2.real*imag);
}
bool operator==(Complex& M,Complex& N) ////这里bool型的返回值,友元函数不用加类名的
{
return ( (M.real == N.real) && (M.imag == N.imag) );//返回的也是布尔值
}
bool operator!=(Complex& M,Complex& N) ////这里bool型的返回值,友元函数不用加类名的
{
return ( (M.real != N.real) && (M.imag != N.imag) );
}
//void Complex::display()
//{
// cout<<this->imag<<" ";
// cout<<this->real<<endl;
//}
int main()
{
Complex C1(5,4),C2(2,10),C3;
cout<<"C1="<<C1<<endl;//C1.display();
cout<<"C2="<<C2<<endl;//C2.display();
C3=C1*C2;
cout<<"C3=C1*C2="<<C3<<endl;
if(C1==C2) //这里只能用作判断了
cout<<"C1==C2"<<endl;//C3.display();
if(C1!=C2) //这里只能用作判断了
cout<<"C1!=C2"<<endl;//C3.display();
return 0;
}
#include <cstdlib>
#include <cctype>
using namespace std;
class Complex;
istream &operator>>(istream& , Complex& );
ostream &operator<<(ostream& , Complex& );
bool operator!=(Complex& M,Complex& N); //应该是V6的问题,这里要声明下
bool operator==(Complex& M,Complex& N);
class Complex
{
private:
double real;
double imag;
public :
friend istream &operator>>(istream& i, Complex& c);
friend ostream &operator<<(ostream& i, Complex& c);
Complex(double r=0.0,double i=0.0){real=r;imag=i;}
Complex operator*(Complex C2);
friend bool operator==(Complex& M,Complex& N);
friend bool operator!=(Complex& M,Complex& N);
void display();
};
istream& operator>>(istream& i, Complex& c)
{
cout<<"input real:";
i>>c.real;
cout<<"input imag:";
i>>c.imag;
return i;
}
ostream& operator<<(ostream& o, Complex& c)
{
o<<"real: "<<c.real;
o<<" imag: "<<c.imag<<endl;
return o;
}
Complex Complex::operator*(Complex C2)
{
return Complex(real*C2.real-imag*C2.imag , real*C2.imag+C2.real*imag);
}
bool operator==(Complex& M,Complex& N) ////这里bool型的返回值,友元函数不用加类名的
{
return ( (M.real == N.real) && (M.imag == N.imag) );//返回的也是布尔值
}
bool operator!=(Complex& M,Complex& N) ////这里bool型的返回值,友元函数不用加类名的
{
return ( (M.real != N.real) && (M.imag != N.imag) );
}
//void Complex::display()
//{
// cout<<this->imag<<" ";
// cout<<this->real<<endl;
//}
int main()
{
Complex C1(5,4),C2(2,10),C3;
cout<<"C1="<<C1<<endl;//C1.display();
cout<<"C2="<<C2<<endl;//C2.display();
C3=C1*C2;
cout<<"C3=C1*C2="<<C3<<endl;
if(C1==C2) //这里只能用作判断了
cout<<"C1==C2"<<endl;//C3.display();
if(C1!=C2) //这里只能用作判断了
cout<<"C1!=C2"<<endl;//C3.display();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2008-12-26
展开全部
#include<iostream>
using namespace std;
class Complex;
istream &operator>>(istream& , Complex& );
ostream &operator<<(ostream& , Complex& );
class Complex
{
private:
double real;
double imag;
public :
friend istream &operator>>(istream& i, Complex& c);
friend ostream &operator<<(ostream& i, Complex& c);
Complex(double r=0.0,double i=0.0){real=r;imag=i;}
Complex operator*(Complex C2);
friend bool operator==(Complex& M,Complex& N);
friend bool operator!=(Complex& M,Complex& N);
void display();
};
istream& operator>>(istream& i, Complex& c)
{
cout<<"input real:";
i>>c.real;
cout<<"input imag:";
i>>c.imag;
return i;
}
ostream& operator<<(ostream& o, Complex& c)
{
o<<"real: "<<c.real;
o<<" imag: "<<c.imag<<endl;
return o;
}
Complex Complex::operator*(Complex C2)
{
return Complex(real*C2.real-imag*C2.imag , real*C2.imag+C2.real*imag);
}
void Complex::operator==(Complex& M,Complex& N)
{
if (M.real=N.real,M.imag=N.imag)
cout<<1<<endl;
else
cout<<0<<endl;
}
void Complex::operator!=(Complex& M;Complex& N)
{
if (M.real!=N.real,M.imag!=N.imag)
cout<<1<<endl;
else
cout<<0<<endl;
}
//void Complex::display()
//{
// cout<<this->imag<<" ";
// cout<<this->real<<endl;
//}
int main()
{
Complex C1(5,4),C2(2,10),C3;
cout<<"C1="<<C1<<endl;//C1.display();
cout<<"C2="<<C2<<endl;//C2.display();
C3=C1*C2;
cout<<"C3=C1*C2="<<C3<<endl;
C1==C2;
cout<<"C1==C2 :"<<C1==C2<<endl;//C3.display();
C1!=C2;
cout<<"C1!=C2 :"<<C1!=C2<<endl;//C3.display();
return 0;
}
using namespace std;
class Complex;
istream &operator>>(istream& , Complex& );
ostream &operator<<(ostream& , Complex& );
class Complex
{
private:
double real;
double imag;
public :
friend istream &operator>>(istream& i, Complex& c);
friend ostream &operator<<(ostream& i, Complex& c);
Complex(double r=0.0,double i=0.0){real=r;imag=i;}
Complex operator*(Complex C2);
friend bool operator==(Complex& M,Complex& N);
friend bool operator!=(Complex& M,Complex& N);
void display();
};
istream& operator>>(istream& i, Complex& c)
{
cout<<"input real:";
i>>c.real;
cout<<"input imag:";
i>>c.imag;
return i;
}
ostream& operator<<(ostream& o, Complex& c)
{
o<<"real: "<<c.real;
o<<" imag: "<<c.imag<<endl;
return o;
}
Complex Complex::operator*(Complex C2)
{
return Complex(real*C2.real-imag*C2.imag , real*C2.imag+C2.real*imag);
}
void Complex::operator==(Complex& M,Complex& N)
{
if (M.real=N.real,M.imag=N.imag)
cout<<1<<endl;
else
cout<<0<<endl;
}
void Complex::operator!=(Complex& M;Complex& N)
{
if (M.real!=N.real,M.imag!=N.imag)
cout<<1<<endl;
else
cout<<0<<endl;
}
//void Complex::display()
//{
// cout<<this->imag<<" ";
// cout<<this->real<<endl;
//}
int main()
{
Complex C1(5,4),C2(2,10),C3;
cout<<"C1="<<C1<<endl;//C1.display();
cout<<"C2="<<C2<<endl;//C2.display();
C3=C1*C2;
cout<<"C3=C1*C2="<<C3<<endl;
C1==C2;
cout<<"C1==C2 :"<<C1==C2<<endl;//C3.display();
C1!=C2;
cout<<"C1!=C2 :"<<C1!=C2<<endl;//C3.display();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询