C 类+错误的输出

发布于 2024-11-02 03:43:10 字数 1942 浏览 4 评论 0 原文

所有代码都工作正常,没有错误,但命令 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的值

All the code is working properly without errors but the command Setreal() and Setimag()
below give wrong output.


#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;}

The wrong result is at cout<<"the new A is"<<A.Setreal(C)<<"+i"<<A.Setimag(D)<<endl; //WRONG OUTPUT as the result is 1.#QNAN+i1.#QNAN instead of being value of C and D like that C+iD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有 2024-11-09 03:43:10

这些方法应该有一个返回语句:

double Setimag(double im)
{
  return imag=im;
};
double Setreal(double re)
{
  return real=re;
};

Those methods should have a return statement:

double Setimag(double im)
{
  return imag=im;
};
double Setreal(double re)
{
  return real=re;
};
等待我真够勒 2024-11-09 03:43:10
double Setreal(double re)
 { 
   real=re;
 };

这个函数应该返回一些 double 类型的东西,但它没有......

double Setreal(double re)
 { 
   real=re;
 };

This function is supposed to return something of type of double, but it doesn't...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文