C 类+错误的输出
所有代码都工作正常,没有错误,但命令 Setreal()
和 Setimag()
下面给出错误的输出。
#include <iostream>
using namespace std;
class complex
{ public:
bool Readcomplex()
{ cout<<"Enter the real part"<<endl;
cin>>real;
cout<<"Enter the imaginary part"<<endl;
cin>>imag;
return true; };
double Getreal()
{ return real;
};
double Getimag()
{ return imag;
};
double Add(complex c)
{ real=real+c.real;
imag=imag+c.imag;
};
double Setimag(double im)
{ imag=im;
};
double Setreal(double re)
{ real=re;
};
void Multiply(complex c)
{ double x;
x=real*c.real-imag*c.imag;
imag=real*c.imag+imag*c.real;
real=x;
};
private:
double real;
double imag;
};
int main()
{ complex A,B,E,R;
double C,D;
A.Readcomplex();
B.Readcomplex();
cout<<"The complex no. A is "<<A.Getreal()<<"+i"<<A.Getimag()<<endl;
cout<<"The complex no. B is "<<B.Getreal()<<"+i"<<B.Getimag()<<endl;
A.Add(B); //Result stored in A.
cout<<"The complex no. A2 is "<<A.Getreal()<<"+i"<<A.Getimag()<<endl;
cout<<"Set the real of A"<<endl;
cin>>C;
cout<<"and set the imaginary part"<<endl;
cin>>D;
cout<<"the new A is"<<A.Setreal(C)<<"+i"<<A.Setimag(D)<<endl; //WRONG OUTPUT
A.Multiply(B);
cout<<"The complex no. A is "<<A.Getreal()<<"+i"<<A.Getimag()<<endl;
system("pause");
return 0;}
错误的结果是cout<<“新的A是”<
1.#QNAN+i1.#QNAN
,而不是像C+iD
那样的C和D的值
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些方法应该有一个返回语句:
Those methods should have a return statement:
这个函数应该返回一些
double
类型的东西,但它没有......This function is supposed to return something of type of
double
, but it doesn't...