一元多项式 加法 减法 乘法

Posted on 2020-05-08 12:13  黑炽  阅读(480)  评论(0编辑  收藏  举报
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 //系数 coefficient,coef
  4 //指数 exponent, expon
  5 typedef struct polyNode* polynomial;
  6 struct polyNode {
  7     int coef;
  8     int expon;
  9     polynomial link;
 10 };
 11 
 12 polynomial readPoly();
 13 //输入 必须是从幂的降幂 输入
 14 void attach(int c, int e, polynomial* prear);
 15 //把这一项插入到链表中
 16 polynomial polyAdd(polynomial p1, polynomial p2);
 17 //主要进行多项式加法运算
 18 polynomial polyMul(polynomial p1, polynomial p2);
 19 //主要进行多项式乘法运算
 20 polynomial polySub(polynomial p1, polynomial p2);
 21 //主要进行多项式减法运算
 22 int compare(int a, int b);//比较两个项的指数
 23 void printPoly(polynomial p);
 24 void display();//显示计算器的选项
 25 
 26 
 27 int main(void) {
 28     polynomial p1, p2, ps;
 29     int t;
 30     while (1) {
 31         display();
 32         scanf("%d", &t);
 33         switch (t) {
 34         case 1:
 35             p1 = readPoly();
 36             p2 = readPoly();
 37             ps = polyAdd(p1, p2);
 38             printPoly(ps);
 39             printf("是否继续操作: 是请输入1,否则输出0:");
 40             scanf("%d", &t);
 41             if (t == 1)
 42                 system("cls");
 43             else if (t == 0)
 44                 return 0;
 45             break;
 46         case 2:
 47             p1 = readPoly();
 48             p2 = readPoly();
 49             ps = polySub(p1, p2);
 50             printPoly(ps);
 51             printf("是否继续操作: 是请输入1,否则输出0:");
 52             scanf("%d", &t);
 53             if (t == 1)
 54                 system("cls");
 55             else if (t == 0)
 56                 return 0;
 57             break;
 58         case 3:
 59             p1 = readPoly();
 60             p2 = readPoly();
 61             ps = polyMul(p1, p2);
 62             printPoly(ps);
 63             printf("是否继续操作: 是请输入1,否则输出0:");
 64             scanf("%d", &t);
 65             if (t == 1)
 66                 system("cls");
 67             else if (t == 0)
 68                 return 0;
 69             break;
 70         }
 71     }
 72     return 0;
 73 }
 74 
 75 void display() {
 76     printf("1.多项式加法\n\n");
 77     printf("2.多项式减法\n\n");
 78     printf("3.多项式乘法\n\n");
 79     printf("请输入所执行内容:");
 80 }
 81 
 82 polynomial readPoly() {
 83     int n, c, e;
 84     polynomial rear, p, t;
 85 
 86     p = (polynomial)malloc(sizeof(struct polyNode));
 87     p->link = NULL;
 88     rear = p;
 89     scanf("%d", &n);//这个语句用来说明有几项
 90     while (n--) {
 91         scanf("%d%d", &c, &e);
 92         attach(c, e, &rear);
 93     }
 94     t = p; p = p->link; free(t);
 95     return p;
 96 }
 97 
 98 void attach(int c, int e, polynomial* prear) {
 99     polynomial p;
100 
101     p = (polynomial)malloc(sizeof(struct polyNode));
102     p->coef = c;
103     p->expon = e;
104     p->link = NULL;
105     (*prear)->link = p;
106     *prear = p;//修改prear的值
107 }
108 
109 polynomial polyAdd(polynomial p1, polynomial p2) {
110     polynomial front, rear, t;
111     int sum;
112 
113     rear = (polynomial)malloc(sizeof(struct polyNode));
114     front = rear;
115     //对比相应项的指数,若指数相同,则把系数相加
116     //否则,把指数大的先插入链表,再指针后移一位
117     while (p1 && p2) {
118         switch (compare(p1->expon, p2->expon)) {
119         case 1:
120             attach(p1->coef, p1->expon, &rear);
121             p1 = p1->link;
122             break;
123         case -1:
124             attach(p2->coef, p2->expon, &rear);
125             p2 = p2->link;
126             break;
127         case 0:
128             sum = p1->coef + p2->coef;
129             if (sum)    attach(sum, p1->expon, &rear);
130             p1 = p1->link;
131             p2 = p2->link;
132             break;
133         }
134     }
135     //因为肯定最多有一个多项式剩余项,所以把残留的也存入链表
136     for (; p1; p1 = p1->link)    attach(p1->coef, p1->expon, &rear);
137     for (; p2; p2 = p2->link)    attach(p2->coef, p2->expon, &rear);
138     rear->link = NULL;
139     t = front;
140     front = front->link;
141     free(t);//把头空节点释放
142     return front;
143 }
144 
145 int compare(int a, int b) {
146     if (a == b)    return 0;
147     else if (a > b)    return 1;
148     else return -1;
149 }
150 
151 void printPoly(polynomial p) {
152     int flag = 0;
153 
154     if (!p) {
155         printf("0 0\n");
156         return;
157     }
158 
159     while (p) {
160         if (!flag)    flag = 1;
161         else printf(" ");//主要在每一组数据之后输出一个空格隔开
162         printf("%d %d", p->coef, p->expon);
163         p = p->link;
164     }
165 
166     printf("\n");
167 }
168 
169 polynomial polyMul(polynomial p1, polynomial p2) {
170     polynomial p, rear, t1, t2, t;
171     int c, e;
172 
173     if (!p1 || !p2)    return NULL;
174     t1 = p1; t2 = p2;
175     p = (polynomial)malloc(sizeof(struct polyNode));
176     p->link = NULL;
177     rear = p;
178     //先用p1的第一项 乘以p2,得到p
179     while (t2) {
180         attach(t1->coef * t2->coef, t1->expon + t2->expon, &rear);
181         t2 = t2->link;
182     }
183     t1 = t1->link;
184     //下面这一大部分 是用t1的 从第二项开始,每一项乘以整个t2。
185     while (t1) {
186         t2 = p2; rear = p;
187         while (t2) {
188             e = t1->expon + t2->expon;
189             c = t1->coef * t2->coef;
190             //把该结点插入到正确的位置,因为指数是按降序排列的
191             while (rear->link && rear->link->expon > e)
192                 rear = rear->link;
193             //若找到位置,则需要和下一项比,因为指数可能会相同
194             //相同则系数相加
195             //否则直接插入即可
196             if (rear->link && rear->link->expon == e) {
197                 if (rear->link->coef + c)
198                     rear->link->coef += c;
199                 else {
200                     t = rear->link;
201                     rear->link = t->link;
202                     free(t);
203                 }
204             }
205             else {
206                 t = (polynomial)malloc(sizeof(struct polyNode));
207                 t->coef = c; t->expon = e;
208                 t->link = rear->link;
209                 rear->link = t;
210                 rear = rear->link;
211             }
212             t2 = t2->link;
213         }
214         t1 = t1->link;
215     }
216     t2 = p; p = p->link; free(t2);//删除头空节点
217     return p;
218 }
219 
220 polynomial polySub(polynomial p1, polynomial p2) {
221     polynomial p = p2;
222     while (p) {
223         p->coef *= -1;
224         p = p->link;
225     }
226     //下面是加法运算的代码,因为根据多项式减法法则
227     //也只是 类似减数的那一个多项式,把每一项的系数取相反数
228     //再相加即可
229     polynomial front, rear, t;
230     int sum;
231 
232     rear = (polynomial)malloc(sizeof(struct polyNode));
233     front = rear;
234     while (p1 && p2) {
235         switch (compare(p1->expon, p2->expon)) {
236         case 1:
237             attach(p1->coef, p1->expon, &rear);
238             p1 = p1->link;
239             break;
240         case -1:
241             attach(p2->coef, p2->expon, &rear);
242             p2 = p2->link;
243             break;
244         case 0:
245             sum = p1->coef + p2->coef;
246             if (sum)    attach(sum, p1->expon, &rear);
247             p1 = p1->link;
248             p2 = p2->link;
249             break;
250         }
251     }
252 
253     for (; p1; p1 = p1->link)    attach(p1->coef, p1->expon, &rear);
254     for (; p2; p2 = p2->link)    attach(p2->coef, p2->expon, &rear);
255     rear->link = NULL;
256     t = front;
257     front = front->link;
258     free(t);
259     return front;
260 }