자료구조 수업을 들으면서 과제로 다항식 연산을 C++코드로 구현하라는 것이 나왔다.
C++ 자체가 처음이라 C++부터 공부를 했는데, 다행히도 내가 배운 C와 Java가 섞여있는 언어라
배우는데 그다지 오래 걸리지는 않았다.
다항식 연산에서 termArray를 static으로 쓰는 것이 포인트고,
매커니즘은 내가 그린 그림과 같다...(이렇게 그림 그려서 코드 작성하니 훨씬 쉬워지는 듯)
곱셈의 경우 추가적인 옵션 과제였는데,
나는 한번 곱셈해서 나온 다항식을 계속해서 더하는 방식으로
곱셈을 구현했다.
코드가 조금 길긴하지만 아래에 붙여넣도록 하겠다.
(다항식 곱셈에 관해서 조금더 효율적이 있는 방법이 있으신 분은 댓글로 같이 공유해봐요!)
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | #include <vector> #include <algorithm> #include <iostream> #define RESET_COLOR "\033[0m" #define GREEN "\033[32m" using namespace std; class Term { friend class Polynomial; private: float coef; int exp; }; class Polynomial { public: Polynomial(); Polynomial Add(Polynomial b); Polynomial Substraction(Polynomial b); Polynomial Multiplication(Polynomial b); void NewTerm(const float theCoeff, const int theExp); int Display(); void GetData(); private: static Term *termArray; static int capacity; static int free; int terms; int start, finish; void ClearTerm(int startPos, int endPos); }; Polynomial::Polynomial() { terms = 0; } int Polynomial::Display() { int aPos = start; for (; aPos <= finish; aPos++) { cout << termArray[aPos].coef << "x^" << termArray[aPos].exp; if (aPos != finish && termArray[aPos].coef > 0) cout << "+"; } cout << "\n"; return 0; } void Polynomial::GetData() { start = free; float theCoeff; int theExp; int pos = start; int answer; while (1) { cout << "Enter Coefficient of the term : "; cin >> theCoeff; cout << "Enter Exponent of the term : "; cin >> theExp; NewTerm(theCoeff, theExp); cout << "Do you want to input next term?\n[Yes = 1, No = 0] : "; cin >> answer; if (termArray[pos].exp == 0 || answer == 0) { break; } else { pos++; } } finish = pos; } void Polynomial::ClearTerm(int startPos, int endPos) { Term *temp = new Term[capacity]; Term *resultTerm = new Term[free - endPos]; copy(termArray + endPos + 1, termArray + free, resultTerm); copy(termArray, termArray + startPos, temp); copy(resultTerm, resultTerm + (free - endPos - 1), temp + startPos); delete[] termArray; delete[] resultTerm; termArray = temp; free = endPos; finish = free - 1; } void Polynomial::NewTerm(const float theCoeff, const int theExp) { if (free == capacity) { capacity *= 2; Term *temp = new Term[capacity]; copy(termArray, termArray + free, temp); delete[] termArray; termArray = temp; } termArray[free].coef = theCoeff; termArray[free++].exp = theExp; terms++; } Polynomial Polynomial::Add(Polynomial b) { Polynomial c; int aPos = start; int bPos = b.start; int aTerms = start + terms; int bTerms = b.start + b.terms; c.start = free; while ((aPos < aTerms) && (bPos < bTerms)) if ((termArray[aPos].exp == termArray[bPos].exp)) { float t = termArray[aPos].coef + termArray[bPos].coef; if (t) c.NewTerm(t, termArray[aPos].exp); aPos++; bPos++; } else if ((termArray[aPos].exp < termArray[bPos].exp)) { c.NewTerm(termArray[bPos].coef, termArray[bPos].exp); bPos++; } else { c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); aPos++; } for (; aPos < aTerms; aPos++) c.NewTerm(termArray[aPos].coef, termArray[aPos].exp); for (; bPos < bTerms; bPos++) c.NewTerm(termArray[bPos].coef, termArray[bPos].exp); c.finish = free - 1; return c; } Polynomial Polynomial::Substraction(Polynomial b) { Polynomial c; int aPos = start; int bPos = b.start; int aTerms = start + terms; int bTerms = b.start + b.terms; c.start = free; while ((aPos < aTerms) && (bPos < bTerms)) if ((termArray[aPos].exp == termArray[bPos].exp)) { float t = termArray[aPos].coef - termArray[bPos].coef; if (t) c.NewTerm(t, termArray[aPos].exp); aPos++; bPos++; } else if ((termArray[aPos].exp < termArray[bPos].exp)) { c.NewTerm(-termArray[bPos].coef, termArray[bPos].exp); bPos++; } else { c.NewTerm(-termArray[aPos].coef, termArray[aPos].exp); aPos++; } for (; aPos < aTerms; aPos++) c.NewTerm(-termArray[aPos].coef, termArray[aPos].exp); for (; bPos < bTerms; bPos++) c.NewTerm(-termArray[bPos].coef, termArray[bPos].exp); c.finish = free - 1; return c; } Polynomial Polynomial::Multiplication(Polynomial b) { Polynomial c; int aPos = start; int aTerms = start + terms; int bTerms = b.start + b.terms; c.start = free++; for (; aPos < aTerms; aPos++) { Polynomial k, r; r = c; k.start = c.terms ? c.start + c.terms : (c.start + c.terms + 1); for (int bPos = b.start; bPos < bTerms; bPos++) { float multCoef = termArray[aPos].coef * termArray[bPos].coef; int multExp = termArray[aPos].exp + termArray[bPos].exp; k.NewTerm(multCoef, multExp); k.finish = free - 1; } r = r.Add(k); c.finish = k.start - 1; c.terms = r.terms; c.ClearTerm(c.start, k.finish); } return c; } int Polynomial::capacity = 1; Term *Polynomial::termArray = new Term[capacity]; int Polynomial::free = 0; int main() { int choice; Polynomial P1, P2, P3; cout << "Instruction:- \nExample:-\nP(x)=5x^3+3x^1\nEnter the Polynomial like\nP(x)=5x^3+0x^2+3x^1+0x^0\n"; cout << "Enter Polynomial1:-" << endl; P1.GetData(); cout << "Enter Polynomial2:-" << endl; P2.GetData(); while (1) { cout << "\n****** Menu Selection ******" << endl; cout << "1: Addition\n2: Substraction\n3: Multiplication\n0: Exit" << endl; cout << "Enter ypur choice:"; cin >> choice; switch (choice) { case 1: cout << "\n--------------- Addition ---------------\n"; cout << "Polynomial1:"; P1.Display(); cout << "Polynomial2:"; P2.Display(); P3 = P1.Add(P2); cout << "Result :"; P3.Display(); cout << "----------------------------------------\n"; break; case 2: cout << "\n------------- Substraction -------------\n"; cout << "Polynomial1:"; P1.Display(); cout << "Polynomial2:"; P2.Display(); P3 = P1.Substraction(P2); cout << "Result :"; P3.Display(); cout << "----------------------------------------\n"; break; case 3: cout << "\n----------- Multiplication -------------\n"; cout << "Polynomial1:"; P1.Display(); cout << "Polynomial2:"; P2.Display(); P3 = P1.Multiplication(P2); cout << "Result :"; P3.Display(); cout << "----------------------------------------\n"; break; case 0: cout << "Good Bye...!!!" << endl; exit(0); } } return 0; } | cs |
'프로그래밍 > C | C++' 카테고리의 다른 글
[C++] Exception / Error Handling 연습하기 (0) | 2017.06.09 |
---|