導航:首頁 > 數據分析 > 如何對鏈表中的數據加減

如何對鏈表中的數據加減

發布時間:2023-06-18 16:36:29

❶ 如何在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;

}

減法的自己改改,很簡單

❷ 如何在C++中用單鏈表實現多項式的加減乘等操作

這是以前做的一個
運行了的
#include <stdio.h>
#include <malloc.h>
#include<iostream.h>
#include<string.h>

typedef struct LNode //多項式的存儲結構定義
{
int coef;
int expn;
struct LNode *next;
}LNode,*polynomail;

void creatpolyn(polynomail &p,int m) //頭插法創建多項式
{
int i;
int coef;
int expn;
polynomail s;
p=(polynomail)malloc(sizeof(LNode));
p->next=NULL;

for(i=1;i<=m;++i)
{
s=(polynomail)malloc(sizeof(LNode));
cout<<"請輸入一元多項式的系數和指數:";
cin>>coef>>expn;
s->coef=coef;
s->expn=expn;
s->next=p->next;
p->next=s;
}
}

void printpolyn(polynomail p) //輸出多項式單鏈表的元素值
{
p=p->next;
while(p!=NULL)
{
if(p->coef!=0)
cout<<p->coef<<"X^"<<p->expn<<"+ ";
p=p->next;
}
cout<<endl;
}

/*void SortPloy(polynomail L) //升冪排序
{
polynomail p=L->next,q,r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
q=L;
while(q->next!=NULL&&q->next->expn<p->expn)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}*/

void SortPloy(polynomail L) //升冪排序
{
polynomail p=L->next,q=p->next;
int coef,expn;
while(p!=NULL&&p->next!=NULL)
{
while(q!=NULL)
{
if(p->expn>q->expn)
{
coef=p->coef;expn=p->expn;
p->coef=q->coef;p->expn=q->expn;
q->coef=coef;q->expn=expn;
}
q=q->next;
}
p=p->next;
q=p->next;
}
}

void polyvalue(polynomail p,int x0) //求多項式的值
{ int i,s=1,t,r=0;
p=p->next;
while(p!=NULL)
{
for(i=1;i<=p->expn;i++)
{ s=s*x0;
}
t=s*p->coef;
r+=t;
s=1;
p=p->next;
}
cout<<r<<endl;
}

polynomail AddPoly(polynomail pa,polynomail pb) /*求兩個多項式的和*/
{
SortPloy(pa);
SortPloy(pb);
polynomail rn,p1=pa->next,p2=pb->next,p,tc,s;
rn=(polynomail)malloc(sizeof(LNode)); /*新建頭結點*rn*/
rn->next=NULL; /*rn為新建單鏈表的頭結點*/
tc=rn; /*tc始終指向新建單鏈表的最後結點*/
while (p1!=NULL && p2!=NULL)
{
if (p1->expn<p2->expn) /*將*p1結點復制到*s並鏈到rn尾*/
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p1->coef;s->expn=p1->expn;s->next=NULL;
tc->next=s;tc=s;
p1=p1->next;
}
else if (p1->expn>p2->expn) /*將*p2結點復制到*s並鏈到rn尾*/
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p2->coef;s->expn=p2->expn;s->next=NULL;
tc->next=s;tc=s;
p2=p2->next;
}
else /*p1->expn=p2->expn的情況*/
{

s=(polynomail)malloc(sizeof(LNode));
s->coef=p1->coef+p2->coef;s->expn=p1->expn;
s->next=NULL;
tc->next=s;tc=s;

p1=p1->next;p2=p2->next;
}
}
if (p1!=NULL) p=p1; /*將尚未掃描完的餘下結點復制並鏈接到rn單鏈表之後*/
else p=p2;
while (p!=NULL)
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p->coef;s->expn=p->expn;s->next=NULL;
tc->next=s;tc=s;
p=p->next;
}
tc->next=NULL; /*新建單鏈表最後結點的next域置空*/
return rn;
}

polynomail DelPoly(polynomail pa,polynomail pb) /*求兩個多項式的差*/
{ SortPloy(pa);
SortPloy(pb);
polynomail tn,p1=pa->next,p2=pb->next,p,tc,s;
tn=(polynomail)malloc(sizeof(LNode)); /*新建頭結點*tn*/
tn->next=NULL; /*tn為新建單鏈表的頭結點*/
tc=tn; /*tc始終指向新建單鏈表的最後結點*/
while (p1!=NULL && p2!=NULL)
{
if (p1->expn<p2->expn) /*將*p1結點復制到*s並鏈到tn尾*/
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p1->coef;s->expn=p1->expn;s->next=NULL;
tc->next=s;tc=s;
p1=p1->next;
}
else if (p1->expn>p2->expn) /*將*p2結點復制到*s並鏈到tn尾*/
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=-p2->coef;s->expn=p2->expn;s->next=NULL;
tc->next=s;tc=s;
p2=p2->next;
}
else /*p1->expn=p2->expn的情況*/
{

s=(polynomail)malloc(sizeof(LNode));
s->coef=p1->coef-p2->coef;s->expn=p1->expn;
s->next=NULL;
tc->next=s;tc=s;

p1=p1->next;p2=p2->next;
}
}
if (p1!=NULL) p=p1; /*將尚未掃描完的餘下結點復制並鏈接到tn單鏈表之後*/
else
{ if(p2!=NULL)
{ s=(polynomail)malloc(sizeof(LNode));
s->coef=-p2->coef;s->expn=p2->expn;s->next=NULL;
tc->next=s;tc=s;
p2=p2->next;
p=p2;
}
else
{
p=NULL;
s->next=NULL;
tc->next=s;tc=s;
}

}
while (p!=NULL)
{
s=(polynomail)malloc(sizeof(LNode));
s->coef=p->coef;s->expn=p->expn;s->next=NULL;
tc->next=s;tc=s;
p=p->next;
}
tc->next=NULL; /*新建單鏈表最後結點的next域置空*/
return tn;
}

void save(polynomail p,int m) /*將多項式的系數和指數保存到外部文件中*/
{FILE *fp;
polynomail s;
char filename[10];
cout<<"請輸入要保存的文件名,例如:c:\\score\n";
cin>>filename;
if((fp=fopen(filename,"w+"))==NULL) /*為輸出打開一個二進制文件,為只寫方式*/
{
printf("打不開文件Cannot open the file\n");
return; /*若打不開則返回菜單*/
}
printf("\n保存中...Saving the file......\n");
s=(polynomail)malloc(sizeof(LNode));
s=p->next;
while(s!=NULL)
{
fprintf(fp,"%d,%d\n",s->coef,s->expn); /*寫入一條記錄*/
s=s->next;
}
fclose(fp); /*關閉文件*/
printf("保存成功....Save the file successfully!\n");
}

polynomail read(int m) /*從保存的外部文件中讀取多項式的系數和指數*/
{ int i;
int coef,expn;
char filename[20];
polynomail s,p;
p=(polynomail)malloc(sizeof(LNode));
p->next=NULL;
cout<<"請輸入要讀取的文件名,例如:c:\\score\n";
cin>>filename;
FILE* fp;
fp=fopen(filename, "r+");
for(i=1;i<=m;i++)
{s=(polynomail)malloc(sizeof(LNode));
fscanf(fp, "%d,%d", &coef, &expn);
cout<<coef<<","<<expn<<endl;
s->coef=coef;
s->expn=expn;
s->next=p->next;
p->next=s;
}
printpolyn(p);
return p;
}

void main()
{ int a=0;
while(a!=8)
{cout<<"--------------------菜單選項--------------------------------------------\n";
cout<<"******** 1、創建一元多項式Pn(x)和Qn(x) *********************************\n";
cout<<"******** 2、輸出一元多項式Pn(x)和Qn(x) *********************************\n";
cout<<"******** 3、對一元多項式Pn(x)和Qn(x)進行升冪排序 ***********************\n";
cout<<"******** 4、對一元多項式Pn(x)和Qn(x)進行求值 ***************************\n";
cout<<"******** 5、對一元多項式Pn(x)和Qn(x)進行求和、差 ***********************\n";
cout<<"******** 6、將一元多項式Pn(x)和Qn(x)的系數和指數保存到外部文件 *********\n";
cout<<"******** 7、讀取外部文件並創建一元多項式Pn(x)和Qn(x) *******************\n";
cout<<"******** 8、退出菜單 ***************************************************\n";
cout<<"------------------------------------------------------------------------\n";
cout<<"請輸入你要選擇的菜單選項:";
cin>>a;
switch(a)
{case 1: int m,n,x0,x1;
polynomail Pn,Qn,Rn,Tn;
cout<<"請輸入一元多項式Pn的項數:";
cin>>m;
creatpolyn(Pn,m);
cout<<"已創建一元多項式Pn\n";
cout<<"請輸入一元多項式Qn的項數:";
cin>>n;
creatpolyn(Qn,n);
cout<<"已創建一元多項式Qn\n";
break;

case 2: cout<<"原一元多項式Pn:";
printpolyn(Pn);
cout<<"原一元多項式Qn:";
printpolyn(Qn);
cout<<endl;
break;

case 3: SortPloy(Pn);
SortPloy(Qn);
cout<<"排序後的一元多項式Pn:";
printpolyn(Pn);
cout<<"排序後的一元多項式Qn:";
printpolyn(Qn);
cout<<endl;
break;

case 4: cout<<"請輸入一元多項式Pn中的x0的值:";
cin>>x0;
cout<<"一元多項式Pn(x0)的值:";
polyvalue(Pn,x0);
cout<<endl;
cout<<"請輸入一元多項式Qn中的x1的值:";
cin>>x1;
cout<<"一元多項式Qn(x1)的值:";
polyvalue(Qn,x1);
cout<<endl;
break;

case 5: Rn=AddPoly(Pn,Qn);
cout<<"一元多項式Pn,Qn相加結果Rn:";
printpolyn(Rn);
cout<<endl;
Tn=DelPoly(Pn,Qn);
cout<<"一元多項式Pn,Qn相減結果Tn:";
printpolyn(Tn);
cout<<endl;
break;

case 6: save(Pn,m);
save(Qn,n);
break;

case 7: cout<<"請輸入一元多項式Pn的項數:";
cin>>m;
Pn=read(m);
cout<<"請輸入一元多項式Qn的項數:";
cin>>n;
Qn=read(n);
break;

case 8: break;

default: cout<<"沒有該菜單選項,請重新選擇\n";
break;

}
}
}

閱讀全文

與如何對鏈表中的數據加減相關的資料

熱點內容
css商業網站布局之道pdf 瀏覽:892
c盤不能新建文件夾win10 瀏覽:384
angularjs左側菜單 瀏覽:180
tp引用js 瀏覽:463
怎麼用公式比對兩列數據 瀏覽:283
交保險的app有哪些 瀏覽:559
2017年蘋果5s可以買嘛 瀏覽:153
加密文件在什麼地方找不到了 瀏覽:676
網卡驅動文件夾 瀏覽:444
iphone6qq關聯賬號顯示台機 瀏覽:709
java文件名亂碼 瀏覽:553
什麼是網橋編程固件 瀏覽:732
jquery實現網站向導提示操作插件 瀏覽:257
java小游戲實例 瀏覽:775
電腦系統能升級64 瀏覽:591
數據如何導入進sql 瀏覽:324
iosqq怎麼發文件夾 瀏覽:285
編程出社會後能做什麼工作 瀏覽:73
為什麼說數據層是里子呢 瀏覽:171
eset官方卸載工具 瀏覽:803

友情鏈接