线性结构习题2——一元多项式的乘法与加法运算

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

输入格式:

输入分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

题目解读:

以输入样例为例,首先输入4表示有四个非零项,其次输入四组非零项的系数和指数,即样例中输入的多项式为:

f1(x)=3x4-5x2+6x-2;    f2(x)=5x20-7x4+3x

输出样例中,

第一行为积:f3(x)=15x24-25x22+30x21-10x20-21x8+35x6-33x5+14x4-15x3+18x2-6x

第二行为和:f4(x)=5x20-4x4-5x2+9x-2


 参考一些别人的博客,对于链表中指针操作需要进一步熟练才行。具体的实现过程可以在草稿纸上走一下,加深理解。

程序实现【vs2015】:

关于结构体

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 typedef struct node{//定义结构体类型的结点
  4     int coefficient;//系数
  5     int exponent;   //指数
  6     struct node * next;//下一个结点
  7 }PolyNode, *Polynomial;
  8 //声明结构体变量PolyNode,以及一个指向它的指针polynomial
  9 
 10 Polynomial ReadPoly();//获取多项式的函数
 11 void Attach(int c, int e, Polynomial * Rear);
 12 Polynomial MultPoly(Polynomial P1, Polynomial P2);
 13 Polynomial AddPoly(Polynomial P1, Polynomial P2);
 14 void PrintPoly(Polynomial P);
 15 
 16 int main(int argc, char const *argv[]){
 17     Polynomial Poly1, Poly2, PolySum, PolyMul;
 18     Poly1 = ReadPoly();
 19     Poly2 = ReadPoly();
 20     PolyMul = MultPoly(Poly1, Poly2);
 21     PrintPoly(PolyMul);
 22     PolySum = AddPoly(Poly1, Poly2);
 23     PrintPoly(PolySum);
 24     system("pause");
 25     return 0;
 26 }
 27 
 28 Polynomial ReadPoly(){
 29     Polynomial P, Rear, temp;
 30     P = (PolyNode*)malloc(sizeof(PolyNode));//开辟一个新结点空间,将其
 31     P->next = NULL;//指向链表的第一个结点
 32     Rear = P;
 33     int N, c, e;
 34     scanf_s("%d", &N);
 35     while(N--){
 36         scanf_s("%d %d", &c, &e);
 37         Attach(c, e, &Rear);
 38     }
 39     temp = P;
 40     P = P->next; 
 41     free(temp);
 42     return P;
 43 }
 44 
 45 void Attach(int c, int e, Polynomial * pRear){
 46     Polynomial P;
 47     P = (PolyNode*)malloc(sizeof(PolyNode));
 48     P->coefficient = c;
 49     P->exponent = e;
 50     P->next = NULL;
 51     (*pRear)->next = P;
 52     *pRear = P;
 53 }
 54 
 55 Polynomial MultPoly(Polynomial P1, Polynomial P2){
 56     Polynomial P, temp1, temp2, Rear, temp;
 57     int c, e;
 58     if(!P1 || !P2)
 59         return NULL;
 60     temp1 = P1;
 61     temp2 = P2;
 62     P = (PolyNode*)malloc(sizeof(PolyNode));
 63     P->next = NULL;
 64     Rear = P;
 65     while(temp2){
 66         c = temp1->coefficient * temp2->coefficient;
 67         e = temp1->exponent + temp2->exponent;
 68         if(c != 0){
 69             Attach(c, e, &Rear);
 70             temp2 = temp2->next;
 71         }
 72     }
 73     temp1 = temp1->next;
 74     while(temp1){
 75         temp2 = P2, Rear = P;
 76         while(temp2){
 77             c = temp1->coefficient * temp2->coefficient;
 78             e = temp1->exponent + temp2->exponent;
 79             if(c != 0){
 80                 while(Rear->next && Rear->next->exponent > e)
 81                     Rear = Rear->next;
 82                 if(Rear->next && Rear->next->exponent == e){
 83                     if(Rear->next->coefficient + c)
 84                         Rear->next->coefficient += c;
 85                     else{
 86                         temp = Rear->next;
 87                         Rear->next = temp->next;
 88                         free(temp);
 89                     }
 90                 }else{
 91                     temp = (PolyNode*)malloc(sizeof(PolyNode));
 92                     temp->coefficient = c;
 93                     temp->exponent = e;
 94                     temp->next = Rear->next;
 95                     Rear->next = temp;
 96                     Rear = Rear->next;
 97                 }
 98                 temp2 = temp2->next;
 99             }
100         }
101         temp1 = temp1->next;
102     }
103     temp = P;
104     P = P->next;
105     free(temp);
106     return P;
107 }
108 
109 Polynomial AddPoly(Polynomial P1, Polynomial P2){
110     Polynomial P, temp1, temp2, Rear, temp;
111     if(!P1 && !P2){
112         if(!P1)
113             return P2;
114         else
115             return P1;
116     }
117     P = (PolyNode*)malloc(sizeof(PolyNode));
118     P->next = NULL;
119     Rear = P;
120     temp1 = P1;
121     temp2 = P2;
122     while(temp1 && temp2){
123         if(temp1->exponent > temp2->exponent){
124             if(temp1->coefficient){
125                 Attach(temp1->coefficient, temp1->exponent, &Rear);
126             }
127             temp1 = temp1->next;
128         }else if(temp1->exponent == temp2->exponent){
129             if(temp1->coefficient + temp2->coefficient){
130                 Attach(temp1->coefficient + temp2->coefficient, temp1->exponent, &Rear);
131             }
132             temp1 = temp1->next;
133             temp2 = temp2->next;
134         }else{
135             if(temp2->coefficient){
136                 Attach(temp2->coefficient, temp2->exponent, &Rear);
137             }
138             temp2 = temp2->next;
139         }
140     }
141     while(temp1){
142         Attach(temp1->coefficient, temp1->exponent, &Rear);
143         temp1 = temp1->next;
144     }
145     while(temp2){
146         Attach(temp2->coefficient, temp2->exponent, &Rear);
147         temp2 = temp2->next;
148     }
149     temp = P;
150     P = P->next;
151     free(temp);
152     return P;
153 }
154 void PrintPoly(Polynomial P){
155     int flag = 0;
156     if(!P){
157         printf("0 0");
158     }
159     while(P){
160         if (!flag)
161             flag = 1;
162         else
163             printf(" ");
164         printf("%d %d", P->coefficient, P->exponent);
165         P = P->next;
166     }
167     printf("\n");
168 }

测试结果:

 

posted @ 2017-06-15 23:14  在下雨的Tokyo  阅读(325)  评论(0)    收藏  举报