Loading [MathJax]/extensions/TeX/AMSmath.js
본문 바로가기

컴퓨터/C++

complex class 구현

반응형

Complex Class


C++에서 제공하는 연산자 overloading을 이용해서 복소수를 클래스로 구현해보았다.

복소수 클래스의 멤버 변수들을 외부에서 변경하지 못하게 하기 위해서 private으로 선언하였으며, 멤버 함수는 사칙 연산 및 입출력에 대한 내용에 대해서만 구현을 하였다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef COMPLEX_H
#define COMPLEX_H
 
#include <iostream>
 
using namespace std;
 
class Complex{
private:
    double real;
    double imag;
public:
    Complex(double real = 0, double imag = 0) : real(real), imag(imag) {}
    friend Complex operator+(const Complex& c1, const Complex& c2);
    friend Complex operator-(const Complex& c1, const Complex& c2);
    friend Complex operator*(const Complex& c1, const Complex& c2);
    friend Complex operator/(const Complex& c1, const Complex& c2);
 
    friend bool operator==(const Complex& c1, const Complex& c2);
    friend bool operator!=(const Complex& c1, const Complex& c2);
 
    friend ostream& operator<< (ostream& out, const Complex& c);
    friend istream& operator>> (istream& in, Complex& c);
};
 
Complex operator+(const Complex& c1, const Complex& c2){
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
 
Complex operator-(const Complex& c1, const Complex& c2){
    return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
 
Complex operator*(const Complex& c1, const Complex& c2){
    return Complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real);
}
 
Complex operator/(const Complex& c1, const Complex& c2){
    double common = c2.real * c2.real + c2.imag * c2.imag;
    return Complex((c1.real * c2.real + c1.imag * c2.imag)/common, (c1.real * c2.imag - c1.imag * c2.real)/common);
}
 
bool operator==(const Complex& c1, const Complex& c2){
    return (c1.real == c2.real) && (c1.imag == c2.imag);
}
 
bool operator!=(const Complex& c1, const Complex& c2){
    return !(c1==c2);
}
 
ostream& operator<< (ostream& out, const Complex& c){
    out << c.real << " + " << c.imag << "i";
    return out;
}
 
istream& operator>> (istream& in, Complex& c){
    in >> c.real >> c.imag;
    return in;
}
 
#endif


만약 멤버 변수들을 public으로 선언하면 아래와 같이 코드를 수정할 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef COMPLEX_H
#define COMPLEX_H
 
#include <iostream>
 
using namespace std;
 
class Complex{
    friend ostream& operator<< (ostream& out, const Complex& c);
    friend istream& operator>> (istream& in, Complex& c);
public:
    Complex(double real = 0, double imag = 0) : real(real), imag(imag) {}
    Complex operator+(const Complex& c);
    Complex operator-(const Complex& c);
    Complex operator*(const Complex& c);
    Complex operator/(const Complex& c);
 
    bool operator==(const Complex& c);
    bool operator!=(const Complex& c);
 
    double real;
    double imag;
};
 
Complex Complex::operator+(const Complex& c){
    return Complex(this->real + c.real, this->imag + c.imag);
}
 
Complex Complex::operator-(const Complex& c){
    return Complex(this->real - c.real, this->imag - c.imag);
}
 
Complex Complex::operator*(const Complex& c){
    return Complex(this->real * c.real - this->imag * c.imag, this->real * c.imag + this->imag * c.real);
}
 
Complex Complex::operator/(const Complex& c){
    double common = c.real * c.real + c.imag * c.imag;
    return Complex((this->real * c.real + this->imag * c.imag)/common, (this->real * c.imag - this->imag * c.real)/common);
}
 
bool Complex::operator==(const Complex& c){
    return (this->real == c.real) && (this->imag == c.imag);
}
 
bool Complex::operator!=(const Complex& c){
    return !(*this==c);
}
 
ostream& operator<< (ostream& out, const Complex& c){
    out << c.real << " + " << c.imag << "i";
    return out;
}
 
istream& operator>> (istream& in, Complex& c){
    in >> c.real >> c.imag;
    return in;
}
 
#endif


이것을 complex.h라는 헤더파일로 만들어놓고 필요할 때 불러와서 사용해도 된다.

반응형

'컴퓨터 > C++' 카테고리의 다른 글

대소문자 변경하기  (0) 2016.04.18
Merge Sort 구현  (0) 2016.04.17
*없이 두 정수 곱하기  (0) 2016.04.13
Stack 기반 미로 찾기(DFS)  (0) 2015.10.27
Stack을 이용한 간단한 계산기  (0) 2015.10.17