一元多项式的乘法与加法运算

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

输入格式:

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

在C语言中函数的参数传递方式有两种:值传递与地址传递。

值传递的特点是单向传递,即主调函数调用时给形参分配存储单元,把实参的值传递给形参,在调用结束后,形参的存储单元被释放,而形参值的任何变化都不会影响到实参的值,实参的存储单元仍保留并维持数值不变。

地址传递的特点是形参并不存在存储空间,编译系统不为形参数组分配内存。数组名或指针就是一组连续空间的首地址。因此在数组名或指针作函数参数时所进行的传送只是地址传送,形参在取得该首地址之后,与实参共同拥有一段内存空间,形参的变化也就是实参的变化。当使用C++时,可以使用&引用传递来达到同样效果。

 1 /*下面给一个C语言地址传递的例子*/
 2 void Swap(int *px, int *py)
 3 {
 4     int tmp;
 5     tmp = *px;
 6     *px = *py;
 7     *py = tmp;
 8     printf("*px = %d, *py = %d
", *px, *py);
 9 }
10 int main(void)
11 {
12     int a=10;
13     int b=20;
14     Swap(&a, &b);
15     printf("a = %d, b = %d
", a, b);
16     return 0;
17 }
18 
19 
20 /*运行结果:
21 *px = 20, *py = 10
22 a = 20, b = 10
23 */
原文地址:https://www.cnblogs.com/i-chase/p/13167284.html