[Absolute C++]Ch10-2

  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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <iostream>
using namespace std;

class polynomial {
public:
 //디폴트 생성자
 polynomial();
 // 복사 생성자
 polynomial(const polynomial &copy);
 //다항식 생성
 polynomial(int len, const double * coeff);
 //소멸자
 ~polynomial();

 /* 연산자 오버로딩 +*/
 friend const polynomial operator +(const polynomial& p1, const polynomial& p2);
 friend const polynomial operator +(const polynomial& p1, double c);
 friend const polynomial operator +(double c, const polynomial& p1);

 /*연산자 오버로딩 -*/
 friend const polynomial operator -(const polynomial& p1, const polynomial& p2);
 friend const polynomial operator -(const polynomial& p1, double c);
 friend const polynomial operator -(double c, const polynomial& p1);

 /*연산자 오버로딩 */
 friend const polynomial operator *(const polynomial& p1, const polynomial& p2);
 friend const polynomial operator *(const polynomial& p1, double c);
 friend const polynomial operator *(double c, const polynomial& p1);

 //할당 연산자
 polynomial& operator =(const polynomial&);
 //-연산자
 const polynomial operator -() const;
 //출력 연산자
 friend ostream& operator <<(ostream& os, const polynomial& p1);
 //숫자 대입
 friend const double evaluate(const polynomial& p1, double value);
private:
 double *coef; //계수 배열
 int len; //항의 개수
 int degree; //최고차수, 지수는 배열의 인덱스
};

int main() {
 
 double c1[] = { 6,5,-2,7 };
 double c2[] = { 7,3,-2 };

 polynomial p1(4, c1); //객체생성
 polynomial p2(3, c2);

 

 cout << "첫 번째 다항식 : " << p1 << endl;
 cout << "두 번째 다항식 : " << p2 << endl << endl;

 cout << "첫 번째 다항식 + 8 : " << p1 + 8 << endl;
 cout << "첫 번째 다항식 - 6 : " << p1 - 6 << endl;
 cout << "2 * 두 번째 다항식 : " << p2 * 2 << endl << endl;

 cout << "첫 번째 다항식 + 두 번째 다항식 : " << p1 + p2 << endl;
 cout << "첫 번째 다항식 - 두 번째 다항식 : " << p1 - p2 << endl;
 cout << "첫 번째 다항식 * 두 번째 다항식 : " << p1 * p2 << endl;

 cout <<"X=" <<2 <<"를 대입한 결과 : " << evaluate(p1, 2) << endl;

 return 0;
}

polynomial::polynomial()
 : len(1) {
 coef = new double[len];
 coef[0] = 0.0;
}

polynomial::polynomial(const polynomial& copy)
 : len(copy.len) {
 coef = new double[len]; //동적배열
 for (int i = 0; i < len; i++)
  coef[i] = copy.coef[i];
}

polynomial::polynomial(int n, const double * coeff)
 : len(n) {
 degree = len - 1;
 coef = new double[len];
 for (int i = 0; i < len; i++)
  coef[i] = coeff[i];
}

polynomial::~polynomial() {
 delete[] coef;
 coef = NULL;
}

const polynomial operator +(const polynomial& p1, const polynomial& p2) {

 polynomial copy1(p1);
 polynomial copy2(p2);
 if (copy1.len < copy2.len) {
  for (int i = 0; i < copy1.len; i++) {
   copy2.coef[i] += copy1.coef[i];

  }
  return copy2;
 }
 else {
  for (int i = 0; i < copy2.len; i++) {
   copy1.coef[i] += copy2.coef[i];
   
  }
  return copy1;
 }
}

const polynomial operator +(const polynomial& p1, double c) {
 polynomial copy(p1);
 copy.coef[0] += c;
 return copy;
}

const polynomial operator +(double c, const polynomial& p1) {
 polynomial copy(p1);
 copy.coef[0] += c;
 return copy;
}

const polynomial operator -(const polynomial& p1, const polynomial& p2) {

 polynomial copy1(p1);
 polynomial copy2(p2);
 if (copy1.len < copy2.len) {
  for (int i = 0; i < copy1.len; i++) {
   copy2.coef[i] = (-1)*copy2.coef[i];
   copy2.coef[i] += copy1.coef[i];

  }
  return copy1;
 }
 else {
  for (int i = 0; i < copy2.len; i++) {
   copy1.coef[i] -= copy2.coef[i];

  }
  return copy1;
 }
}

const polynomial operator -(const polynomial& p1, double c) {
 polynomial copy(p1);
 copy.coef[0] -= c;
 return copy;
}

const polynomial operator -(double c, const polynomial& p1) {
 polynomial copy(p1);
 copy.coef[0] -= c;
 return copy;
}

const polynomial operator *(const polynomial& p1, const polynomial& p2) {
 polynomial copy1(p1);
 polynomial copy2(p2);
 polynomial result;
 result.len = p1.degree + p2.degree + 1; //다항식 간 곱셈은 최고차수+최고차수가 새로운 최고차수가 된다. 길이는 상수를 포함하므로 +1
 result.coef = new double[result.len];
 for (int i = 0; i < result.len; i++)
  result.coef[i] = 0;

 result.degree = copy1.degree*copy2.degree;
 
 for (int i = 0; i < p1.len; i++) {
  for (int j = 0; j < p2.len; j++) {
   
   result.coef[i+j] += p1.coef[i] * p2.coef[j];
   
  }
 }

 return result;
}

const polynomial operator *(const polynomial& p1, double c) {
 polynomial copy(p1);
 for(int i=0; i<copy.len;i++)
  copy.coef[i] *= c;
 return copy;
}

const polynomial operator *(double c, const polynomial& p1) {
 polynomial copy(p1);
 for (int i = 0; i < copy.len; i++)
  copy.coef[i] *= c;
 return copy;
}

polynomial& polynomial::operator =(const polynomial& result) {
 if (len != result.len) {
  delete[] coef;
  coef = NULL;
 }

 len = result.len;
 for (int i = 0; i < len; i++)
  coef[i] = result.coef[i];
 
 return *this;
}

const polynomial polynomial::operator -() const {
 polynomial minus(*this);
 for (int i = 0; i <= minus.len; i++)
  minus.coef[i] += -1;

 return minus;
}

ostream& operator <<(ostream& os, const polynomial& p) {
 for (int i = p.len-1; i > 1; i--) {
  os << p.coef[i] << "x^" << i<<" ";
  if (p.coef[i-1] > 0)
   os << '+';
 }
 os << p.coef[1]<<"x ";
 if (p.coef[0] > 0)
  os << '+';
 if (p.coef[0] != 0)
  os << p.coef[0];

 
 return os;
}

const double evaluate(const polynomial& p1, double value) {
 double Result=0.0;
 polynomial result(p1);
 for (int i = 0; i < result.len; i++)
  Result += result.coef[i] * pow(value, i); //지수만큼 제곱 후 계수와 곱한다.
  
 return Result;
}

댓글