1 #include2 #include 3 #include 4 #include 5 #include 6 #define LIST_INIT_SIZE 100 7 using namespace std; 8 typedef struct{ 9 int *elem; 10 int length; 11 int listsize; 12 }Sqlist;//顺序存储结构; 13 14 15 16 17 typedef struct LNode{ 18 int data;//系数 19 int elem;//指数; 20 struct LNode *next; 21 22 }LNode,*LinkList;//动态存储; 23 24 25 void Polyomial_one_input(Sqlist &list)// 顺序存储结构多项式输入 26 {printf("\n%15s","");cout<<"请输入多项式最高次幂"; 27 int n; 28 cin>>n; 29 system("cls"); 30 printf("\n%15s","");cout<<"请输入多项式的各项系数(按指数降序)\n"; 31 int i,j,k; 32 int sum=0; 33 for(i=n;i>=0;i--) 34 {cin>>list.elem[i]; 35 if(list.elem[i]==0) 36 sum++; 37 } 38 list.length=n+1; 39 double x=sum,y=n+1; 40 system("cls"); 41 if(x/y>0.3) 42 {printf("\n%15s","*");cout<<"该多项式是稀疏多项式\n";} 43 else 44 {printf("\n%15s","*");cout<<"该多项式不是稀疏多项式\n";} 45 } 46 47 void Polyomial_one_add(Sqlist list_one,Sqlist list_two,Sqlist &list_three) 48 { 49 int i; 50 51 int n1=list_one.length-1,n2=list_two.length-1,n3=0; 52 53 for(i=0;((i<=n1)&&(i<=n2));i++) 54 {list_three.elem[i]=list_one.elem[i]+list_two.elem[i];} 55 for(i;i<=n1;i++) 56 list_three.elem[i]=list_one.elem[i]; 57 for(i;i<=n2;i++) 58 list_three.elem[i]=list_two.elem[i]; 59 list_three.length=i; 60 } 61 62 void Polyomial_one_multiply(Sqlist list_one,Sqlist list_two,Sqlist &list_three) 63 { 64 int i,j; 65 for(i=0;i n) 73 n=i+j; 74 } 75 list_three.length=n+1; 76 } 77 78 79 80 81 82 83 84 85 86 void Polyomial_one_output(Sqlist list_three) 87 { 88 int i; 89 cout<<"按指数升序:\n"; 90 int k=0; 91 for(i=0;i 0)102 {cout<<" + "< =0;i--)118 { if(list_three.elem[i]==0)119 continue;120 121 122 if(k==0)123 {cout< 0)128 {cout<<" + "< next=NULL;197 L1->data=0;198 L2=(LinkList)malloc(sizeof(LNode));199 L2->next=NULL;200 L2->data=0;201 L3=(LinkList)malloc(sizeof(LNode));202 L3->next=NULL;203 L3->data=0;204 205 printf("%15s","");cout<<"多项式1\n"; 206 Polyomial_two_input(L1);207 cout<<"多项式1:"; Polyomial_two_output(L1);208 printf("%15s","");209 system("PAUSE");210 system("cls");211 printf("%15s","");cout<<"多项式2\n"; 212 Polyomial_two_input(L2);cout<<"多项式2:";213 Polyomial_two_output(L2);214 system("PAUSE");215 system("cls");216 cout<<"多项式和为:\n";217 218 Polyomial_two_add(L1,L2,L3);219 Polyomial_two_output(L3);220 LinkList pp=L3->next,ppp;221 L3->next=NULL;222 while(pp)223 {ppp=pp;224 pp=pp->next;225 free(ppp);226 }227 system("PAUSE");228 system("cls");229 cout<<"多项式乘积为:\n";230 Polyomial_two_multiply(L1,L2,L3);231 Polyomial_two_output(L3);232 233 }234 235 236 void Polyomial_two_output(LinkList L)237 { int i,j;238 cout<<"按指数升序:\n";239 int k=0;240 LinkList p=L->next;241 while(p)242 { if(k==0)243 {cout< data;244 if(p->elem!=0)245 cout<<"X("< elem<<")";k++;}246 else247 { if(p->data>0)248 {cout<<" + "< data;249 if(p->elem!=0)250 cout<<"X("< elem<<")";}251 else252 {cout<<" - "< data*(-1);253 if(p->elem!=0)254 cout<<"X("< elem<<")";}255 k++;256 }257 p=p->next;258 }259 260 261 cout< data;i>0;i--)266 { j=1;p=L;267 while(j<=i)268 {p=p->next;j++;}269 270 271 272 if(k==0)273 {cout< data;274 if(p->elem!=0)275 cout<<"X("< elem<<")";k++;}276 else277 { if(p->data>0)278 {cout<<" + "< data;279 if(p->elem!=0)280 cout<<"X("< elem<<")";}281 else282 {cout<<" - "< data*(-1);283 if(p->elem!=0)284 cout<<"X("< elem<<")";}285 k++;286 }287 }288 289 290 291 292 293 }294 void Polyomial_two_multiply(LinkList L1,LinkList L2,LinkList &L3)295 {LinkList p,p1,p2,p3;296 297 298 for(p1=L1->next;p1;p1=p1->next)299 { for(p2=L2->next;p2;p2=p2->next)300 {301 302 for(p3=L3->next,p=L3;p3;p3=p3->next,p=p->next)303 if((p1->elem+p2->elem)<=p3->elem)304 break;305 if(!p3)306 {LinkList pp=(LinkList)malloc(sizeof(LNode));307 pp->next=NULL;308 pp->data=p1->data*p2->data;309 pp->elem=p1->elem+p2->elem;310 p->next=pp;311 }312 else313 { if(p3->elem==(p1->elem+p2->elem))314 p3->data+=(p1->data*p2->data);315 else316 {LinkList pp=(LinkList)malloc(sizeof(LNode));317 pp->next=NULL;318 pp->data=p1->data*p2->data;319 pp->elem=p1->elem+p2->elem;320 pp->next=p->next;321 p->next=pp;322 }323 }324 }}325 p3=L3->next;326 p=L3;327 L3->data=0;328 while(p3)329 { if(p3->data==0)330 {p->next=p3->next;331 free(p3);332 p3=p->next;}333 else334 { L3->data++;p3=p3->next;}335 }336 }337 338 339 340 341 342 343 344 345 346 347 348 void Polyomial_two_add(LinkList L1,LinkList L2,LinkList &L3)349 {LinkList p1,p2,p3;350 351 352 L3->data=0;353 p3=L3;354 for(p1=L1->next,p2=L2->next;p1&&p2;)355 { if(p1->elem elem)356 {p3->next=(LinkList)malloc(sizeof(LNode));357 p3=p3->next;358 p3->next=NULL;359 p3->data=p1->data;360 p3->elem=p1->elem;361 p1=p1->next;L3->data++;362 }363 else if(p1->elem>p2->elem)364 {p3->next=(LinkList)malloc(sizeof(LNode));365 p3=p3->next;366 p3->next=NULL;367 p3->data=p2->data;368 p3->elem=p2->elem;369 p2=p2->next;L3->data++;370 }371 else372 { int n=p1->data+p2->data;373 if(n)374 {p3->next=(LinkList)malloc(sizeof(LNode));375 p3=p3->next;376 p3->next=NULL;377 p3->data=n;378 p3->elem=p2->elem;379 p2=p2->next;p1=p1->next;L3->data++;}380 381 }382 }383 for(p1;p1;p1=p1->next)384 {p3->next=(LinkList)malloc(sizeof(LNode));385 p3=p3->next;386 p3->next=NULL;387 p3->data=p1->data;388 p3->elem=p1->elem;389 L3->data++;390 }391 for(p2;p2;p2=p2->next)392 {p3->next=(LinkList)malloc(sizeof(LNode));393 p3=p3->next;394 p3->next=NULL;395 p3->data=p2->data;396 p3->elem=p2->elem;397 L3->data++;398 }399 400 }401 void Polyomial_two_input(LinkList &L)402 {printf("\n%15s","");cout<<"请输入多项式最高次幂";403 int n;404 cin>>n; 405 system("cls");406 printf("\n%15s","");cout<<"请输入多项式的各项系数(按指数降序)\n";407 int i,k;408 409 LinkList p,p1;410 L->data=0;411 L->next=NULL;412 for(i=n;i>=0;i--)413 {cin>>k;414 if(k==0)415 continue;416 L->data++;417 p1=(LinkList)malloc(sizeof(LNode));418 p1->data=k;419 p1->elem=i;420 p1->next=NULL;421 p1->next=L->next;422 L->next=p1;423 }424 double x=L->data,y=n+1;425 system("cls");426 if(x/y>0.3)427 {printf("\n%15s","*");cout<<"该多项式不是稀疏多项式\n";}428 else429 {printf("\n%15s","*");cout<<"该多项式是稀疏多项式\n";}430 }431 432 433 434 435 436 437 438 int main(int argc, char *argv[])439 {printf("\n%15s","*");cout<<"*************************************************";440 printf("\n%15s","*");cout<<"****************一元多项式计算*******************\n";441 442 printf("\n%15s","*");cout<<"采用顺序结构请按1,动态存储结构请按2\n";443 printf("\n%15s","");444 int n;445 cin>>n;446 while(n<1||n>2)447 { printf("\n%15s","*");cout<<"输入错误,请重新输入";cin>>n;}448 system("cls");449 if(n==1)450 {Polyomial_one();}451 else452 {Polyomial_two();}453 454 system("PAUSE");455 return EXIT_SUCCESS;456 }457 458
#include <cstdlib>#include <iostream>#include <conio.h>#include <string.h>#include <stdio.h>#define LIST_INIT_SIZE 100using namespace std;typedef struct{ int *elem; int length; int listsize; }Sqlist;//顺序存储结构; typedef struct LNode{ int data;//系数 int elem;//指数; struct LNode *next; }LNode,*LinkList;//动态存储; void Polyomial_one_input(Sqlist &list)// 顺序存储结构多项式输入{printf("\n%15s","");cout<<"请输入多项式最高次幂"; int n; cin>>n; system("cls"); printf("\n%15s","");cout<<"请输入多项式的各项系数(按指数降序)\n"; int i,j,k; int sum=0; for(i=n;i>=0;i--) {cin>>list.elem[i]; if(list.elem[i]==0) sum++; } list.length=n+1; double x=sum,y=n+1; system("cls"); if(x/y>0.3) {printf("\n%15s","*");cout<<"该多项式是稀疏多项式\n";} else {printf("\n%15s","*");cout<<"该多项式不是稀疏多项式\n";}}void Polyomial_one_add(Sqlist list_one,Sqlist list_two,Sqlist &list_three){ int i; int n1=list_one.length-1,n2=list_two.length-1,n3=0; for(i=0;((i<=n1)&&(i<=n2));i++) {list_three.elem[i]=list_one.elem[i]+list_two.elem[i];} for(i;i<=n1;i++) list_three.elem[i]=list_one.elem[i]; for(i;i<=n2;i++) list_three.elem[i]=list_two.elem[i]; list_three.length=i;}void Polyomial_one_multiply(Sqlist list_one,Sqlist list_two,Sqlist &list_three){ int i,j;for(i=0;i<list_one.length+list_two.length;i++)list_three.elem[i]=0;list_three.length=0;int n=0;for(i=0;i<list_one.length;i++)for(j=0;j<list_two.length;j++){list_three.elem[i+j]+=list_one.elem[i]*list_two.elem[j];if((i+j)>n)n=i+j;}list_three.length=n+1;}void Polyomial_one_output(Sqlist list_three){ int i;cout<<"按指数升序:\n";int k=0;for(i=0;i<list_three.length;i++){ if(list_three.elem[i]==0) continue; if(k==0) {cout<<list_three.elem[i]; if(i!=0) cout<<"X("<<i<<")";k++;} else { if(list_three.elem[i]>0) {cout<<" + "<<list_three.elem[i]; if(i!=0) cout<<"X("<<i<<")";} else {cout<<" - "<<list_three.elem[i]*(-1); if(i!=0) cout<<"X("<<i<<")";} k++; } }cout<<endl;k=0;cout<<"按指数降序:\n";for(i=list_three.length-1;i>=0;i--){ if(list_three.elem[i]==0) continue; if(k==0) {cout<<list_three.elem[i]; if(i!=0) cout<<"X("<<i<<")";k++;} else { if(list_three.elem[i]>0) {cout<<" + "<<list_three.elem[i]; if(i!=0) cout<<"X("<<i<<")";} else {cout<<" - "<<list_three.elem[i]*(-1); if(i!=0) cout<<"X("<<i<<")";} k++; }}} void Polyomial_one()//顺序存储; { Sqlist list_one,list_two,list_three; list_one.elem=(int *)malloc(sizeof(int)*LIST_INIT_SIZE); list_one.length=0; list_one.listsize=LIST_INIT_SIZE; list_two.elem=(int *)malloc(sizeof(int)*LIST_INIT_SIZE); list_two.length=0; list_two.listsize=LIST_INIT_SIZE; list_three.elem=(int *)malloc(sizeof(int)*LIST_INIT_SIZE); list_three.length=0; list_three.listsize=LIST_INIT_SIZE; printf("%15s","");cout<<"多项式1"; Polyomial_one_input(list_one); cout<<"多项式1:"; Polyomial_one_output(list_one); printf("%15s",""); system("PAUSE"); system("cls"); printf("%15s","");cout<<"多项式2"; Polyomial_one_input(list_two);cout<<"多项式2:"; Polyomial_one_output(list_two); system("PAUSE"); system("cls");cout<<"多项式和为:\n"; Polyomial_one_add(list_one,list_two,list_three); Polyomial_one_output(list_three); system("PAUSE"); system("cls");cout<<"多项式乘积为:\n";Polyomial_one_multiply(list_one,list_two,list_three); Polyomial_one_output(list_three);} void Polyomial_two_output(LinkList L);void Polyomial_two_multiply(LinkList L1,LinkList L2,LinkList &L3);void Polyomial_two_add(LinkList L1,LinkList L2,LinkList &L3);void Polyomial_two_input(LinkList &L); void Polyomial_two()//动态存储;{LinkList L1,L2,L3;L1=(LinkList)malloc(sizeof(LNode));L1->next=NULL;L1->data=0;L2=(LinkList)malloc(sizeof(LNode));L2->next=NULL;L2->data=0;L3=(LinkList)malloc(sizeof(LNode));L3->next=NULL;L3->data=0;printf("%15s","");cout<<"多项式1\n"; Polyomial_two_input(L1); cout<<"多项式1:"; Polyomial_two_output(L1); printf("%15s",""); system("PAUSE"); system("cls"); printf("%15s","");cout<<"多项式2\n"; Polyomial_two_input(L2);cout<<"多项式2:"; Polyomial_two_output(L2); system("PAUSE"); system("cls");cout<<"多项式和为:\n"; Polyomial_two_add(L1,L2,L3); Polyomial_two_output(L3); LinkList pp=L3->next,ppp; L3->next=NULL; while(pp) {ppp=pp; pp=pp->next; free(ppp); } system("PAUSE"); system("cls");cout<<"多项式乘积为:\n";Polyomial_two_multiply(L1,L2,L3); Polyomial_two_output(L3);}void Polyomial_two_output(LinkList L){ int i,j;cout<<"按指数升序:\n";int k=0;LinkList p=L->next;while(p){ if(k==0) {cout<<p->data; if(p->elem!=0) cout<<"X("<<p->elem<<")";k++;} else { if(p->data>0) {cout<<" + "<<p->data; if(p->elem!=0) cout<<"X("<<p->elem<<")";} else {cout<<" - "<<p->data*(-1); if(p->elem!=0) cout<<"X("<<p->elem<<")";} k++; } p=p->next;} cout<<endl;k=0;cout<<"按指数降序:\n";for(i=L->data;i>0;i--){ j=1;p=L; while(j<=i) {p=p->next;j++;} if(k==0) {cout<<p->data; if(p->elem!=0) cout<<"X("<<p->elem<<")";k++;} else { if(p->data>0) {cout<<" + "<<p->data; if(p->elem!=0) cout<<"X("<<p->elem<<")";} else {cout<<" - "<<p->data*(-1); if(p->elem!=0) cout<<"X("<<p->elem<<")";} k++; }} }void Polyomial_two_multiply(LinkList L1,LinkList L2,LinkList &L3){LinkList p,p1,p2,p3; for(p1=L1->next;p1;p1=p1->next) { for(p2=L2->next;p2;p2=p2->next) { for(p3=L3->next,p=L3;p3;p3=p3->next,p=p->next) if((p1->elem+p2->elem)<=p3->elem) break; if(!p3) {LinkList pp=(LinkList)malloc(sizeof(LNode)); pp->next=NULL; pp->data=p1->data*p2->data; pp->elem=p1->elem+p2->elem; p->next=pp; } else { if(p3->elem==(p1->elem+p2->elem)) p3->data+=(p1->data*p2->data); else {LinkList pp=(LinkList)malloc(sizeof(LNode)); pp->next=NULL; pp->data=p1->data*p2->data; pp->elem=p1->elem+p2->elem; pp->next=p->next; p->next=pp; } }}}p3=L3->next;p=L3;L3->data=0;while(p3){ if(p3->data==0) {p->next=p3->next; free(p3); p3=p->next;} else { L3->data++;p3=p3->next;}}} void Polyomial_two_add(LinkList L1,LinkList L2,LinkList &L3){LinkList p1,p2,p3;L3->data=0;p3=L3;for(p1=L1->next,p2=L2->next;p1&&p2;){ if(p1->elem<p2->elem){p3->next=(LinkList)malloc(sizeof(LNode)); p3=p3->next; p3->next=NULL; p3->data=p1->data; p3->elem=p1->elem; p1=p1->next;L3->data++;}else if(p1->elem>p2->elem){p3->next=(LinkList)malloc(sizeof(LNode)); p3=p3->next; p3->next=NULL; p3->data=p2->data; p3->elem=p2->elem; p2=p2->next;L3->data++;}else{ int n=p1->data+p2->data; if(n) {p3->next=(LinkList)malloc(sizeof(LNode)); p3=p3->next; p3->next=NULL; p3->data=n; p3->elem=p2->elem; p2=p2->next;p1=p1->next;L3->data++;} }}for(p1;p1;p1=p1->next){p3->next=(LinkList)malloc(sizeof(LNode)); p3=p3->next; p3->next=NULL; p3->data=p1->data; p3->elem=p1->elem; L3->data++;}for(p2;p2;p2=p2->next){p3->next=(LinkList)malloc(sizeof(LNode)); p3=p3->next; p3->next=NULL; p3->data=p2->data; p3->elem=p2->elem; L3->data++; }}void Polyomial_two_input(LinkList &L){printf("\n%15s","");cout<<"请输入多项式最高次幂"; int n; cin>>n; system("cls"); printf("\n%15s","");cout<<"请输入多项式的各项系数(按指数降序)\n"; int i,k; LinkList p,p1;L->data=0;L->next=NULL; for(i=n;i>=0;i--) {cin>>k; if(k==0) continue; L->data++; p1=(LinkList)malloc(sizeof(LNode)); p1->data=k; p1->elem=i; p1->next=NULL; p1->next=L->next; L->next=p1; } double x=L->data,y=n+1; system("cls"); if(x/y>0.3) {printf("\n%15s","*");cout<<"该多项式不是稀疏多项式\n";} else {printf("\n%15s","*");cout<<"该多项式是稀疏多项式\n";}} int main(int argc, char *argv[]){printf("\n%15s","*");cout<<"*************************************************"; printf("\n%15s","*");cout<<"****************一元多项式计算*******************\n"; printf("\n%15s","*");cout<<"采用顺序结构请按1,动态存储结构请按2\n"; printf("\n%15s",""); int n; cin>>n; while(n<1||n>2) { printf("\n%15s","*");cout<<"输入错误,请重新输入";cin>>n;} system("cls"); if(n==1) {Polyomial_one();} else {Polyomial_two();} system("PAUSE"); return EXIT_SUCCESS;}