資料結構課程設計一元稀疏多項式計算器
#include<stdio.h>
#include<malloc.h>
#include<iostream.h>
typedef struct Polynomial
{
float coef;
int exp;
struct Polynomial *next;
}*Poly,Polynomial;
void Insert(Poly p,Poly head) //插入
{
if(p->coef==0) //係數為0時釋放結點
free(p);
else
{
Poly q1,q2;
q1=head;
q2=head->next;
while(q2&&p->exp<q2->exp) //後移查詢插入位置
{
q1=q2;
q2=q2->next;
}
if(q2&&p->exp==q2->exp) //將指數相同的相合並
{
q2->coef+=p->coef;
free(p);
if(!q2->coef) //係數為0時釋放結點
{
q1->next=q2->next;
free(q2);
}
//p->next=q2;
//q1->next=p;
}
else
{
p->next=q2;
q1->next=p;
}
}
}
Poly CreateList(Poly head,int m) //建立一個頭指標為head、項數為m的一元多項式
{
int i;
Poly p;
Polynomial *q;
p=head=(Poly)malloc(sizeof(struct Polynomial));
head->next=NULL;
for(i=0;i<m;i++)
{
p=(Poly)malloc(sizeof(struct Polynomial)); //建立新結點以接收資料
cout<<"請輸入第"<<i+1<<"項的係數與指數: "<<endl;
cin>>p->coef>>p->exp;
Insert(p,head); //呼叫Insert函式插入結點
}
return head;
}
int OutputList(Poly P) //輸出多項式
{
Poly q=P->next;
int flag=1; //項數計數器
if(!q)
{ //若多項式為空,輸出0
cout<<" 0 "<<endl;
return(0);
}
while (q)
{
if(q->coef>0&&flag!=1) //係數大於0且不是第一項
cout<<"+";
if(q->coef!=1&&q->coef!=-1) //係數非1或-1的普通情況
{
cout<<q->coef;
if(q->exp==1)
cout<<"X";
else if(q->exp)
cout<<"X^"<<q->exp;
}
else
{
if(q->coef==1)
{
if(!q->exp)
cout<<"1";
else if(q->exp==1)
cout<<"X";
else if(q->exp)
cout<<"X^"<<q->exp;
}
if(q->coef==-1)
{
if(!q->exp) cout<<"-1";
else if(q->exp==1) cout<<"-X";
else cout<<"-X^"<<q->exp;
}
}
q=q->next;
flag++;
}
cout<<endl;
return(0);
}
int compare(Poly a,Poly b)
{
if(a&&b)
{
if(!a||a->exp>b->exp) return 1;
else if(!b||a->exp<b->exp) return -1;
else return 0;
}
else if(!a&&b) return -1; //a多項式已空,但b多項式非空
else return 1; //b多項式已空,但a多項式非空
}
Poly AddPoly(Poly pa,Poly pb) //求解並建立和多項式a+b,返回其頭指標
{
Poly qa=pa->next;
Poly qb=pb->next;
Poly headc,hc,qc;
hc=(Poly)malloc(sizeof(struct Polynomial)); //建立頭結點
hc->next=NULL;
headc=hc;
while(qa||qb)
{
qc=(Poly)malloc(sizeof(struct Polynomial));
switch(compare(qa,qb))
{
case 1:
{
qc->coef=qa->coef;
qc->exp=qa->exp;
qa=qa->next;
break;
}
case 0:
{
qc->coef=qa->coef+qb->coef;
qc->exp=qa->exp;
qa=qa->next;
qb=qb->next;
break;
}
case -1:
{
qc->coef=qb->coef;
qc->exp=qb->exp;
qb=qb->next;
break;
}
}
if(qc->coef!=0)
{
qc->next=hc->next;
hc->next=qc;
hc=qc;
}
else free(qc); //當相加係數為0時,釋放該結點
}
return headc;
}
int main()
{
int m,n,flag=0;
float x;
Poly pa=0,pb=0,pc,pd,pe,pf; //定義各式的頭指標,pa與pb在使用前付初值NULL
cout<<" 輸入多項式a和b: "<<endl;
cout<<"請輸入a的項數:" ; //建立多項式a
cin>>m;
if(!cin)
{
cout<<endl<<endl;
cout<<"輸入為小數英文或者其他字元,輸入錯誤,退出!";
cout<<endl<<endl;
}
pa=CreateList(pa,m);
cout<<"請輸入b的項數:" ; //建立多項式b
cin>>n;
if(!cin)
{
cout<<endl<<endl;
cout<<"輸入為小數英文或者其他字元,輸入錯誤,退出!";
cout<<endl<<endl;
}
pb=CreateList(pb,n);
cout<<endl<<endl;
cout<<"多項式a和b為:"<<endl;
OutputList(pa);
OutputList(pb);
cout<<"***************************************"<<endl<<endl;
cout<<" 1.輸出多項式a "<<endl;
cout<<" 2.輸出多項式b "<<endl;
cout<<" 3.輸出多項式a+b "<<endl;
cout<<" 0.退出程式 "<<endl<<endl;
cout<<"***************************************"<<endl;
for(;;flag=0)
{
cout<<"執行操作為:" ;
cin>>flag;
if(flag==1)
{
if(pa)
{
cout<<"多項式a為:";
OutputList(pa);
}
else
{
cout<<"0"<<endl;
}
continue;
}
if(flag==2)
{
cout<<"多項式b為:";
OutputList(pb);
continue;
}
if(flag==3)
{
pc=AddPoly(pa,pb);
cout<<"多項式a+b為:";
OutputList(pc);
continue;
}
if(flag==0)
break;
if(flag<0||flag>7)
cout<<"輸入錯誤!!!請重新選擇!!";
continue;
}
return 0;
}