當前位置:編程學習大全網 - 源碼下載 - 請問大哥如何用C#鏈表實現壹元多項式的加減呀,給個源代碼吧

請問大哥如何用C#鏈表實現壹元多項式的加減呀,給個源代碼吧

#include<iostream>

using namespace std;

#include<stdlib.h>

typedef int ElemType;

struct Pnomial //Pnomial=Polynomial(多項式)

{

ElemType co,de1,de2,de3;

//co=coefficient(系數), de=degree(次數)

Pnomial* next;

};

Pnomial *ADD(Pnomial *ph);

void mul(Pnomial *ph,Pnomial *qh);

void main()

{

Pnomial *ph,*qh,*p,*s,*q;

//ph表頭指針,p移動指針,q臨時儲存結點

ph=p=new Pnomial;

qh=q=new Pnomial;

cout<<"\nthe 1st Polynomial:"<<endl;

cout<<"give coefficient value:"<<endl;

while(p->co!=0)

{

s=new Pnomial;

cin>>s->co;

if(s->co!=0)

{

cout<<"x^";

cin>>s->de1;

cout<<"y^";

cin>>s->de2;

cout<<"z^";

cin>>s->de3;

cout<<"+";

}

p->next=s;

p=s;

}

cout<<"\nthe 1st Polynomial end."<<endl;

cout<<"\nthe 2nd Polynomial:"<<endl;

cout<<"give coefficient value:"<<endl;

do

{

s=new Pnomial;

cin>>s->co;

if(s->co!=0)

{

cout<<"x^";

cin>>s->de1;

cout<<"y^";

cin>>s->de2;

cout<<"z^";

cin>>s->de3;

cout<<"+";

}

q->next=s;

q=s;

} while(q->co!=0);

cout<<"the 2nd Polynomial end."<<endl;

p->next=NULL;

p=ph->next;

q->next=NULL;

q=qh->next;

cout<<'\n'<<endl;

cout<<"the 1st Polynomial:"<<endl;

while(p->next!=NULL)

{

if(p!=ph->next)

cout<<" + ";

if(p->co!=1)

cout<<p->co;

if(p->de1!=0)

{

cout<<"x";

if(p->de1!=1)

cout<<"^"<<p->de1;

}

if(p->de2!=0)

{

cout<<"y";

if(p->de2!=1)

cout<<"^"<<p->de2;

}

if(p->de3!=0)

{

cout<<"z";

if(p->de3!=1)

cout<<"^"<<p->de3;

}

p=p->next;

}

cout<<"\nthe 1st Polynomial end."<<endl;

cout<<"\nthe 2nd Polynomial:"<<endl;

while(q->next!=NULL)

{

if(q!=qh->next)

cout<<" + ";

if(q->co!=1)

cout<<q->co;

if(q->de1!=0)

{

cout<<"x";

if(q->de1!=1)

cout<<"^"<<q->de1;

}

if(q->de2!=0)

{

cout<<"y";

if(q->de2!=1)

cout<<"^"<<q->de2;

}

if(q->de3!=0)

{

cout<<"z";

if(q->de3!=1)

cout<<"^"<<q->de3;

}

q=q->next;

}

cout<<"\nthe 2nd Polynomial end."<<endl;

cout<<'\n'<<endl;

mul(ph,qh);

}

void mul(Pnomial *ph,Pnomial *qh)

{

Pnomial *p,*q;

Pnomial *re,*di,*temp; //新建壹個鏈表儲存結果

//re=result(結果), di=displace(移動指針)

int counter=0;

//計數變量,記錄p賦值給temp的起始結點

re=di=new Pnomial;

for(p=ph->next;p->next!=NULL;p=p->next)

{

for(q=qh->next;q->next!=NULL;q=q->next)

{

temp=new Pnomial;

temp->co=p->co*q->co;

temp->de1=p->de1+q->de1;

temp->de2=p->de2+q->de2;

temp->de3=p->de3+q->de3;

di->next=temp;

di=temp;

if(p==ph->next&&q==qh->next)

re=di;

}

}

di->next=NULL;

cout<<"the result Polynomial:"<<endl;

di=ADD(re);

//di回到表頭結點,準備打印結果多項式

// di=re->next;//di回到表頭結點,準備整理多項式

re=di;

while(di!=NULL)

{

if(di!=re)

cout<<" + ";

if(di->co!=1)

cout<<di->co;

if(di->de1!=0)

{

cout<<"x";

if(di->de1!=1)

cout<<"^"<<di->de1;

}

if(di->de2!=0)

{

cout<<"y";

if(di->de2!=1)

cout<<"^"<<di->de2;

}

if(di->de3!=0)

{

cout<<"z";

if(di->de3!=1)

cout<<"^"<<di->de3;

}

di=di->next;

}

}

Pnomial *ADD(Pnomial *ph)

{

Pnomial *re,*p,*q;

p=ph;

q=p->next;

Pnomial *di,*temp; //新建壹個鏈表儲存結果

//re=result(結果), di=displace(移動指針)

re=di=new Pnomial;

while(p!=NULL)

{

temp=new Pnomial;

temp->co=p->co;

temp->de1=p->de1;

temp->de2=p->de2;

temp->de3=p->de3;

for(;q!=NULL;)

{

if(p->de1==q->de1&&p->de2==q->de2&&p->de3==q->de3)

{

temp->co+=q->co;

p->next=q->next;

delete q;

q=p->next;

}

else

q=q->next;

}

di->next=temp;

di=temp;

if(p==ph)

re=di;

p=p->next;

if(p!=NULL)

q=p->next;

}

di->next=NULL;

return re;

}

減法的自己改改,很簡單

  • 上一篇:android 怎麽自定popupwindow設置高度無反應
  • 下一篇:請問,當前比較新的,比較先進的施工現場安全管理方法或是措施有哪些?麻煩大俠們幫忙賜教壹下啊,謝謝
  • copyright 2024編程學習大全網