1 #include <iostream> 2 #include<stdlib.h> 3 4 using namespace std; 5 6 7 #define MAXSIZE 20 8 9 typedef struct LNode 10 { 11 int coef;//系数 12 int expn;//指数 13 struct LNode *next; 14 }LNode,*LinkList; 15 16 17 int CreateList_L(LinkList &L,int n) 18 { 19 LinkList p,q; 20 for(int i=0;i<n;i++) 21 { 22 p=(LinkList)malloc(sizeof(LNode)); 23 cout<<"输入元素系数:"; 24 cin>>p->coef; 25 cout<<"输入元素指数:"; 26 cin>>p->expn; 27 if(i==0)L=p; 28 else q->next=p; 29 q=p; 30 p->next=NULL; 31 } 32 return 0; 33 } 34 35 36 int LengthPolyn(LinkList L)//判断多项式的项数 37 { 38 int s=0; 39 while(L) 40 { 41 s++; 42 L=L->next; 43 } 44 return s; 45 } 46 47 48 int CopyList_L(LinkList La,LinkList &Lb)//开辟一段和La指向的空间一样的新的空间,并把La中各元素的值依次复制到Lb中 49 { 50 LinkList p,q,pa=La; 51 int n=LengthPolyn(La); 52 for(int i=0;i<n;i++) 53 { 54 p=(LinkList)malloc(sizeof(LNode)); 55 p->coef=pa->coef; 56 p->expn=pa->expn; 57 if(i==0)Lb=p; 58 else q->next=p; 59 q=p; 60 p->next=NULL; 61 pa=pa->next; 62 } 63 return 0; 64 } 65 66 67 68 int Display_L(LinkList L) 69 { 70 cout<<"f(x)="<<L->coef; 71 if(L->expn)cout<<"*"<<"x^"<<L->expn; 72 L=L->next; 73 while(L) 74 { 75 if(L->coef>0) 76 { 77 cout<<"+"; 78 cout<<L->coef<<"*"<<"x^"<<L->expn; 79 } 80 else cout<<L->coef<<"*"<<"x^"<<L->expn; 81 L=L->next; 82 } 83 return 0; 84 } 85 86 87 88 int AddPolyn(LinkList La,LinkList Lb,LinkList &Lc) 89 { 90 LinkList pa,pb,p; 91 int k=1; 92 pa=La;pb=Lb; 93 //确定“和多项式”的头结点 94 while(k) 95 { 96 if(pa->expn<pb->expn){p=Lc=pa;pa=pa->next;k=0;} 97 else if(pa->expn>pb->expn){p=Lc=pb;pb=pb->next;k=0;} 98 else 99 { 100 pa->coef+=pb->coef; 101 if(pa->coef) 102 { 103 p=Lc=pa; 104 pa=pa->next; 105 k=0; 106 } 107 else 108 { 109 pa=pa->next; 110 pb=pb->next; 111 if(!pa||!pb) break; 112 } 113 } 114 } 115 if(!pb&&pa){p=Lc=pa;pa=pa->next;} 116 if(!pa&&pb){p=Lc=pb;pb=pb->next;} 117 if(!pa&&!pb) Lc=NULL; 118 //确定接下来的节点 119 while(pa&&pb) 120 { 121 if(pa->expn<pb->expn){p->next=pa;p=pa;pa=pa->next;} 122 else if(pa->expn>pb->expn){p->next=pb;p=pb;pb=pb->next;} 123 else 124 { 125 pa->coef+=pb->coef; 126 if(pa->coef) 127 { 128 p->next=pa; 129 p=pa; 130 pa=pa->next; 131 pb=pb->next; 132 } 133 else 134 { 135 pa=pa->next; 136 pb=pb->next; 137 } 138 139 } 140 } 141 while(!pb&&pa) 142 { 143 p->next=pa; 144 p=pa; 145 pa=pa->next; 146 } 147 while(!pa&&pb) 148 { 149 p->next=pb; 150 p=pb; 151 pb=pb->next; 152 } 153 return 0; 154 } 155 156 157 158 int MultiplyPolyn(LinkList La,LinkList Lb,LinkList &Lc)//La指向的多项式乘以Lb指向的多项式 159 { 160 LinkList pa,pb=Lb,l; 161 int i=0,j,n; 162 LNode *L[MAXSIZE];//指针数组,Lb至多MAXSIZE项! 163 while(pb) 164 { 165 CopyList_L(La,L[i]); 166 pa=L[i]; 167 while(pa) 168 { 169 pa->coef*=pb->coef; 170 pa->expn+=pb->expn; 171 pa=pa->next; 172 } 173 pb=pb->next; 174 i++; 175 } 176 n=LengthPolyn(Lb); 177 AddPolyn(L[0],L[1],Lc); 178 for(j=2;j<n;j++) 179 { 180 AddPolyn(L[j],l,Lc); 181 l=Lc; 182 } 183 return 0; 184 } 185 186 187 188 int main() 189 { 190 LinkList La,Lb,Lc; 191 int m,n; 192 cout<<"请输入La中的元素个数:"; 193 cin>>m; 194 CreateList_L(La,m); 195 cout<<"请输入Lb中的元素个数:"; 196 cin>>n; 197 CreateList_L(Lb,n); 198 MultiplyPolyn(La,Lb,Lc); 199 Display_L(Lc); 200 return 0; 201 }
时间: 2024-10-19 10:14:40