niithub

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

输入格式:

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

================================================================================
第一次code:
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5 
  6 /*
  7     定义链表结构 
  8 */
  9 typedef struct Node *Ptrnomial;
 10 typedef Ptrnomial LinkList;  //头结点
 11 typedef Ptrnomial Position;//中间节点
 12 struct Node
 13 {
 14     int coef;  /*系数*/
 15     int expon;  /*指数*/ 
 16     Position link;
 17 };
 18 
 19 LinkList Read();
 20 void Print(LinkList L);
 21 LinkList Multiplication(LinkList L1,LinkList L2);    /*乘法*/
 22 LinkList Add(LinkList L1,LinkList L2);  /*加法*/
 23  
 24 int main(void) 
 25 {
 26     LinkList L1,L2,L3,L4;
 27     L1 = Read();
 28     L2 = Read();
 29     L3 = Add(L1,L2); 
 30     L4 = Multiplication(L1,L2);
 31     Print(L4);
 32     printf("\n");
 33     Print(L3);
 34     return 0;
 35 }
 36 LinkList Read()
 37 {
 38     int n,c,e;
 39     scanf("%d",&n);
 40     LinkList L,Rear,p;
 41     L = (Ptrnomial)malloc(sizeof(struct Node));
 42     Rear = L;
 43     while(n--)
 44     {
 45         p = (Ptrnomial)malloc(sizeof(struct Node));
 46         scanf("%d %d",&c,&e);
 47         p->coef = c;
 48         p->expon = e;
 49         Rear->link = p;
 50         Rear = p;
 51     } 
 52     Rear->link = NULL;
 53     return L;
 54 }
 55 void Print(LinkList L)
 56 {
 57     LinkList L1;
 58     L1 = L->link;
 59     int flag = 0;
 60     if( !L1 )    /*意思是L1为空时*/
 61     {
 62         printf("0 0");
 63         return ;
 64     }
 65     while( L1 )  /*意思是L1不为空时*/
 66     {
 67         if(!flag)
 68         {
 69             flag = 1;
 70         }
 71         else
 72         {
 73             printf(" ");
 74         }
 75         printf("%d %d",L1->coef,L1->expon);
 76         L1 = L1->link;
 77     }
 78 }
 79 /*多项式相乘*/ 
 80 LinkList Multiplication(LinkList L1,LinkList L2)
 81 {
 82     Position l1,l2;
 83     LinkList l,temp,r,p;
 84     l1 = L1->link;
 85     l2 = L2->link;
 86     l = (struct Node*)malloc(sizeof(struct Node));
 87     l->link = NULL;
 88     if( !l1 || !l2)
 89     {
 90         return l;
 91     }
 92     while( l1 )
 93     {
 94         temp = (struct Node*)malloc(sizeof(struct Node));
 95         r = temp;
 96         l2 = L2->link;
 97         while( l2 )
 98         {
 99             p = (struct Node*)malloc(sizeof(struct Node));
100             p->expon = l1->expon + l2->expon;
101             p->coef = l1->coef * l2->coef;
102             l2 = l2->link; 
103             r->link = p;
104             r = p;
105         }
106         r->link = NULL;
107         l = Add(l,temp);
108         l1 = l1->link;
109     }
110     return l; 
111 }
112 /*多项式相加*/
113 LinkList Add(LinkList L1,LinkList L2)
114 {
115     int sum;
116     Position l1, l2;
117     LinkList l,r,p;
118     l1 = L1->link;
119     l2 = L2->link;
120     l = (struct Node*)malloc(sizeof(struct Node));
121     r = l;
122     while( l1 && l2)
123     {
124         /*当两个多项式都存在时*/
125         p = (struct Node*)malloc(sizeof(struct Node));
126         if(l1->expon == l2->expon)
127         {
128             /*系数相等时*/
129             sum = l1->coef + l2->coef;
130             if( sum )
131             {
132                 p->expon = l1->expon;
133                 p->coef = sum;
134                 r->link = p;
135                 r = p;
136             }
137             l2 = l2->link;
138             l1 = l1->link;
139         }
140         else if(l1->expon < l2->expon)
141         {
142             p->expon = l2->expon;
143             p->coef = l2->coef;
144             l2 = l2->link;
145             r->link = p;
146             r = p;
147         }    
148         else
149         {
150             p->expon = l1->expon;
151             p->coef = l1->coef;
152             l1 = l1->link;
153             r->link = p;
154             r = p;
155         }
156     }    
157     if( l2 == NULL)
158     {
159         while( l1 )
160         {
161             p = (struct Node*)malloc(sizeof(struct Node));
162             p->expon = l1->expon;
163             p->coef = l1->coef;
164             l1 = l1->link;
165             r->link = p;
166             r =p ;
167         }
168     }
169     if( l1 == NULL)
170     {
171         while( l2 )
172         {
173             p = (struct Node*)malloc(sizeof(struct Node));
174             p->expon = l2->expon;
175             p->coef = l2->coef;
176             l2 = l2->link;
177             r->link = p;
178             r =p ;
179         }
180     }
181     r->link = NULL;
182     return l;
183 }

posted on 2016-09-19 21:17  niithub  阅读(283)  评论(1编辑  收藏  举报