原题是:复数由两个部分组成:实属部分和叙述部分。复数的一种书写方式是:(3.0,4.0),其中,3.0是实数部分,4.0是虚数部分。假设a=(A,Bi),c=(C,Di),则下面是一些复数运算:
加法:a+c=(A+C,(B=D)i).
减法:a-c=(A-C,(B-D)I).
乘法:a*c=(A*C-B*D,(A*D+B*C)i).
乘法:x*c=(x*C,x*Di),其中x为实数.
共轭:~a=(A,-Bi).
请定义一个复数类,一片下面的程序可以使用它来获得正确结果。
#include<iostream>
using namespace std;
int main()
{
complex a (3.0,4.0);
complex c;
cout<<"Enter a complex number (q to quit): \n";
while(cin>>c)
{
cout<<"c is "<<c<<endl;
cout<<"complex conjugate is "<<~c<<endl;
cout<<"a is "<<a<<endl;
cout<<"a + c is "<<a+c<<endl;
cout<<"a - c is "<<a-c<<endl;
cout<<"a * c is "<<a*c<<endl;
cout<<"2 * c is "<<2*c<<endl;
cout<<"Enter a complex number (q to quit): "<<endl;
}
cout<<"Done! "<<endl;
return 0;
}
下面是我写的程序,可有个错误,网上我查了下,我的输入操作符重载好像没有错误。但就是编译不了,难道是C++版本问题?我的是VC++6.0,求助各位大大帮我看看,谢谢!!
#include<iostream>
class complex
{
double x;
double y;
public:
complex();
complex(double a,double b);
complex operator +(const complex & c)const;
complex operator -(const complex & c)const;
complex operator *(const complex & c)const;
complex operator *(double n);
friend complex operator *(double n,const complex & c);
complex operator ~();
friend std::ostream & operator <<(std::ostream & os,const complex & c);
friend std::istream & operator >>(std::istream & is,const complex & c);
};
complex::complex()
{
x=0.0;
y=0.0;
}
complex::complex(double a,double b)
{
x=a;
y=b;
}
complex complex::operator +(const complex & c)const
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}
complex complex::operator -(const complex & c)const
{
complex temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}
complex complex::operator *(const complex & c)const
{
complex temp;
temp.x=x*c.x-y*c.y;
temp.y=x*c.y+y*c.x;
return temp;
}
complex complex::operator *(double n)
{
complex temp;
temp.x=n*x;
temp.y=n*y;
return temp;
}
complex operator *(double n,const complex & c)
{
return n*c;
}
complex complex::operator ~()
{
complex temp;
temp.x=x;
temp.y=-y;
return temp;
}
std::ostream & operator <<(std::ostream & os,const complex & c)
{
os<<c.x<<"+"<<c.y<<"i";
return os;
}
std::istream & operator >>(std::istream & is,const complex & c)//VC++ 6.0无法通过编译
{
is>>c.x>>c.y;
return is;
}
int main()
{
using namespace std;
complex a (3.0,4.0);
complex c;
cout<<"Enter a complex number (q to quit): \n";
while(cin>>c)
{
cout<<"c is "<<c<<endl;
cout<<"complex conjugate is "<<~c<<endl;
cout<<"a is "<<a<<endl;
cout<<"a + c is "<<a+c<<endl;
cout<<"a - c is "<<a-c<<endl;
cout<<"a * c is "<<a*c<<endl;
cout<<"2 * c is "<<2*c<<endl;
cout<<"Enter a complex number (q to quit): "<<endl;
}
cout<<"Done! "<<endl;
return 0;
}
这是提示信息
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
C:\Users\Hacker\Desktop\Program\Cpp1.cpp(84) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'const double' (or there is no acceptable conversion)
Error executing cl.exe.
Cpp1.obj - 1 error(s), 0 warning(s)
6 个解决方案
#1
看不懂,帮顶
#2
const不能写入值
#3
const complex 用怎么可以用来输入
#4
std::istream & operator >>(std::istream & is,const complex & c)去掉const
常量不能被更改
常量不能被更改
#5
两种解决办法 。①帮你的类的数据成员声明为mutable
②去掉你的友元函数的const
②去掉你的友元函数的const
#include<iostream>
class complex
{
double x;//mutable double x;
double y;//mutable double y这样可以继续你的const
public:
complex();
complex(double a,double b);
complex operator +(const complex & c)const;
complex operator -(const complex & c)const;
complex operator *(const complex & c)const;
complex operator *(double n);
friend complex operator *(double n,const complex & c);
complex operator ~();
//不能定义成const。 const成员函数是不能修改类的数据成员的,除非你帮数据成员声明为mutable
friend std::ostream & operator <<(std::ostream & os, complex & c);//
friend std::istream & operator >>(std::istream & is, complex & c);
};
complex::complex()
{
x=0.0;
y=0.0;
}
complex::complex(double a,double b)
{
x=a;
y=b;
}
complex complex::operator +(const complex & c)const
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}
complex complex::operator -(const complex & c)const
{
complex temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}
complex complex::operator *(const complex & c)const
{
complex temp;
temp.x=x*c.x-y*c.y;
temp.y=x*c.y+y*c.x;
return temp;
}
complex complex::operator *(double n)
{
complex temp;
temp.x=n*x;
temp.y=n*y;
return temp;
}
complex operator *(double n,const complex & c)
{
return n*c;
}
complex complex::operator ~()
{
complex temp;
temp.x=x;
temp.y=-y;
return temp;
}
std::ostream & operator <<(std::ostream & os, complex & c)
{
os<<c.x<<"+"<<c.y<<"i";
return os;
}
std::istream & operator >>(std::istream & is,complex & c)//VC++ 6.0无法通过编译
{
is>>c.x>>c.y;
return is;
}
int main()
{
using namespace std;
complex a (3.0,4.0);
complex c;
cout<<"Enter a complex number (q to quit): \n";
while(cin>>c)
{
cout<<"c is "<<c<<endl;
cout<<"complex conjugate is "<<~c<<endl;
cout<<"a is "<<a<<endl;
cout<<"a + c is "<<a+c<<endl;
cout<<"a - c is "<<a-c<<endl;
cout<<"a * c is "<<a*c<<endl;
cout<<"2 * c is "<<2*c<<endl;
cout<<"Enter a complex number (q to quit): "<<endl;
}
cout<<"Done! "<<endl;
return 0;
}
#6
哦 原来如此 一直听别人说const难用 就按葫芦画瓢照着书上学了点 果然错了 感谢楼上各位大大
#1
看不懂,帮顶
#2
const不能写入值
#3
const complex 用怎么可以用来输入
#4
std::istream & operator >>(std::istream & is,const complex & c)去掉const
常量不能被更改
常量不能被更改
#5
两种解决办法 。①帮你的类的数据成员声明为mutable
②去掉你的友元函数的const
②去掉你的友元函数的const
#include<iostream>
class complex
{
double x;//mutable double x;
double y;//mutable double y这样可以继续你的const
public:
complex();
complex(double a,double b);
complex operator +(const complex & c)const;
complex operator -(const complex & c)const;
complex operator *(const complex & c)const;
complex operator *(double n);
friend complex operator *(double n,const complex & c);
complex operator ~();
//不能定义成const。 const成员函数是不能修改类的数据成员的,除非你帮数据成员声明为mutable
friend std::ostream & operator <<(std::ostream & os, complex & c);//
friend std::istream & operator >>(std::istream & is, complex & c);
};
complex::complex()
{
x=0.0;
y=0.0;
}
complex::complex(double a,double b)
{
x=a;
y=b;
}
complex complex::operator +(const complex & c)const
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}
complex complex::operator -(const complex & c)const
{
complex temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}
complex complex::operator *(const complex & c)const
{
complex temp;
temp.x=x*c.x-y*c.y;
temp.y=x*c.y+y*c.x;
return temp;
}
complex complex::operator *(double n)
{
complex temp;
temp.x=n*x;
temp.y=n*y;
return temp;
}
complex operator *(double n,const complex & c)
{
return n*c;
}
complex complex::operator ~()
{
complex temp;
temp.x=x;
temp.y=-y;
return temp;
}
std::ostream & operator <<(std::ostream & os, complex & c)
{
os<<c.x<<"+"<<c.y<<"i";
return os;
}
std::istream & operator >>(std::istream & is,complex & c)//VC++ 6.0无法通过编译
{
is>>c.x>>c.y;
return is;
}
int main()
{
using namespace std;
complex a (3.0,4.0);
complex c;
cout<<"Enter a complex number (q to quit): \n";
while(cin>>c)
{
cout<<"c is "<<c<<endl;
cout<<"complex conjugate is "<<~c<<endl;
cout<<"a is "<<a<<endl;
cout<<"a + c is "<<a+c<<endl;
cout<<"a - c is "<<a-c<<endl;
cout<<"a * c is "<<a*c<<endl;
cout<<"2 * c is "<<2*c<<endl;
cout<<"Enter a complex number (q to quit): "<<endl;
}
cout<<"Done! "<<endl;
return 0;
}
#6
哦 原来如此 一直听别人说const难用 就按葫芦画瓢照着书上学了点 果然错了 感谢楼上各位大大