2009-11-17 2 views
3

복소수를 공역으로 만드는 함수를 만들려고합니다. 예를 들어 A (2, 3)가 ~ A로 입력하면 A (2, -3)가됩니다. 약간의 코드를 입력했습니다. 아래에 나와 있겠지만 그것이 틀렸다는 것을 짐작할 수 있습니다. 나는 아래 코드에서 내가 잘못한 부분을 인용했다.복소수에 대한 공액 함수

#include <iostream> 
    using namespace std; 
    class Complex 
    { 
    private: 
     double real; 
     double imaginenary; 
    public: 
     Complex(); 
     Complex(double r, double i = 0); 

     // Declaration 
     Complex operator~(const Complex & c) const; 


     Complex operator+(const Complex & c) const; 
     Complex operator-(const Complex & c) const; 
     Complex operator*(const Complex & c) const; 
     Complex operator*(double n) const; 
     friend Complex operator*(double m, const Complex & c) 
       { return c * m; }  
     friend ostream & operator<<(ostream & os, const Complex & c); 
    }; 
    Complex::Complex() 
    { 
     real = imaginenary = 0; 
    } 

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



    // Definition 
    Complex Complex::operator~(const Complex & c) const 
    { 
     Complex conj; 
     conj.imaginenary = -1 * imaginenary; 
     conj.real = real; 
    } 


    Complex Complex::operator+(const Complex & c) const 
    { 
     Complex sum; 
     sum.imaginenary = imaginenary + c.imaginenary; 
     sum.real = real + c.real; 
     return sum; 
    } 

    Complex Complex::operator-(const Complex & c) const 
    { 
     Complex diff; 
     diff.imaginenary = imaginenary - c.imaginenary; 
     diff.real = real - c.real; 
     return diff; 
    } 
    Complex Complex::operator*(const Complex & c) const 
    { 
     Complex mult; 
     mult.imaginenary = imaginenary * c.imaginenary; 
     mult.real = real * c.real; 
     return mult; 
    } 

    Complex Complex::operator*(double mult) const 
    { 
     Complex result; 
     result.real = real * mult; 
     result.imaginenary = imaginenary * mult; 
     return result; 
    } 
    ostream & operator<<(ostream & os, const Complex & c) 
    { 
     os << "(" << c.real <<"," << c.imaginenary<<"i)"; 
     return os; 
    } 
    int main() 
    { 
     Complex A; 
     Complex B(5, 40); 
     Complex C(2, 55); 
     cout << "A, B, and C:\n"; 
     cout << A <<"; " << B << ": " << C << endl; 
     cout << " complex conjugate is" << ~C << endl; 
     cout << "B + C: " << B+C << endl; 
     cout << "B * C: " << B*C << endl; 
     cout << "10 * B: " << 10*B << endl; 
     cout << "B - C: " << B - C << endl; 
     return 0; 
    } 
+2

당신의 질문은 아니지만 'operator * (const Complex & c)'를 잘못 구현 한 것으로 나타났습니다. (a + ib) * (c + id) = (ac-bd) + i (ad + bc)이다. –

+0

imaginenary - 끔찍한 철자 !!! – Programmer

답변

3

Complex Complex::operator~() const  
{ 
    Complex conj; 
    conj.imaginenary = -1 * imaginenary; 
    conj.real = real; 
    return conj; 
} 

을 시도하지만 클래스 정의에서 운영자를 제거하고 대신에 (친구) 함수를 만들 현명 수 있습니다. 그것은 묵시적 ​​타입 변환으로 더 잘 작동합니다.

+0

친구 복잡한 연산자 * (double m, const Complex & c) {return c * m; } 친구 ostream & 연산자 << (ostream & os, const complex & c); 친구에게이 문제를 사용하지 않으려면 어떻게해야합니까? 책에서 읽었으나 이해하지 못하기 때문에 – Toila

+0

문제가 표시되지 않습니다. 여기에 있지만 생성자를 사용하면 double을 사용하여 double에서 Complex로 암시 적으로 변환 할 수 있습니다. 연산자 *가 멤버가 아닌 경우 컴파일러에서 double에 복소수 곱하기 및 다른 반올림 코드를 생성 할 수 있습니다. 연산자 *가 멤버에서 double을 곱하면됩니다. double에서 double을 곱하면됩니다. double에서 complex로 전환하는 연산자가 없으므로 –

9

물결표 (~) 연산자는 (은 *this에서 작동) 매개 변수를 허용해서는 안 단항 연산자입니다. operator~에서 값을 반환하는 것을 잊었습니다. 그것은 가상을 철자 :

Complex operator~() const 
{ 
    return Complex(real, -1 * imaginenary); 
} 

는 고정 코드 BTW here

참조하십시오.

+0

+1 [:-)] 나는 같은 것을 붙일 예정이었다 !!! –

1
Complex Complex::operator~(const Complex & c) const 
{ 
    Complex conj; 
    conj.imaginenary = -1 * c.imaginenary; 
    conj.real = c.real; 
    return conj; 
}

이 작업을 수행해야합니다.

메모리에있는 해당 영역을 언제든지 덮어 쓸 수 있기 때문에 비전 역 범위 내에서 할당 한 항목을 반환하는 것은 좋지 않습니다. btw 그것은 상상의 것이지 imaginenary가 아닙니다.

+1

"비 역외 범위 내에서 할당 한 모든 것을 반환하는 것이 최선의 방법은 아닙니다"- 여기서는 해당되지 않습니다. '반환'은 파괴되기 전에 그것을 복사합니다. 실제로 반환 된 지역 변수 'conj'는 일반적으로 호출자가 설정 한 반환 값 슬롯에 할당되므로 복사가 필요하지 않습니다. – greggo

3

많은 코드가 있지만, operator~과 예를 들어 비교했을 것입니다. operator+, 당신은 하나의 세부 사항을 발견했을 것입니다. return 문은 없습니다.

0
friend Complex operator*(double m, const Complex & c) { return c * m; } 
friend ostream & operator<<(ostream & os, const Complex & c); 

이 문제에 친구를 사용하지 않으려면 어떻게해야합니까? 책에서 읽었지만 이해가 안가서.

+2

이있는 경우에는 friend가 필요하지 않습니다. 컴플렉스 멤버에 대한 전체 (읽기) 액세스. 숨겨진 내부 액세스가 필요한 경우에만 친구가 필요합니다. –

관련 문제