[刷题] 多项式加法、乘法

mooc例题

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 #include<string.h>
  4 #include<time.h>
  5 #include<math.h>
  6 
  7 //指向结构体头节点的指针 
  8 typedef struct PolyNode *Polynomial;
  9 
 10 struct PolyNode{
 11     int coef;
 12     int expon;
 13     Polynomial link;
 14 };
 15 
 16 Polynomial ReadPoly();
 17 void Attach(int c,int e,Polynomial *pRear);
 18 Polynomial Add(Polynomial P1,Polynomial P2);
 19 Polynomial Mult(Polynomial P1,Polynomial P2);
 20 void PrintPoly(Polynomial P);
 21 
 22 int main(){
 23     Polynomial P1,P2,PP,PS;
 24     P1 = ReadPoly();
 25     P2 = ReadPoly();
 26     PP = Mult(P1,P2);
 27     PrintPoly(PP);
 28     printf("\n");
 29     PS = Add(P1,P2);
 30     PrintPoly(PS);
 31     return 0;
 32 }
 33 
 34 /*读入多项式*/ 
 35 Polynomial ReadPoly(){
 36     Polynomial P,Rear,t;    /*Rear为指向当前结果尾项的指针*/ 
 37     int c,e,N;
 38     scanf("%d",&N);
 39     P = (Polynomial)malloc(sizeof(struct PolyNode));    /*申请空节点*/ 
 40     P->link = NULL;
 41     Rear = P;    /*初始化,Rear指向空节点*/
 42     while(N--){
 43         scanf("%d%d",&c,&e);
 44         Attach(c,e,&Rear);        /*增加一项*/ 
 45     } 
 46     t = P;P = P->link;free(t);    /*删除临时生成的头节点*/ 
 47     return P;
 48 }
 49 
 50 /*插入新节点*/ 
 51 /*更改Rear的值,传入指针*/ 
 52 void Attach(int c,int e,Polynomial *pRear){
 53     Polynomial P;
 54     P = (Polynomial)malloc(sizeof(struct PolyNode));
 55     P->coef = c;    
 56     P->expon = e;
 57     P->link = NULL;        /*Rear指向空节点*/ 
 58     (*pRear)->link = P;        /*pRear是指针的指针*/ 
 59     *pRear = P;        /*修改pRear的值*/ 
 60 }
 61 
 62 Polynomial Add(Polynomial P1,Polynomial P2){
 63     Polynomial t1,t2,P,Rear,t;
 64     t1 = P1;t2 = P2;
 65     /*当生成新的头节点*/ 
 66     P = (Polynomial)malloc(sizeof(struct PolyNode));
 67     P -> link = NULL;
 68     Rear = P;
 69     /*当t1,t2都不空时,比较当前t1,t2的指数*/ 
 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             }
 75             t1 = t1->link;
 76             t2 = t2->link;
 77         }
 78         else if(t1->expon > t2->expon){
 79         Attach(t1->coef,t1->expon,&Rear);
 80         t1 = t1->link;
 81         }
 82         else{
 83         Attach(t2->coef,t2->expon,&Rear);
 84         t2 = t2->link;
 85         }
 86     }
 87     while(t1){
 88         Attach(t1->coef,t1->expon,&Rear);
 89         t1 = t1->link;
 90     }
 91     while(t2){
 92         Attach(t2->coef,t2->expon,&Rear);
 93         t2 = t2->link;
 94     }
 95     t = P;
 96     P = P->link;
 97     free(t);
 98     return P;
 99 }
100 
101 Polynomial Mult(Polynomial P1, Polynomial P2)
102 {
103     Polynomial P, Rear;
104     Polynomial t1, t2, t;
105     if (!P1 ||!P2)
106     {
107         return NULL;
108 
109     }
110     t1 = P1;
111     t2 = P2;
112     P = (Polynomial)malloc(sizeof(struct PolyNode));
113     Rear = P;
114     while (t2)        /*用P1的第一项乘以P2,得到初始P*/ 
115     {
116         Attach(t1->coef*t2->coef, t1->expon + t2->expon, &Rear);
117         t2 = t2->link;
118     }
119     t1 = t1->link;
120 
121     while (t1)        /*t1的第一项后的每一项乘以t2的每一项*/
122     {
123         t2 = P2;    /*每循环一遍要使指针回到首节点*/
124         Rear = P;    /*将尾节点置到头结点*/
125         while (t2) 
126         {
127             int c = t1->coef*t2->coef;        /*以后每一项相乘的结果*/
128             int e = t1->expon + t2->expon;
129             while (Rear->link&&Rear->link->expon > e)    /*找插入的位置*/
130             {
131                 Rear = Rear->link;
132             }
133             if (Rear->link&&Rear->link->expon == e)        /*相等就不需要申请一个新的节点,只要把系数相加*/
134             {
135                 if (Rear->link->coef + c)    /*判断系数是否为0*/
136                 {
137                     Rear->link->coef += c;
138                 }
139                 else    /*系数和为0,删除节点*/
140                 {
141                     t = Rear->link;
142                     Rear->link = t->link;
143                     free(t);
144                 }
145             }
146             else    /*不相等,也就是小于的情况*/
147             {
148                 t = (Polynomial)malloc(sizeof(struct PolyNode));    /*申请一个新的节点*/
149                 t->link = NULL;
150                 t->coef = c;
151                 t->expon = e;
152                 t->link = Rear->link;
153                 Rear->link = t;
154                 Rear = Rear->link;
155             }
156             t2 = t2->link;
157         }
158         t1 = t1->link;
159     }
160     /*把一开始申请的内容空P的指针删除,指向下一个位置*/
161     t2 = P;
162     P = P->link;
163     free(t2);
164     return P;
165 }
166 
167 void PrintPoly(Polynomial P)
168 {
169     int flag = 0;    /*辅助调整输出格式*/
170     if (!P)
171     {
172         printf("0 0");
173         return;
174     }
175     while (P)
176     {
177         if (!flag)        /*可以看做系数 指数 ,空格 系数 指数,空格 系数 指数。。。。。。用flag标记第一次*/
178         {
179             flag = 1;
180 
181         }
182         else    /*不是第一项*/
183         {
184             printf(" ");
185         }
186         printf("%d %d", P->coef, P->expon);
187         P = P->link;
188     }
189 }

猜你喜欢

转载自www.cnblogs.com/cxc1357/p/10633524.html