2013-12-07 3 views
0

여기에 몇 가지 문제가 있습니다. main 함수에서 result2와 result3에 대한 계산을 작성하는 방법을 알아낼 수 없습니다. 다음 그들은 :클래스를 사용하는 함수에 값을 전달하는 중

result2 계산 (Z2 + Z3)/(Z3 + Z2)

result3 테스트를 볼 경우 (Z1)의 X (Z2 + Z3) 및 (Z1 X Z2) + (Z1 X Z3) 같다.

result2는 항상 1이어야하고 result3은 항상 같아야한다고 가정하고 있지만 어떻게 작성해야하는지 이해하지 못합니다. 어떤 아이디어? 여기에 지금까지이 작업은 다음과 같습니다. (이 첫 번째 질문입니다)

class Complex 
{ 
private: 
    double real; 
    double imag; 

public: 
    Complex() 
    { 
     real = 0.0; 
     imag = 0.0; 
    } 

    Complex(double r, double i) 
    { 
     real = r; 
     imag = i; 
    } 

    Complex add(const Complex& z) const 
    { 
     Complex sum; 
     sum.real = (real + z.real); 
     sum.imag = (imag + z.imag); 

     return sum; 
    } 

    Complex conjugate() const 
    { 
     Complex conj; 

     conj.real = real; 
     conj.imag = imag * -1; 

     return conj; 
    } 

    bool equals(const Complex& z) const 
    { 
     if (real == z.real && imag == z.imag) 
      return true; 
     else 
      return false; 
    } 

    Complex inverse() const 
    { 
     Complex inv; 

     inv.real = (real/(pow(real, 2) + pow(imag, 2))); 
     inv.imag = -1 * (imag/(pow(real, 2) + pow(imag, 2))); 

     return inv; 
    } 

    double modulus() const 
    { 
     double mod; 

     mod = sqrt(pow(real, 2) + pow(imag, 2)); 

     return mod; 
    } 

    Complex multiply(const Complex& z) const 
    { 
     Complex prod; 
     prod.real = (real * z.real) - (imag * z.imag); 
     prod.imag = (real * z.imag) + (imag * z.real); 

     return prod; 
    } 

    Complex subtract(const Complex& z) const 
    { 
     Complex diff; 
     diff.real = real - z.real; 
     diff.imag = imag - z.imag; 

     return diff; 
    } 

    string str() const 
    { 
     stringstream sout; 
     sout<<fixed<<setprecision(6); 
     if (real == 0 && imag == 0) 
      sout<<"0"; 
     else if (imag == 0) 
      sout<<real; 
     else if (real == 0 && imag == 1) 
      sout<<"i"; 
     else if (real == 0 && imag == -1) 
      sout<<"-i"; 
     else if (imag == 1) 
      sout<<real<<"+i"; 
     else if (imag == -1) 
      sout<<real<<"-i"; 
     else if (real == 0) 
      sout<<real<<"i"; 
     else if (imag > 0) 
      sout<<real<<"+"<<imag<<"i"; 
     else if (imag < 0) 
      sout<<real<<imag<<"i"; 

     return sout.str(); 
    } 
}; 

int main() 
{ 
double r1, r2, r3, c1, c2, c3; 

cout<<endl; 
cout<<"Enter the real and imaginary part of the first complex number-> "; 
cin>>r1>>c1; 
cout<<"Enter the real and imaginary part of the second complex number-> "; 
cin>>r2>>c2; 
cout<<"Enter the real and imaginary part of the third complex number-> "; 
cin>>r3>>c3; 

Complex z1(r1, c1); 
Complex z2(r2, c2); 
Complex z3(r3, c3); 

cout<<endl; 
cout<<"z1 = "<<z1.str()<<endl; 
cout<<"z2 = "<<z2.str()<<endl; 
cout<<"z3 = "<<z3.str()<<endl; 
cout<<endl; 

Complex result1 = z2.subtract(z3).multiply(z1); 
Complex result2 = (z2.add(z3)); //not sure what goes next 
Complex result3 = 

cout<<"(z2 - z3) x z1 = "<<result1.str()<<endl; 
cout<<"(z2 + z3)/(z3 + z2) = "<<result2.str()<<endl; 
cout<<"z1 x (z2 + z3) and (z1 x z2) + (z1 x z3) "<<result3.str()<<endl; 

return 0; 
} 

답변

0

편집 : 실수했습니다. 가로줄 뒤의 코드는 (z2 + z3)/(z3 + z2)이 아니라 z2/z3을 계산합니다. Wikipedia에서 공식 빼서

Complex z2(4, 4); 
Complex z3(2, 2); 

Complex numerator = z2.add(z3); 
Complex denominator = z3.add(z2); 
Complex conjugate = denominator.conjugate(); 
numerator = numerator.multiply(conjugate); 
denominator = denominator.multiply(conjugate); 
Complex result = numerator.multiply(denominator.inverse()); 

cout<<"(z2 + z3)/(z3 + z2) = "<< result.str(); // 1 

: 다음은 올바른 결과를 가져옵니다.

Complex z2(4, 4); 
Complex z3(2, 2); 

// z2.real = A 
// z2.imag = B 
// z3.real = C 
// z3.imag = D 
// Formula is: 
// a*c + b*d + b*c - a*d 
// ----------------------- 
// c^2 + d^2  c^2 + d^2 
double first = ((z2.real * z3.real) + (z2.imag * z3.imag)) 
       /(z3.real * z3.real + z3.imag * z3.imag); 
double second = ((z2.imag * z3.real) - (z2.real * z3.imag)) 
       /(z3.real * z3.real + z3.imag * z3.imag); 
Complex result(first, second); 

cout<<"z2/z3 = "<<result.str()<<endl; 
// Result: 2 
2
Complex result2 = ((z2.add(z3)).multiply((z3.add(z2)).inverse()); 

나는

내가 잘 가지고 희망하지만이 예는 실제로 이야기에 대한 을 완벽하게 소개 될 것이다 연산자 오버로드. 위와 같은 끔찍한 읽을 수없는 표현은 정확하게 +, * 등을 오버로드하여 피하기 원하는 것입니다.

어쩌면 이것은 학교에서해야 할 과제이며, 지금 당장해야 할 일이지만, 연산자 오버로딩에 대해 조금 읽었습니다. :)

관련 문제