

1 #include <iostream.h>
2 typedef struct lnode
3 {
4 float coef, exp;
5 struct lnode *next;
6 }Lnode, *Linklist;
7 //初始化,使多项式按升幂排列
8 void Initialization(Linklist &L)
9 {
10 int flag;
11 L = (Linklist)malloc(sizeof(Lnode));
12 L->next = NULL;
13 Linklist p, r;
14 cout << "请依次输入各项的系数和指数" << endl;
15 p = (Linklist)malloc(sizeof(Lnode));
16 while (cin >> p->coef >> p->exp && (p->coef != 0 || p->exp != 0))
17 {
18 if (p->coef == 0)
19 continue;
20 if (L->next == NULL) //第一项
21 {
22 flag = 1;
23 L->next = p;
24 p->next = NULL;
25 }
26 else
27 {
28 for (r = L,flag = 0; r->next != NULL;r = r->next) //查找
29 {
30 if (r->next->exp == p->exp) //如果插入的数据与前面的数据的指数项相同
31 {
32 flag = 1;
33 r->next->coef += p->coef;
34 free(p);
35 break;
36 }
37 else if (r->next->exp < p->exp) //插入的元素大于多项式中的指数项
38 continue;
39 else //小于时插入
40 {
41 flag = 1;
42 p->next = r->next;
43 r->next = p;
44 break;
45 }
46
47 }
48 }
49 if (flag == 0) //如果它比多项式中任何数据的指数项都大,则插到最后
50 {
51 p->next = r->next;
52 r->next = p;
53 }
54 p = (Linklist)malloc(sizeof(Lnode));
55 }
56 }
57 Linklist copy(Linklist a) //复制数据,保护原数据
58 {
59 Linklist p = (Linklist)malloc(sizeof(Lnode));
60 Linklist r, q = a->next, t;
61 p->coef = a->coef;
62 p->exp = a->exp;
63 p->next = NULL;
64 t = p;
65 while(q != NULL)
66 {
67 r = (Linklist)malloc(sizeof(Lnode));
68 r->coef = q->coef;
69 r->exp = q->exp;
70 r->next = t->next;
71 t->next = r;
72 t = r;
73 q = q->next;
74 }
75 return p;
76 }
77 void output(Linklist L)
78 {
79 if (L->next == NULL)
80 cout << "空";
81 else
82 {
83 L = L->next;
84 cout << L->coef << "*" << "x^" << L->exp;
85 while(L->next != NULL)
86 {
87 L = L->next;
88 if (L->coef >= 0)
89 cout << "+" << L->coef << "*" << "x^" << L->exp;
90 else
91 cout << L->coef << "*" << "x^" <<L->exp;
92 }
93 }
94 }
95
96 Linklist add(Linklist a, Linklist b)
97 {
98 Linklist p, q, r, t;
99 p = a->next;
100 q = b->next;
101 r = a;
102 if(a->next == NULL)
103 return b;
104 if(b->next == NULL)
105 return a;
106 while (q != NULL && p != NULL)
107 {
108 if (p->exp < q->exp) //小于时
109 {
110 r = r->next;
111 p = p->next;
112 }
113 else if (p->exp > q->exp) //大于时插入
114 {
115 t = (Linklist)malloc(sizeof(Lnode));
116 t->coef = q->coef;
117 t->exp = q->exp;
118 r->next = t;
119 t->next = p;
120 r = t;
121 q = q->next;
122 }
123 else //相同时相加
124 {
125 p->coef += q->coef;
126 if (p->coef == 0)
127 {
128 r->next = p->next;
129 }
130 q = q->next;
131 r = p;
132 p = p->next;
133 }
134 }
135 if (q) //把第二个多项式没有插入的部分,插入到第一个多项式的最后
136 r->next = q;
137 return a;
138 }
139
140 Linklist sub(Linklist a, Linklist b)
141 {
142 Linklist c = b->next;
143 while (c != NULL)
144 {
145 c->coef = 0 - c->coef;
146 c = c->next;
147 }
148 return add(a, b);
149 }
150




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

#include <iostream.h>
typedef struct lnode
{
float coef, exp;
struct lnode *next;
}Lnode, *Linklist;
//初始化,使多项式按升幂排列
void Initialization(Linklist &L)
{
int flag;
L = (Linklist)malloc(sizeof(Lnode));
L->next = NULL;
Linklist p, r;
cout << "请依次输入各项的系数和指数" << endl;
p = (Linklist)malloc(sizeof(Lnode));
while (cin >> p->coef >> p->exp && (p->coef != 0 || p->exp != 0))
{
if (p->coef == 0)
continue;
if (L->next == NULL) //第一项
{
flag = 1;
L->next = p;
p->next = NULL;
}
else
{
for (r = L,flag = 0; r->next != NULL;r = r->next) //查找
{
if (r->next->exp == p->exp) //如果插入的数据与前面的数据的指数项相同
{
flag = 1;
r->next->coef += p->coef;
free(p);
break;
}
else if (r->next->exp < p->exp) //插入的元素大于多项式中的指数项
continue;
else //小于时插入
{
flag = 1;
p->next = r->next;
r->next = p;
break;
}
}
}
if (flag == 0) //如果它比多项式中任何数据的指数项都大,则插到最后
{
p->next = r->next;
r->next = p;
}
p = (Linklist)malloc(sizeof(Lnode));
}
}
Linklist copy(Linklist a) //复制数据,保护原数据
{
Linklist p = (Linklist)malloc(sizeof(Lnode));
Linklist r, q = a->next, t;
p->coef = a->coef;
p->exp = a->exp;
p->next = NULL;
t = p;
while(q != NULL)
{
r = (Linklist)malloc(sizeof(Lnode));
r->coef = q->coef;
r->exp = q->exp;
r->next = t->next;
t->next = r;
t = r;
q = q->next;
}
return p;
}
void output(Linklist L)
{
if (L->next == NULL)
cout << "空";
else
{
L = L->next;
cout << L->coef << "*" << "x^" << L->exp;
while(L->next != NULL)
{
L = L->next;
if (L->coef >= 0)
cout << "+" << L->coef << "*" << "x^" << L->exp;
else
cout << L->coef << "*" << "x^" <<L->exp;
}
}
}
Linklist add(Linklist a, Linklist b)
{
Linklist p, q, r, t;
p = a->next;
q = b->next;
r = a;
if(a->next == NULL)
return b;
if(b->next == NULL)
return a;
while (q != NULL && p != NULL)
{
if (p->exp < q->exp) //小于时
{
r = r->next;
p = p->next;
}
else if (p->exp > q->exp) //大于时插入
{
t = (Linklist)malloc(sizeof(Lnode));
t->coef = q->coef;
t->exp = q->exp;
r->next = t;
t->next = p;
r = t;
q = q->next;
}
else //相同时相加
{
p->coef += q->coef;
if (p->coef == 0)
{
r->next = p->next;
}
q = q->next;
r = p;
p = p->next;
}
}
if (q) //把第二个多项式没有插入的部分,插入到第一个多项式的最后
r->next = q;
return a;
}
Linklist sub(Linklist a, Linklist b)
{
Linklist c = b->next;
while (c != NULL)
{
c->coef = 0 - c->coef;
c = c->next;
}
return add(a, b);
}


#include<iostream.h>
#include<stdlib.h>
#include "polynomial.h"
int main()
{
Linklist pa, pb;
char y = 'y';
while (y == 'Y' || y == 'y')
{
cout << "*****一元多项式加减法*****" << endl;
cout << "建立一元多项式A(x)[以0 0 为结束符]" << endl;
Initialization(pa);
cout << "A(x) = ";
output(pa);
cout << endl;
cout << "建立一元多项式B(x)[以0 0 为结束符]" << endl;
Initialization(pb);
cout << "B(x) = ";
output(pb);
cout << endl;
cout << "两式相加 = " << endl;
output(add(copy(pa), copy(pb)));
cout<< endl;
cout << "两式相减 = " << endl;
output(sub(copy(pa), copy(pb)));
cout<< endl;
cout << "are you continue? y or n?" << endl;
cin >> y;
if (y != 'y' && y != 'Y')
break;
}
return 0;
}