#include"iostream"#include"stdio.h"#include"string.h"#include"algorithm"#include"Queue"#include"Stack"#include"ctype.h"#include"Cmath"#defineMX 1005using namespacestd;structPoly//linked list node data type{ DoubleCeof//coefficient intExp//IndexPoly *next;};DoubleA[MX],B[MX];//Store A and B expressionsintAdegree,bdegree,maxexp;//storage of the highest ordervoidInit (poly *&PL)//Linked list Initialization{PL=NewPoly//dynamically allocate space, output allocate error if errors occur if(Pl==null) {cout<<"Allocate error!"<<Endl;} Else{PL->next=NULL; }}voidInput (Poly *&PL,intdegree,DoubleX[])//input to the expression (in the linked list){Poly*t=PL; intI=0; while(i<=degree) { if(x[i]!=0)//Zero of the coefficients is not stored{T->next=NewPoly; T=t->Next; T->ceof=X[i]; T->exp=i; } I++; } t->next=NULL;}voidOutput (poly *&PL)//output of an expression{Poly*t=pl->Next; cout<<"The Polynomal is:"; BOOLH=true; while(t!=NULL) { if(!h&&t->exp>0.0) cout<<"+"; H=false; cout<<t->ceof; Switch(T->EXP)//output the corresponding item according to the order number { Case 0: Break; Case 1:cout<<"x"; Break; default:cout<<"x^"<<t->exp; } t=t->Next; } cout<<Endl;}voidMul_poly (poly *&pla,poly *&plb,poly *&PLC)//implementing an expression multiplication{ DoubleRESULT[MX];//The coefficients of the resulting expression used to record the multiplication of a quantity expressionPoly *ta,*TB; inti,k; if(adegree!=-1|| bdegree!=-1)//consider the case of 0 polynomial{maxexp=adegree+Bdegree; for(i=0; i<maxexp;i++) result[i]=0.0; Ta=pla->Next; while(ta!=NULL) {TB=plb->Next; while(tb!=NULL) {k=ta->exp+tb->exp; RESULT[K]+ = (ta->ceof) * (tb->ceof); TB=tb->Next; } ta=ta->Next; } input (Plc,maxexp,result); }}intMain () {Poly*pla,*plb,*plc; intI,j,_exp; Double_ceof; Charch; intcase_=0; while(++case_) {cout<<" Case"<<case_<<":"<<Endl; memset (A,0,sizeof(A)); memset (B,0,sizeof(B)); Init (PLA);//initialization, this operation must haveinit (PLB); Init (PLC); CH='0'; cout<<"input A Poly:"; while(ch!='\ n')//input of a expression{cin>>_ceof; GetChar (); GetChar (); CIN>>_exp; CH=GetChar (); A[_EXP]=_ceof; Adegree=_exp; } input (Pla,adegree,a); cout<<"input B Poly:"; CH='0'; while(ch!='\ n')//input of the B-expression{cin>>_ceof; GetChar (); GetChar (); CIN>>_exp; CH=GetChar (); B[_EXP]=_ceof; Bdegree=_exp; } input (PLB,BDEGREE,B); Mul_poly (PLA,PLB,PLC); Output (PLC);//Output Final Resultcout<<Endl; } return 0;}
View Code
The multiplication of one-element polynomial is realized.