?? yuan.cpp
字號:
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
struct node *creat();
struct node
{
float coef;
int exp;
struct node *next;
};
struct node *creat(char c)
{
struct node *r,*s,*head;
float l,m;
int n;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;//將頭節(jié)點(diǎn)指向空
cout<<"輸入多項(xiàng)式的系數(shù)與指數(shù)\n";
cout<<"coef=";
cin>>l;
cout<<"exp=";
cin>>n;
c=='+'?m=l:m=-l;//如果執(zhí)行減法,則將系數(shù)變?yōu)槠鹣喾磾?shù)(此語句是針對第二個是、多項(xiàng)式的)
while(l!=0)//當(dāng)輸入系數(shù)為0時結(jié)束輸出
{
s=(struct node*)malloc(sizeof(struct node));
s->coef=m;
s->exp=n;
s->next=NULL;//將當(dāng)前節(jié)點(diǎn)的想一個節(jié)點(diǎn)賦為空
if(head->next==NULL)head->next=s;//將節(jié)點(diǎn)插入頭節(jié)點(diǎn)后
else r->next=s;//將節(jié)點(diǎn)插入上一個節(jié)點(diǎn)后
r=s;
cout<<"輸入多項(xiàng)式的系數(shù)與指數(shù)\n";
cout<<"coef=";
cin>>l;
cout<<"exp=";
cin>>n;
c=='+'?m=l:m=-l;
}
return head;//返回頭節(jié)點(diǎn)
}
struct node *add( struct node *ha, struct node *hb)
{
struct node *p,*q,*r,*s,*head;
float x;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;//將頭節(jié)點(diǎn)指向空
p=ha;q=hb;
while (p!= NULL&&q!=NULL) //如果兩個多項(xiàng)式都不為空則執(zhí)行
{
if (p->exp==q->exp)//當(dāng)指數(shù)相等時
{
x=p->coef+q->coef;//系數(shù)相加
if(x!=0)
{
s=(struct node*)malloc(sizeof(struct node));
s->coef=x;
s->exp=p->exp;
s->next=NULL;
if(head->next==NULL)head->next=s;
else r->next=s;
r=s;
}
p=p->next;
q=q->next;
}
else
if(p->exp<q->exp)//前一個多項(xiàng)式相應(yīng)的項(xiàng)的指數(shù)小于后一個多項(xiàng)式
{
s=(struct node*)malloc(sizeof(struct node));
s->coef=p->coef;
s->exp=p->exp;
s->next=NULL;
if(head->next==NULL)head->next=s;
else r->next=s;
r=s;
p=p->next;
}
else//后一個多項(xiàng)式相應(yīng)的項(xiàng)的指數(shù)小于前一個多項(xiàng)式
{
s=(struct node*)malloc(sizeof(struct node));
s->coef=q->coef;
s->exp=q->exp;
s->next=NULL;
if(head->next==NULL)head->next=s;
else r->next=s;
r=s;
q=q->next;
}
}
while(p!=NULL)//當(dāng)只剩下第一個多項(xiàng)式時
{
s=(struct node*)malloc(sizeof(struct node));
s->coef=p->coef;
s->exp=p->exp;
s->next=NULL;
if(head->next==NULL)head->next=s;
else r->next=s;
r=s;
p=p->next;
}
while(q!=NULL)//當(dāng)只剩下第二個多項(xiàng)式時
{
s=(struct node*)malloc(sizeof(struct node));
s->coef=q->coef;
s->exp=q->exp;
s->next=NULL;
if(head->next==NULL)head->next=s;
else r->next=s;
r=s;
q=q->next;
}
return head;
}
value(struct node *pc)
{
float x,l,sum;int m;double n;
cout<<"輸入自變量x的值\n"<<"x=";
cin>>x;
sum=0.0;
while(pc!=NULL)
{
l=pc->coef;
m=pc->exp;
n=pow(x,m);
sum+=l*n;;
pc=pc->next;
}
return sum;
}
void main()
{
struct node *ha,*hb,*hc,*pc,*head;
char c,d,e;
float sum;
c='+';
cout<<" _________________\n";
cout<<" |一元多項(xiàng)式的運(yùn)算|\n";
cout<<" --————————\n";
cout<<"********************************************************************************\n";
cout<<"輸入第一個多項(xiàng)式(升冪順序輸入,以輸入系數(shù)為0是結(jié)束)\n";
cout<<"********************************************************************************\n";
ha=creat(c);
cout<<"********************************************************************************\n";
cout<<"輸入運(yùn)算符\n";
cin>>d;
while(d!='+'&&d!='-')
{
cout<<"輸入運(yùn)算符\n";
cin>>d;
}
cout<<"輸入第二個多項(xiàng)式(升冪順序輸入,以輸入系數(shù)為0是結(jié)束)\n";
cout<<"********************************************************************************\n";
hb=creat(d);
cout<<"********************************************************************************\n";
head=add(ha,hb);
hc=head->next->next;
cout<<"運(yùn)算結(jié)果是:(若為0則不輸出)\n";
cout<<"********************************************************************************\n";
while(hc!=NULL)//輸出計(jì)算后的多項(xiàng)式的個項(xiàng)
{
cout<<" coef="<<hc->coef;
cout<<" exp="<<hc->exp;
cout<<"\n";
hc=hc->next;
}
cout<<"********************************************************************************\n";
cout<<"你是否需要求值(需要請輸入y,若不需要,請輸入別的任何字符)\n";
cin>>e;
if(e=='y')
{
pc=head->next->next;
sum=value(pc);
cout<<"你要求的值為:\n"<<sum<<"\n";
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -