#c #class #dev-c
#c #класс #dev-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;}
Неправильный результат равен at, cout<<"the new A is"<<A.Setreal(C)<<" i"<<A.Setimag(D)<<endl; //WRONG OUTPUT
поскольку результат 1.#QNAN i1.#QNAN
вместо того, чтобы быть значением C и D подобным этому C iD
Ответ №1:
У этих методов должен быть оператор return:
double Setimag(double im)
{
return imag=im;
};
double Setreal(double re)
{
return real=re;
};
Ответ №2:
double Setreal(double re)
{
real=re;
};
Предполагается, что эта функция возвращает что-то типа double
, но это не так…