| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4500 人关注过本帖
标题:本人的一元多项式加减乘
取消只看楼主 加入收藏
chllcy
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2006-10-7
收藏
 问题点数:0 回复次数:0 
本人的一元多项式加减乘

做的比较粗略,希望大家指点一下 还有一个输入x的值输出多项式的结果
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include "math.h"
using namespace std;
typedef struct
{
double coef;
int expn;
}elemtype;
typedef struct lnode
{
elemtype data;
struct lnode * next;
}lnode,*linklist;
linklist creat() //输入并建立多项式
{
char ch;int i=1;
elemtype _data;
linklist head=new lnode,p,r=head;
ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"请输入第"<<i<<"项系数: ";
cin>>_data.coef;
cout<<"请输入第"<<i<<"项指数: ";
cin>>_data.expn;
p=new lnode;
(p->data).coef=_data.coef;
(p->data).expn=_data.expn;
r->next=p;
r=p;i++;
cout<<"是否还要输入项,要则输入Y,否则按任意其他键退出 ";
cin>>ch;
}
r->next=NULL;
return(head);
}

void del(linklist head)
{
linklist p=head,q=head;
while(q!=NULL)
{
p=q; q=p->next; delete p;
}
}
void swap(linklist &head)//按降序排,并合并指数相同的项
{
lnode m,*p=head->next,*q,*z,*r;
int i,j,count=0;double sum=0;
while(p!=NULL)
{
count++;
p=p->next;

}
for(i=1;i<count;i++)
{
p=head->next;q=p->next;
for(j=0;j<count-i;j++)
{
if(((p->data).expn)<((q->data).expn))
{
(m.data).expn=(q->data).expn;
(m.data).coef=(q->data).coef;
(q->data).expn=(p->data).expn;
(q->data).coef=(p->data).coef;
(p->data).expn=(m.data).expn;
(p->data).coef=(m.data).coef;

}
p=p->next;q=q->next;
}
}
p=head->next;r=head;
while(p!=NULL)
{
q=p->next;z=p;sum=(p->data).coef;
while(q!=NULL)
{
if((p->data).expn==(q->data).expn)
{sum+=(q->data).coef;z->next=q->next;delete q;q=z->next;}
else
{z=q;q=q->next;}
}
if(sum!=0)
{(p->data).coef=sum;r=p;p=p->next;}
else
{r->next=p->next;delete p;p=r->next;}
}

}

void print(const linklist head) //输出多项式,序列按指数降序排列
{
lnode *p=head->next;
if(p!=NULL)
{
if((p->data).coef!=0)
cout<<(p->data).coef<<"x^"<<(p->data).expn;
p=p->next;
while(p!=NULL)
{
if(((p->data).coef)>0)
cout<<'+';
if((p->data).coef!=0)
{
if((p->data).expn>=0)
cout<<(p->data).coef<<"x^"<<(p->data).expn;
else
cout<<(p->data).coef<<"x^("<<(p->data).expn<<')';
}
p=p->next;
}
}cout<<endl;
}
int cmp(int &a,int &b)
{
if(a>b)
return 1;
else if(a==b)
return 0;
else if(a<b)
return -1;
}
linklist add(linklist m,linklist n)
{
linklist head=new lnode;
linklist p=m->next,q=n->next,x,y;//利用x,y创建另一个链表存两个链表的和
x=head;
elemtype a,b; double sum=0;
if(p==NULL)
print(q);
else if(q==NULL)
print(p);
while(p&&q)
{
a=(p->data); b=(q->data);y=new lnode;x->next=y;x=y;
switch(cmp(a.expn,b.expn))
{
case 1:(y->data).expn=(p->data).expn;
(y->data).coef=(p->data).coef;p=p->next;break;
case 0:sum=a.coef+b.coef;
if(sum!=0)
{(y->data).coef=sum;(y->data).expn=(p->data).expn;}
else
{(y->data).expn=0;
(y->data).coef=0;}
q=q->next;p=p->next;
break;
case -1:(y->data).expn=(q->data).expn;
(y->data).coef=(q->data).coef;q=q->next;break;
}
}
while(q!=NULL)
{
y=new lnode;x->next=y;x=y;
(y->data).expn=(q->data).expn;
(y->data).coef=(q->data).coef;
q=q->next;
}
while(p!=NULL)
{
y=new lnode;x->next=y;x=y;
(y->data).expn=(p->data).expn;
(y->data).coef=(p->data).coef;
p=p->next;
}
x->next=NULL;
swap(head);
return head;
}


linklist sub(linklist m,linklist n)
{
linklist p=m->next,q=n->next,x,y;//利用x,y创建另一个链表存两个链表的和
linklist head=new lnode;x=head;
elemtype a,b; double sum=0;
if(p==NULL)
print(q);
else if(q==NULL)
print(p);
while(p&&q)
{
a=(p->data); b=(q->data);y=new lnode;x->next=y;x=y;
switch(cmp(a.expn,b.expn))
{case 1:(y->data).expn=(p->data).expn;
(y->data).coef=(p->data).coef;p=p->next;break;
case 0:sum=a.coef-b.coef;
if(sum!=0)
{(y->data).coef=sum;(y->data).expn=(p->data).expn;}
else
{(y->data).expn=0;
(y->data).coef=0;}
q=q->next;p=p->next;
break;
case -1:(y->data).expn=(q->data).expn;
(y->data).coef=-(q->data).coef;q=q->next;break;
}
}

while(q!=NULL)
{
y=new lnode;x->next=y;x=y;
(y->data).expn=(q->data).expn;
(y->data).coef=-(q->data).coef;
q=q->next;
}
while(p!=NULL)
{
y=new lnode;x->next=y;x=y;
(y->data).expn=(p->data).expn;
(y->data).coef=(p->data).coef;
p=p->next;
}
x->next=NULL;swap(head);
return head;
}
void result(linklist &head,const double& x)
{
double sum=0,temp=0;
linklist p=head->next;
while(p!=NULL)
{
temp=((p->data).coef)*pow(x,(p->data).expn);
sum+=temp;
p=p->next;
}
cout<<"第1个多项式结果为: "<<sum<<endl;
}
linklist mul(linklist m,linklist n)
{
linklist head=new lnode;
linklist p=m->next,q=n->next,x,y,z,r=head;
x=head;int count=0,i,j;
double sum=0;
while(p!=NULL)
{
for(q=n->next;q!=NULL;q=q->next)
{
// count++;
y=new lnode;x->next=y;x=y;
(y->data).coef=(p->data).coef*(q->data).coef;
(y->data).expn=(p->data).expn+(q->data).expn;
}
p=p->next;
}
x->next=NULL;
swap(head);
return head;
}
int _tmain(int argc, _TCHAR* argv[])
//int main(int argc, char *argv[])
{
linklist x1,x2;double x;
cout<<"请输入第一个多元二项式"<<endl;
x1=creat();
swap(x1);
print(x1);
cout<<"请输入第二个多元二项式"<<endl;
x2=creat();
swap(x2);
print(x2);
print(add(x1,x2));
del(add(x1,x2));
print(sub(x1,x2));
del(sub(x1,x2));
cout<<"请输入x的值:";
cin>>x;
result(x1,x);
print(mul(x1,x2));
del(mul(x1,x2));
del(x1);del(x2);//删除建立起来存储一元多项式的链表
getch();
return 0;
}

搜索更多相关主题的帖子: 多项式 
2006-10-17 18:25
快速回复:本人的一元多项式加减乘
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016807 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved