数据结构题目---设计函数分别求两个一元多项式的乘积与和。


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

输入格式:

输入分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 
  4 typedef struct PolyNode* Polynomial;
  5 
  6 struct PolyNode{
  7     int coef;
  8     int expon;
  9     Polynomial next;
 10 };
 11 
 12 Polynomial ReadPoly();
 13 Polynomial Mult(Polynomial P1,Polynomial P2);
 14 void PrintPoly(Polynomial PP);
 15 Polynomial Add(Polynomial P1,Polynomial P2);
 16 
 17 int main() //程序框架搭建
 18 {
 19     Polynomial P1, P2, PP, PS;
 20     P1 = ReadPoly();
 21     P2 = ReadPoly();
 22 
 23     PP = Mult(P1, P2);
 24     PrintPoly(PP);
 25 
 26     PS = Add(P1, P2);
 27     PrintPoly(PS);
 28 
 29     return 0;
 30 }
 31 
 32 //对Rear指针的处理:1.初值为NULL,根据是否为空做不同处理;2.指向一个空节点
 33 //*pRear当前结果表达式尾项指针的指针
 34 //函数实现在pRear后面插入节点
 35 void Attach(int c, int e, Polynomial *pRear)  
 36 {
 37     Polynomial P;
 38     P = (Polynomial)malloc(sizeof(struct PolyNode));
 39     P->coef = c;
 40     P->expon = e;
 41     P->next = NULL;
 42     (*pRear)->next = P;
 43     *pRear = P;
 44 }
 45 
 46 Polynomial ReadPoly()
 47 {
 48     Polynomial P, Rear, t;
 49     int c, e, N;
 50     scanf("%d", &N);
 51     P = (Polynomial)malloc(sizeof(struct PolyNode)); //链表头空节点
 52     P->next = NULL;
 53     Rear = P; 
 54     while (N--)
 55     {
 56         scanf("%d %d", &c, &e);
 57         if (c != 0)  
 58            Attach(c, e, &Rear); //将当前项插入多项式尾部
 59     }
 60     t = P;
 61     P = P->next;
 62     free(t); //删除临时生成的头结点
 63     return P;
 64 }
 65 
 66 Polynomial Add(Polynomial P1, Polynomial P2)
 67 {
 68     Polynomial t1, t2;
 69     t1 = P1;
 70     t2 = P2;
 71     //生成新的头结点
 72     Polynomial P,t;
 73     P = (Polynomial)malloc(sizeof(struct PolyNode));
 74     P->next = NULL;
 75     Polynomial Rear;
 76     Rear = P;
 77     while (t1&&t2)
 78     {
 79         if (t1->expon==t2->expon)
 80         {
 81             if (t1->coef+t2->coef)  //系数和为0时不添加到尾节点上  /*考虑周全*/
 82             {
 83                 Attach(t1->coef + t2->coef, t1->expon, &Rear);
 84             }
 85             t1 = t1->next;
 86             t2 = t2->next;
 87         }
 88         else if (t1->expon>t2->expon)
 89         {
 90             Attach(t1->coef, t1->expon, &Rear);
 91             t1=t1->next;
 92         }else 
 93         {
 94             Attach(t2->coef, t2->expon, &Rear);
 95             t2 = t2->next;
 96         }
 97     }
 98     while (t1)
 99     {
100         Attach(t1->coef, t1->expon, &Rear);
101         t1 = t1->next;
102     }
103     while (t2)
104     {
105         Attach(t2->coef, t2->expon, &Rear);
106         t2 = t2->next;
107     }
108     t = P;
109     P = P->next;
110     free(t);
111     return P;
112 }
113 
114 //多项式乘法方法:
115 //1. 将乘法运算转换为加法运算,让P1的每一项和P2相乘,在加到结果多项式中
116 //2. 逐项插入,将P1当前项乘P2当前项,在插入到结果表达式中,关键是要找到插入的位置
117 
118 Polynomial Mult(Polynomial P1, Polynomial P2)
119 {
120     Polynomial P, Rear;
121     Polynomial t1, t2, t;
122     if (!P1||!P2)
123     {
124         return NULL;
125     }
126     t1 = P1;
127     t2 = P2;
128     P = (Polynomial)malloc(sizeof(struct PolyNode));
129     Rear = P;
130     while (t2)
131     {
132         //先用P1的第一项乘以P2,得到初始结果多项式
133         Attach(t1->coef*t2->coef, t1->expon + t2->expon, &Rear);
134         t2 = t2->next;
135     }
136     t1 = t1->next;
137     while (t1)
138     {
139         t2 = P2;
140         Rear = P; //将尾节点置到头结点来
141         while (t2)
142         {
143             int e = t1->expon + t2->expon;
144             int c = t1->coef * t2->coef;   //以后的每一项相乘的结果
145             while (Rear->next&&Rear->next->expon>e) //找插入位置
146             {
147                 Rear = Rear->next;
148             }
149             if (Rear->next&&Rear->next->expon==e)
150             {
151                 if (Rear->next->coef+c) //判系数是否为0
152                 {
153                     Rear->next->coef += c;
154                 }
155                 else  //为0删除节点
156                 {
157                     t = Rear->next;
158                     Rear->next = t->next;
159                     free(t);
160                 }
161             }
162             else  //插入位置
163             {
164                 t = (Polynomial)malloc(sizeof(struct PolyNode));
165                 t->coef = c;
166                 t->expon = e;
167                 t->next = Rear->next;
168                 Rear->next = t;
169 
170                 Rear = Rear->next;
171             }
172             t2 = t2->next;
173         }
174         t1 = t1->next;
175     }
176     t2 = P;
177     P = P->next;
178     free(t2);
179 
180     return P;
181 }
182 
183 void PrintPoly(Polynomial P)
184 {
185     int flag = 0;//辅助调整输出格式
186     if (!P)
187     {
188         printf("0 0\n"); /*格式*/
189         return;
190     }
191     while (P)
192     {
193         if (!flag) //第一次
194         {
195             flag = 1;
196         }
197         else
198         {
199             printf(" ");
200         }
201         printf("%d %d", P->coef, P->expon);
202         P = P->next;
203     }
204     printf("\n");
205 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM