实验二:一元多项式的基本运算
实验目的:掌握用线性表实现一元多项式的基本运算。
实验内容:使用链式存储实现一元多项式的加法、减法、乘法和求导。即:
C(x)= A(x)+B(x);C(x)= A(x)-B(x) C(x)= A(x)*B(x) C(x)= A’(x)
菜单:
1)C :分别创建两个多项式A(x)和B(x),其中 输入时按照 指数的升序顺序输入,遇到系数为0则停止。例如:输入 :
1 2 3 4 5 6 7 8
2 3 4 5 6 7 0
则生成的多项式分别为:
A(x)=x^2+3x^4+5x^6+7x^8
B(x)=2x^3+4x^5+6x^7
2)P:计算C(x)= A(x)+B(x),计算完毕后输出C(x)的 结果
3)S: 计算C(x)= A(x)-B(x),计算完毕后输出C(x)的 结果
4)M: 计算C(x)= A(x)*B(x),计算完毕后输出C(x)的 结果
5)D: 计算C(x)= A’(x),计算完毕后输出C(x)的 结果
6)V: 首先输入一个 float型数据,然后计算 A(x)并输出计算的结果。
7)E: 分别清空A(x)、B(x)、C(x)三个多项式。
8)X: 退出程序。

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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
| #include <cmath> #include <iomanip> #include <iostream> #include <stdlib.h> using namespace std; class List; class LinkNode { friend class List;
private: LinkNode *link; int coef; int exp;
public: LinkNode(LinkNode *p = NULL) { link = p; } LinkNode(int num, LinkNode *p = NULL) { coef = num; link = p; } LinkNode(const int &a, const int &b, LinkNode *ptr = NULL) { coef = a; exp = b; link = ptr; } }; class List { protected: LinkNode *first;
public: List(); ~List() { MakeEmpty(); } void MakeEmpty(); void input(); void output(); void Insert(const int &a, const int &b); void Add(List &l1, List &l2, List &l3); void Sub(List &l1, List &l2, List &l3); void Mul(List &l1, List &l2, List &l3); void Dao(); void Cal(float a); void Qc(); void Copy(List &l3); void sort(); }; List::List() { first = new LinkNode(); } void List::MakeEmpty() { LinkNode *q; while (first->link != NULL) { q = first->link; first->link = q->link; delete q; } } void List::input() { int coef, exp; cin >> coef; while (coef) { cin >> exp; LinkNode *newnode = new LinkNode(coef, exp); newnode->link = first->link; first->link = newnode; cin >> coef; } } void List::output() { cout << "C(x)="; LinkNode *node = first->link; while (node != NULL) { if (node->exp != 0) { if (node->coef != 1) { if (node->coef < 0) { cout << node->coef << "x"; } if (node->coef > 0) { cout << node->coef << "x"; } } if (node->coef == 1) { cout << "x"; } if (node->exp != 1) { if (node->exp > 0) { cout << "^" << node->exp; } else { cout << "^(" << node->exp << ")"; } } } if (node->exp == 0) { cout << node->coef; }
if (node->link != NULL && node->link->coef > 0) { cout << "+"; } node = node->link; } cout << endl; } void List::sort() { for (LinkNode *node1 = first->link; node1 != NULL; node1 = node1->link) { for (LinkNode *node2 = node1->link; node2 != NULL; node2 = node2->link) { int j; if (node1->exp > node2->exp) { j = node2->exp; node2->exp = node1->exp; node1->exp = j; j = node2->coef; node2->coef = node1->coef; node1->coef = j; } } } } void List::Insert(const int &a, const int &b) { LinkNode *newnode = new LinkNode(a, b); newnode->link = first->link; first->link = newnode; } void List::Qc() { LinkNode *pc, *p; p = first->link; while (p->link != NULL) { LinkNode *q = p->link; while (q != NULL) { if (p->exp == q->exp) { p->coef = p->coef + q->coef; q->coef = 0; } q = q->link; } p = p->link; } LinkNode *r = first; while (r != NULL) { if (r->link != NULL) { LinkNode *q = r->link; int flag = 0; if (q->coef == 0) { r->link = q->link; delete q; flag = 1; } if (!flag && r != NULL) r = r->link; } else { if (r->coef == 0) delete r; r = r->link; } } } void List::Add(List &l1, List &l2, List &l3) { LinkNode *node1 = l1.first->link; LinkNode *node2 = l2.first->link; while (node1 != NULL) { l3.Insert(node1->coef, node1->exp); node1 = node1->link; } while (node2 != NULL) { l3.Insert(node2->coef, node2->exp); node2 = node2->link; } l3.Qc(); } void List::Sub(List &l1, List &l2, List &l3) { LinkNode *node1 = l1.first->link; LinkNode *node2 = l2.first->link; while (node1 != NULL) { l3.Insert(node1->coef, node1->exp); node1 = node1->link; } while (node2 != NULL) { l3.Insert(-(node2->coef), node2->exp); node2 = node2->link; } l3.Qc(); } void List::Mul(List &l1, List &l2, List &l3) { l1.sort(); l2.sort(); LinkNode *p = l1.first->link; LinkNode *q; while (p != NULL) { q = l2.first->link; while (q != NULL) { l3.Insert(p->coef * q->coef, p->exp + q->exp); q = q->link; } p = p->link; } l3.Qc(); if (l3.first->link == NULL) { l3.Insert(0, 0); } } void List::Dao() { LinkNode *p = first->link; while (p != NULL) { p->coef = p->coef * p->exp; p->exp = p->exp - 1; p = p->link; } } void List::Cal(float a) { float sum = 0;
for (LinkNode *p = first->link; p != NULL; p = p->link) { sum = sum + (p->coef) * pow(a, p->exp); } cout << setiosflags(ios::fixed) << setprecision(2); cout << sum << endl; } void List::Copy(List &l3) { for (LinkNode *p = first->link; p != NULL; p = p->link) { l3.Insert(p->coef, p->exp); } }
int main() {
List l1, l2, l3, l4; while (1) { char a; cin >> a; if (a == 'C') { l1.input(); l2.input(); } else if (a == 'P') { l3.MakeEmpty(); l3.Add(l1, l2, l3); l3.sort(); l3.output(); } else if (a == 'S') { l3.MakeEmpty(); l3.Sub(l1, l2, l3); l3.sort(); l3.output(); } else if (a == 'M') { l3.MakeEmpty(); l3.Mul(l1, l2, l3); l3.sort(); l3.output(); } else if (a == 'D') { l3.MakeEmpty(); l1.Copy(l3); l3.Dao(); l3.sort(); l3.Qc(); l3.output(); } else if (a == 'V') { float x; cin >> x; l1.Cal(x); } else if (a == 'E') { l1.MakeEmpty(); l2.MakeEmpty(); l3.MakeEmpty(); } else if (a == 'X') { return 0; } } return 0; }
|