码迷,mamicode.com
首页 > 其他好文 > 详细

02-线性结构2 一元多项式的乘法与加法运算

时间:2018-10-12 17:45:23      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:大小   size   span   节点   数字   需要   null   结构   next   

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

输入格式:

输入分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<cstdio> 
  2 #include<cstdlib>
  3 
  4 typedef struct polynode *ptrtopolynode;
  5 typedef ptrtopolynode position;
  6 struct polynode {
  7     int c, e;
  8     position next;
  9 };
 10 typedef ptrtopolynode list;
 11 
 12 //多项式读入函数 
 13 list readpoly(){
 14     int n, c, e;
 15     scanf("%d", &n);
 16     list p = (list)malloc(sizeof(struct polynode));    
 17     p->next = NULL; 
 18     position rear = p;   
 19     while(n--){
 20         list temp = (list)malloc(sizeof(struct polynode));   
 21         scanf("%d%d", &temp->c , &temp->e);   
 22         temp->next = NULL;                   
 23         rear->next = temp;            
 24         rear = temp;             
 25     }
 26     list t=p; p=p->next; free(t);
 27     return p;
 28 }
 29 
 30 //多项式输出函数 
 31 void print(list p){
 32     int flag=0; 
 33     if(p==NULL) printf("0 0");     //零多项式特殊处理
 34     else{
 35         while(p!=NULL){
 36             if(flag==0) flag=1;
 37             else printf(" ");
 38             if(p->c==0&&p->e==0) flag=2;
 39             else printf("%d %d", p->c, p->e);
 40             p = p->next;
 41         }
 42     }
 43     printf("\n");
 44 }
 45 
 46 //根据指数的大小确定temp插入的位置 
 47 void attach(list p, list temp1){
 48     position pre=p, rear=p->next;
 49     while(rear->e>temp1->e){         //rear在第一个不大于temp指数的位置,或者链表末尾跳出循环 
 50         pre = rear;
 51         if(rear->next==NULL){
 52             rear=NULL;
 53             break;
 54         }
 55         rear = rear->next;
 56     }
 57     if(rear==NULL){                  
 58         temp1->next = NULL;
 59         pre->next = temp1;
 60     }
 61     else if(rear->e==temp1->e){      //同类项合并 
 62         rear->c += temp1->c;
 63         if(rear->c==0){
 64             pre->next = rear->next;   
 65         }
 66     }
 67     else{
 68         temp1->next = rear;
 69         pre->next = temp1;
 70     }
 71 }
 72 
 73 //多项式乘法函数 
 74 list multiply( list p1, list p2){
 75     list mult = (list)malloc(sizeof(struct polynode)); 
 76     mult->next = NULL;
 77     if(p1==NULL || p2==NULL){         //p1,p2有一个或以上为空,乘积一定为零多项式 
 78         mult=NULL;
 79     }
 80     else{                               
 81         position rear1, rear2;          
 82         rear1=p1; rear2=p2;
 83         while(rear1) {                 
 84             while(rear2){              
 85                 list temp = (list)malloc(sizeof(struct polynode));  
 86                 temp->c = rear1->c*rear2->c;
 87                 temp->e = rear1->e+rear2->e;
 88                 if(temp->c==0) temp->e=0;
 89                 if(mult->next==NULL){        //第一个节点插入到头节点之后  
 90                     temp->next =NULL;          
 91                     mult->next = temp;
 92                 }
 93                 else attach(mult, temp);
 94                 rear2 = rear2->next;
 95             }
 96             rear2 = p2;
 97             rear1 = rear1->next; 
 98         }
 99         list t = mult; mult = mult->next; free(t);
100     }
101     return mult;
102 }
103 
104 //加法实现函数 
105 list polysum(list p1, list p2){
106     list sum = (list)malloc(sizeof(struct polynode));
107     //两多项式其中之一是零多项式,则和等于另一多项式 
108     if(p1==NULL) sum->next=p2;                 
109     else if(p2==NULL) sum->next=p1;
110     else{
111         //两多项式均不为零多项式,只需要把p2逐个插入到p1中去,attach函数中已处理好同类项合并 
112         sum->next = p1;
113         for(list rear=p2; rear; rear=rear->next){ 
114             list temp = (list)malloc(sizeof(struct polynode));
115             temp->c = rear->c; temp->e = rear->e; temp->next = NULL;    
116             attach(sum, temp);
117         }
118     }
119     list t=sum; sum = sum->next; free(t);
120     return sum;
121 }
122 
123 //程序框架 
124 int main( ){   
125     list p1, p2, mult, sum;
126     p1 = readpoly();
127     p2 = readpoly();
128     mult = multiply(p1, p2);
129     sum = polysum(p1, p2);
130     print(mult);
131     print(sum);
132     return 0;
133 }

 

02-线性结构2 一元多项式的乘法与加法运算

标签:大小   size   span   节点   数字   需要   null   结构   next   

原文地址:https://www.cnblogs.com/shin0324/p/9779329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!