计算数列1 x-x^2 的和 C语言,数据结构一元多项式相加求C语言程序:f(X)=x^10+x^7+x^3+1加g(x)=x^5+3x^3+x^2+1...

以前写的代码.#include

using namespace std;

#define max 10000000

struct node

{

float coef;

int exp;

node *next;

};

struct Poly

{

char name;

node *other;

}poly[max];

int count;//poly的个数;

//生成多项式的函数

void creat(node *&L)

{

node *p,*q;

L=new node;

q=L;

float a;

int b;

while(cin>>a>>b)

{

if((a==0&&b<0)||b<0) break;

p=new node;

p->coef=a;

p->exp=b;

q->next=p;

q=p;

}

q->next=NULL;

}

//输入多项式

void inputing()

{

char c,d;

node *L;

do

{

cout<

cin>>c;

cin.get();

if(c=='Y')

{

count++;

cout<

cin>>d;

cin.get();

poly[count].name=d;

cout<

creat(L);

poly[count].other=L->next;

}

}while(c=='Y');

}

//两个多项式的加法

void adding(int i1,int i2,node *&L)

{

node *L1=poly[i1].other;

node *L2=poly[i2].other;

L=new node;

node *r=L,*s;

while(L1!=NULL&&L2!=NULL)

{

s=new node;

if(L1->expexp)

{

s->coef=L1->coef;

s->exp=L1->exp;

r->next=s;

r=s;

L1=L1->next;

}

else if(L1->exp>L2->exp)

{

s->coef=L2->coef;

s->exp=L2->exp;

r->next=s;

r=s;

L2=L2->next;

}

else

{

s->coef=L1->coef+L2->coef;

s->exp=L1->exp;

r->next=s;

r=s;

L1=L1->next;

L2=L2->next;

}

}

while(L1!=NULL)

{

s=new node;

s->coef=L1->coef;

s->exp=L1->exp;

r->next=s;

r=s;

L1=L1->next;

}

while(L2!=NULL)

{

s=new node;

s->coef=L2->coef;

s->exp=L2->exp;

r->next=s;

r=s;

L2=L2->next;

}

r->next=NULL;

}

//多项式的运算

void operating()

{

int n,i;

char c1,c2;

node *L;

printf("1--两个多项式之和\n2--两个多项式之差\n3--数乘\n4--求导\n0--放弃\n");

cout<

cin>>n;

cin.get();

if(n==1||n==2)

{

if(n==1) cout<

if(n==2) cout<

count++;

cin>>poly[count].name>>c1>>c1>>c2>>c2;

int i1,i2;

for(i=0;i

{

if(c1==poly.name) i1=i;

if(c2==poly.name) i2=i;

}

if(n==2)

{

node *s=poly[i2].other;

while(s!=NULL)

{

s->coef=-s->coef;

s=s->next;

}

}

adding(i1,i2,L);

if(n==2)

{

node *s=poly[i2].other;

while(s!=NULL)

{

s->coef=-s->coef;

s=s->next;

}

}

poly[count].other=L->next;

}

else if(n==3)

{

node *p,*r,*q;

cout<

count++;

cin>>poly[count].name>>c1>>n>>c1>>c2;

for(i=0;i

if(c2==poly.name) break;

L=poly.other;

q=new node;

r=q;

while(L!=NULL)

{

p=new node;

p->coef=n*(L->coef);

p->exp=L->exp;

r->next=p;

r=p;

L=L->next;

}

r->next=NULL;

poly[count].other=q->next;

}

else if(n==4)

{

node *p,*q,*r;

cout<

count++;

cin>>poly[count].name>>c1>>c1>>c2;

for(i=0;i

if(c1==poly.name) break;

L=poly.other;

q=new node;

r=q;

while(L!=NULL)

{

p=new node;

if(L->exp>0)

{

p->exp=L->exp-1;

p->coef=(L->coef)*(L->exp);

r->next=p;

r=p;

}

L=L->next;

}

r->next=NULL;

poly[count].other=q->next;

}

}

//输出多项式

void display()

{

int i,k,t;

node *p;

for(i=0;i<=count;i++)

{

t=0;

cout<

p=poly.other;

k=-1;

while(p!=NULL)

{

t=1;

k++;

if(k==0&&p->exp==0) cout<coef;

else

{

if(p->coef>=0&&k!=0)

cout<

cout<coef<exp;

}

p=p->next;

}

if(t==0) cout<

cout<

}

}

解析看不懂?求助智能家教解答查看解答

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值