-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining5_1_2.cpp
More file actions
203 lines (188 loc) · 5.35 KB
/
Copy pathtraining5_1_2.cpp
File metadata and controls
203 lines (188 loc) · 5.35 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include<iostream>
#include<string.h>
#include<cstdlib>
class Complex
{
double real, img;
public:
Complex(double real, double img) : real(real), img(img) {}
Complex(const char *str);
Complex operator+(const Complex &c) const;
Complex operator+(const char *c) const;
Complex operator-(const Complex &c) const;
Complex operator-(const char *c) const;
Complex operator*(const Complex &c) const;
Complex operator*(const char *c) const;
Complex operator/(const Complex &c) const;
Complex operator/(const char *c) const;
Complex &operator=(const Complex &c);
Complex &operator+=(const Complex &c);
Complex &operator-=(const Complex &c);
Complex &operator*=(const Complex &c);
Complex &operator/=(const Complex &c);
void show()
{
if(img >0){
std::cout << real << "+" << img << "i" << std::endl;
}
else if(img < 0){
std::cout << real << img << "i" << std::endl;
}
else{
std::cout << real <<std::endl;
}
}
double get_number(const char *str, int from, int to) const;
};
Complex Complex::operator+(const Complex &c) const
{
Complex temp(real + c.real, img + c.img);
return temp;
}
Complex Complex::operator-(const Complex &c) const
{
Complex temp(real - c.real, img - c.img);
return temp;
}
Complex Complex::operator*(const Complex &c) const
{
Complex temp(real * c.real - img * c.img, real * c.img + img * c.real);
return temp;
}
Complex Complex::operator/(const Complex &c) const
{
Complex temp(
(real * c.real + img * c.img) / (c.real * c.real + c.img * c.img),
(img * c.real - real * c.img) / (c.real * c.real + c.img * c.img));
return temp;
}
Complex &Complex::operator=(const Complex &c)
{ // 디폴트 대입연산자가 자동생성되긴 함
real = c.real;
img = c.img;
return *this; // 동일 연산자 우선순위가 오른쪽부터 처리하기 때문에 자신을 반환해야 한다.
}
Complex &Complex::operator+=(const Complex &c)
{
(*this) = (*this) + c;
return *this;
}
Complex &Complex::operator-=(const Complex &c)
{
(*this) = (*this) - c;
return *this;
}
Complex &Complex::operator*=(const Complex &c)
{
(*this) = (*this) * c;
return *this;
}
Complex &Complex::operator/=(const Complex &c)
{
(*this) = (*this) / c;
return *this;
}
Complex::Complex(const char *str) //사실 이 생성자를 만들어주면 +-*/도 자동으로 가능해진다. 자체적으로 변환해서 사칙연산하기 때문
//단 연산자 오버로딩시에 확실히 인자를 const로 받아야한다.
{
// 입력 받은 문자열을 분석하여 real 부분과 img 부분을 찾아야 한다.
// 문자열의 꼴은 다음과 같습니다 "[부호](실수부)(부호)i(허수부)"
// 이 때 맨 앞의 부호는 생략 가능합니다. (생략시 + 라 가정)
int begin = 0, end = strlen(str);
img = 0.0;
real = 0.0;
// 먼저 가장 기준이 되는 'i' 의 위치를 찾는다.
int pos_i = -1;
for (int i = 0; i != end; i++)
{
if (str[i] == 'i')
{
pos_i = i;
break;
}
}
// 만일 'i' 가 없다면 이 수는 실수 뿐이다.
if (pos_i == -1)
{
real = get_number(str, begin, end - 1);
return;
}
// 만일 'i' 가 있다면, 실수부와 허수부를 나누어서 처리하면 된다.
real = get_number(str, begin, pos_i - 1);
img = get_number(str, pos_i + 1, end - 1);
if (pos_i >= 1 && str[pos_i - 1] == '-')
img *= -1.0;
}
Complex Complex::operator+(const char *str) const
{
Complex temp(str);
return (*this) + temp;
}
Complex Complex::operator-(const char *str) const
{
Complex temp(str);
return (*this) - temp;
}
Complex Complex::operator*(const char *str) const
{
Complex temp(str);
return (*this) * temp;
}
Complex Complex::operator/(const char *str) const
{
Complex temp(str);
return (*this) / temp;
}
double Complex::get_number(const char *str, int from, int to) const
{
bool minus = false;
if (from > to)
return 0;
if (str[from] == '-')
minus = true;
if (str[from] == '-' || str[from] == '+')
from++; // 부호 붙였을때 다음자리부터 읽음.
double num = 0.0;
num = atof(str+from);
std::cout << num << std::endl;
if(minus) num*=-1.0;
return num;
}
// }
// double Complex::get_number(const char *str, int from, int to) const
// {
// bool minus = false;
// if (from > to)
// return 0;
// if (str[from] == '-')
// minus = true;
// if (str[from] == '-' || str[from] == '+')
// from++; // 부호 붙였을때 다음자리부터 읽음.
// double num = 0.0;
// double decimal = 1.0;
// bool integer_part = true;
// for (int i = from; i <= to; i++)
// {
// if (isdigit(str[i]) && integer_part)
// {
// num *= 10.0;
// num += (str[i] - '0');
// }
// else if (str[i] == '.')
// integer_part = false;
// else if (isdigit(str[i]) && !integer_part)
// {
// decimal /= 10.0;
// num += ((str[i] - '0') * decimal);
// }
// else
// break;
// }
// if (minus) num *= -1.0;
// return num;
// }
int main()
{
Complex num("10-i20");
num.show();
}