【问题标题】:Class C+ wrong outputC+类错误输出
【发布时间】:2011-04-18 04:09:26
【问题描述】:

除了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&lt;&lt;"the new A is"&lt;&lt;A.Setreal(C)&lt;&lt;"+i"&lt;&lt;A.Setimag(D)&lt;&lt;endl; //WRONG OUTPUT,因为结果是1.#QNAN+i1.#QNAN,而不是像C+iD那样的C和D的值

【问题讨论】:

    标签: c++ class dev-c++


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      double Setreal(double re)
       { 
         real=re;
       };
      

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

      【讨论】:

        猜你喜欢
        • 2014-05-17
        • 1970-01-01
        • 2013-05-26
        • 2013-06-23
        • 2017-08-02
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多