码迷,mamicode.com
首页 > 其他好文 > 详细

线性结构CT 02-线性结构1 一元多项式的乘法与加法运算

时间:2015-10-01 00:36:09      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 typedef struct PolyNode *Polynomial;
  4 struct PolyNode 
  5 {
  6     int coef;
  7     int expon;
  8     struct PolyNode *next;
  9 };
 10 
 11 Polynomial ReadPoly();
 12 void Attach(int c,int e,Polynomial *pReal);
 13 Polynomial Add(Polynomial P1,Polynomial P2);
 14 Polynomial Mult(Polynomial P1,Polynomial P2);
 15 void PrintPoly(Polynomial P);
 16 
 17 int main()
 18 {
 19     Polynomial P1,P2,PMult,PSum;
 20     P1 = ReadPoly();
 21     P2 = ReadPoly();
 22     PMult = Mult(P1,P2);
 23     PrintPoly(PMult);
 24     PSum = Add(P1,P2);
 25     PrintPoly(PSum);
 26     return 0;
 27 }
 28 Polynomial ReadPoly()
 29 {
 30     Polynomial P,Rear,t;
 31     int c,e,N;
 32     scanf("%d",&N);
 33     P = (Polynomial)malloc(sizeof(struct PolyNode));
 34     P->next = NULL;
 35     Rear = P;
 36     while(N--){
 37         scanf("%d %d",&c,&e);
 38         Attach(c,e,&Rear);
 39     }
 40     t = P;
 41     P = P->next;
 42     free(t);
 43     return P;
 44 }
 45 void Attach(int c,int e,Polynomial *pReal)
 46 {
 47     Polynomial P;
 48     P = (Polynomial)malloc(sizeof(struct PolyNode));
 49     P->coef = c;
 50     P->expon = e;
 51     P->next = NULL;
 52     (*pReal)->next = P;
 53     *pReal = P;
 54 }
 55 
 56 Polynomial Add(Polynomial P1,Polynomial P2)
 57 {
 58     Polynomial P,t1,t2,t,Rear;
 59     if(!P1 && !P2){  
 60         if(!P1)
 61             return P2;
 62         else
 63             return P1;
 64     }
 65     P = (Polynomial)malloc(sizeof(struct PolyNode));
 66     P->next = NULL;
 67     Rear = P;
 68     t1 = P1;
 69     t2 = P2;
 70     while(t1 && t2){
 71         if(t1->expon == t2->expon){
 72             if(t1->coef + t2->coef) 
 73                 Attach(t1->coef + t2->coef,t1->expon,&Rear);
 74             t1 = t1->next;
 75             t2 = t2->next;
 76         }
 77         else if(t1->expon > t2->expon){
 78             if(t1->coef)
 79                 Attach(t1->coef,t1->expon,&Rear);
 80             t1 = t1->next;
 81         }
 82         else{
 83             if(t2->coef)
 84                 Attach(t2->coef,t2->expon,&Rear);
 85             t2 = t2->next;
 86         }
 87     }
 88     while(t1){
 89         if(t1->coef)
 90             Attach(t1->coef,t1->expon,&Rear);
 91         t1 = t1->next; 
 92     } 
 93     while(t2){
 94         if(t2->coef)
 95             Attach(t2->coef,t2->expon,&Rear);
 96         t2 = t2->next; 
 97     }
 98     t = P;
 99     P = P->next;
100     free(t);
101     return P;
102 }
103 Polynomial Mult(Polynomial P1,Polynomial P2)
104 {
105     Polynomial P,t1,t2,t,Rear;
106     int c,e;
107     if(!P1 || !P2)
108         return NULL;
109     t1 = P1;
110     t2 = P2;
111     P = (Polynomial)malloc(sizeof(struct PolyNode));
112     P->next = NULL;
113     Rear = P;
114     while(t2){
115         if(t1->coef * t2->coef){
116             Attach(t1->coef * t2->coef,t1->expon + t2->expon,&Rear); 
117         }
118         t2 = t2->next;
119     }
120     t1 = t1->next;
121     while(t1){
122         t2 = P2;
123         Rear = P;
124         while(t2){
125             e = t1->expon + t2->expon;
126             c = t1->coef * t2->coef;
127             while(Rear->next && Rear->next->expon > e)
128                 Rear = Rear->next;
129             if(Rear->next && Rear->next->expon == e){
130                 if(Rear->next->coef + c)
131                     Rear->next->coef += c;
132                 else{
133                     t = Rear->next;
134                     Rear->next = t->next;
135                     free(t);
136                 }
137             }
138             else{
139                 if(c){
140                     t = (Polynomial)malloc(sizeof(struct PolyNode));
141                     t->coef = c;
142                     t->expon = e;
143                     t->next = Rear->next;
144                     Rear->next = t;
145                     Rear = Rear->next;
146                 }
147             }
148         t2 = t2->next;
149         }
150     t1 = t1->next;
151     }
152     t2 = P;
153     P = P->next;
154     free(t2);
155     return P;
156 }
157 void PrintPoly(Polynomial P)
158 {
159     int flag = 0;
160     if(!P)
161         printf("0 0");
162     while(P){
163         if (!flag)
164             flag = 1;
165         else
166             printf(" ");
167         printf("%d %d", P->coef, P->expon);
168         P = P->next;
169     }
170     printf("\n");
171 }

需注意的点:需系数为0时的处理!

乘法运算实现方法即多项式逐项相乘的手算方法,将所得单项结果有序插入第一个多项式第一项与第二个多项式所得的多项式P中。

 

线性结构CT 02-线性结构1 一元多项式的乘法与加法运算

标签:

原文地址:http://www.cnblogs.com/kuotian/p/4850564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!