紧急求救:如何用c语言版编写有两个一元多项式相减操作

liangch13 2007-10-09 04:20:23
设有两个一元多项式:
p(x)=p0+p1x+p2x2+•••+pnxn
q(x)=q0+q1x+q2x2+•••+qmxm
请问如何使用链表中的插入和删除操作来实现两个一元多项式的相减。怎么使用c语言编写呢?
请各位高手赐教!谢谢!
...全文
692 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
没遇到过这种情况.
meiZiNick 2008-05-01
  • 打赏
  • 举报
回复
以后需再关注,现在先帮你顶一下
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
什么呀
missilery 2007-10-14
  • 打赏
  • 举报
回复
数据结构书中有多项式加法
mapper 2007-10-09
  • 打赏
  • 举报
回复
看的头昏
freesnail 2007-10-09
  • 打赏
  • 举报
回复

3 稀疏多项式运算 加,减,乘除(链表实现)
/* 返 回 值:结果多项式的链表头指针 */
/************************************************************/
Polyn AddPolyn(Polyn h1,Polyn h2)
{
Item *head,*last,*pa=h1->next,*pb=h2->next,*s;
double coef;
CreateItem(head);
last=head;
head->next=NULL;
while(pa&&pb)
{if(pa->expn==pb->expn)
{
coef=pa->coef+pb->coef;
if(coef!=0)
{
CreateItem(s);
s->next=NULL;
s->coef=coef;
s->expn=pa->expn;
}
pa=pa->next;
pb=pb->next;
}
else if(pa->expn<pb->expn)
{
CreateItem(s);
s->next=NULL;
s->coef=pa->coef;
s->expn=pa->expn;
pa=pa->next;
}
else
{
CreateItem(s);
s->next=NULL;
s->coef=pb->coef;
s->expn=pb->expn;
pb=pb->next;
}
if(head->next==NULL)
{
head->next=s;
last=s;
}
else
{
last->next=s;
last=s;
}
}
while(!pa||!pb)
{
if(!pa&&pb)
{
CreateItem(s);
s->next=NULL;
s->coef=pb->coef;
s->expn=pb->expn;
pb=pb->next;
}
else if(pa&&!pb)
{
CreateItem(s);
s->next=NULL;
s->coef=pa->coef;
s->expn=pa->expn;
pa=pa->next;
}
else break;
last->next=s;
last=s;
}
return head;
}
/************************************************************/
/* 两多项式多项式相减 */
/* 函数格式:Polyn AddPolyn(Polyn h1,Polyn h2) */
/* 功 能:实现两个多项式的相减运算 */
/* 参 数:Polyn h1--第一个多项式的链表头指针 */
/* Polyn h2--第二个多项式的链表头指针 */
/* 返 回 值:结果多项式的链表头指针 */
/************************************************************/
Polyn SubtractPolyn(Polyn h1,Polyn h2)
{ int flag;
Item *head,*last,*pa=h1->next,*pb=h2->next,*s;
double coef;
CreateItem(head);
last=head;
last=head;
head->next=NULL;
s=NULL;
while(pa&&pb)
{if(pa->expn==pb->expn)
{
coef=pa->coef-pb->coef;
if(coef!=0.0)
{
CreateItem(s);
s->next=NULL;
s->coef=coef;
s->expn=pa->expn;
}
pa=pa->next;
pb=pb->next;
}
else if(pa->expn<pb->expn)
{
CreateItem(s);
s->next=NULL;
s->coef=pa->coef;
s->expn=pa->expn;
pa=pa->next;
}
else
{
CreateItem(s);
s->next=NULL;
s->coef=0.0-pb->coef;
s->expn=pb->expn;
pb=pb->next;
}
if(head->next==NULL)
{
head->next=s;
last=s;
}
else
{
last->next=s;
last=s;
}
}
while(!pa||!pb)
{
if(!pa&&pb)
{
CreateItem(s);
s->next=NULL;
s->coef=0.0-pb->coef;
s->expn=pb->expn;
pb=pb->next;
}
else if(pa&&!pb)
{
CreateItem(s);
s->next=NULL;
s->coef=pa->coef;
s->expn=pa->expn;
pa=pa->next;
}
else break;
last->next=s;
last=s;
}

return head;
}
/************************************************************/
/* 两多项式多项式相乘 */
/* 函数格式:Polyn AddPolyn(Polyn h1,Polyn h2) */
/* 功 能:实现两个多项式的相乘运算 */
/* 参 数:Polyn h1--第一个多项式的链表头指针 */
/* Polyn h2--第二个多项式的链表头指针 */
/* 返 回 值:结果多项式的链表头指针 */
/************************************************************/



4 稀疏多项式运算 加,减,乘除(链表实现)
Polyn MultPolyn(Polyn h1,Polyn h2) /*两个多项式相乘*/
{ int item,expn;
Item *head,*pa=h1->next,*pb=h2->next,*s,*p,*q,*h,*temp;
double coef;
h=pa;
CreateItem(head);
head->next=NULL;

while(pb)
{
CreateItem(p); /*P为构造的辅助链表的头结点*/
q=p; /*q为插入结点标志*/
while(pa)
{
CreateItem(s);
s->next=NULL;
s->coef=pa->coef*pb->coef;
s->expn=pa->expn+pb->expn;
q->next=s;
q=s;
pa=pa->next;
}
temp=AddPolyn(head,p);
Destroy(head);
head=temp;
Destroy(p);
pa=h;
pb=pb->next;
}
return head;
}

/************************************************************/
/* 菜单选择 */
/* 函数格式:int menu(void) */
/* 功 能:选择要操作的内容 */
/* 参 数:(无) */
/* 返 回 值:一个整数,表示操作内容的编码 */
/************************************************************/
int menu(void)
{ int num;
clrscr();
printf("%20c1--create P(x)\n",' ');
printf("%20c2--create Q(x)\n",' ');
printf("%20c3--p(x)+Q(x)\n",' ');
printf("%20c4--P(x)-Q(x)\n",' ');
printf("%20c5--p(x)*Q(x)\n",' ');
printf("%20c6--print P(x)\n",' ');
printf("%20c7--print Q(x)\n",' ');
printf("%20c8--print P(x)+Q(x)\n",' ');
printf("%20c9--print P(x)-Q(x)\n",' ');
printf("%20c10--print P(x)*Q(x)\n",' ');
printf("%20c11--Quit\n",' ');
printf(" please select 1,2,3,4,5,6,7,8,9,10,11:");
do{
scanf("%d",&num);
}while(num<1 || num>11);
return(num);
}
/************************************************************/
/* 判断多项式是否存在 */
/* 函数格式:int PolynNotEmpty(Polyn h,char *p) */
/* 功 能:判断多项式是否存在,标志:头指针是否为空 */
/* 参 数:Polyn h--表示多项式的链表头指针 */
/* char *p--提示信息,多项式的名字 */
/* 返 回 值:1--存在,0--不存在 */
/************************************************************/
int PolynNotEmpty(Polyn h,char *p)
{ if(h==NULL)
{ printf("%s is not exist!\n",p);
getchar();
return(0);
}
else return(1);
}
/************************************************************/
/* 主函数 */
/************************************************************/
void main()
{ int num;
Polyn h1=NULL; /*p(x)*/
Polyn h2=NULL; /*Q(x) */
Polyn h3=NULL; /*P(x)+Q(x)*/
Polyn h4=NULL; /*P(x)-Q(x) */
Polyn h5=NULL; /*P(x)*Q(x) */
while(1)
{ num=menu();
getchar();
switch(num)
{
case 1: /*输入第一个多项式,若多项式存在,首先撤消然后再输入 */
if(h1!=NULL)
{ if(Select("P(x) is not Empty,Create P(x) again?"))
{ Destroy(h1);
h1=Input();
}
}
else h1=Input();
break;
case 2: /*输入第二个多项式,若多项式存在,首先撤消然后再输入 */
if(h2!=NULL)
{ if(Select("Q(x) is not Empty,Create Q(x) again?"))
{ Destroy(h2);
h2=Input();
}
}
else h2=Input();
break;
case 3: /*两多项式相加 */
if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)"))
{h3=AddPolyn(h1,h2);
Output(h3,"P(x)+Q(X)");
printf("P(x)+Q(x) has finished!\n");
getchar();
}
break;
case 4: /*两多项式相减 */
if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)"))
{ h4=SubtractPolyn(h1,h2);
Output(h4,"P(x)-Q(x)");
printf("P(x)-Q(x) has finished!\n");
getchar();
}
break;
case 5: /*两多项式相乘 */
if(PolynNotEmpty(h1,"p(x)")&&PolynNotEmpty(h2,"Q(X)"))
{ h5=MultPolyn(h1,h2);
Output(h5,"P(x)*Q(x)");
printf("P(x)*Q(x) has finished!\n");
getchar();
}
break;
case 6: /*显示第一个多项式 */
if(PolynNotEmpty(h1,"p(x)"))
{ Output(h1,"P(x)");
getchar();
}
break;
case 7: /*显示第二个多项式 */
if(PolynNotEmpty(h2,"Q(x)"))
{ Output(h2,"Q(x)");
getchar();
}
break;
case 8: /*显示相加结果多项式 */
if(PolynNotEmpty(h3,"P(x)+Q(x)"))
{ Output(h3,"P(x)+Q(x)");
getchar();
}
break;
case 9: /*显示相减结果多项式 */
if(PolynNotEmpty(h4,"P(x)-Q(x)"))
{ Output(h4,"P(x)-Q(x)");
getchar();
}
break;
case 10: /*显示相乘结果多项式 */
if(PolynNotEmpty(h5,"P(x)*Q(x)"))
{ Output(h5,"P(x)*Q(x)");
getchar();
}
break;
case 11: /*结束程序运行。结束前应先撤消已存在的多项式 */
if(h1!=NULL) Destroy(h1);
if(h2!=NULL) Destroy(h2);
if(h3!=NULL) Destroy(h3);
if(h4!=NULL) Destroy(h4);
if(h4!=NULL) Destroy(h4);
if(h4!=NULL) Destroy(h5);
}
if(num==11) break;
}
getch();
}
freesnail 2007-10-09
  • 打赏
  • 举报
回复
你看看这个吧


1 稀疏多项式运算 加,减,乘除(链表实现)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct Item{
double coef;
int expn;
struct Item *next;
}Item,*Polyn;
#define CreateItem(p) p=(Item *)malloc(sizeof(Item));
#define Delete(p) free((void *)p);
int menu(void);
int PolynNotEmpty(Polyn h,char *p);
/************************************************************/
/* 判断选择函数 */
/* 函数格式:int Select(char *str) */
/* 功 能:根据str提示的内容判断是执行指定的操作 */
/* 参 数:char *str-将要确定的内容 */
/* 返 回 值:1-表示执行指定的操作 */
/* 0-表示不执行指定的操作 */
/************************************************************/
int Select(char *str)
{ char ch;
printf("%s\n",str);
printf("Input Y or N:");
do{ ch=getch();
}while(ch!='Y'&&ch!='y'&&ch!='N'&&ch!='n');
printf("\n");
if(ch=='Y'||ch=='y') return(1);
else return(0);
}
/************************************************************/
/* 插入位置定位函数 */
/* 函数格式:int InsertLocate(Polyn h,int expn,Item **p) */
/* 功 能:确定指数值为expn的项在链表中的位置 */
/* 参 数:Polyn h-要查找的多项式链表的头指针 */
/* int expn-新插入项的指数值 */
/* Item **p-插入位置的前驱结点指针 */
/* 返 回 值:-1-参数p带回的值为指数值小于expn的结点指针 */
/* 0-参数p带回的值为指数值等于expn的结点指针 */
/* 1-参数p带回最后一个结点的指针 */
/************************************************************/
int InsertLocate(Polyn h,int expn,Item **p)
{ Item *pre,*q;
pre=h;
q=h->next;
while(q&&q->expn<expn)
{ pre=q;
q=q->next;
}
if(!q)
{ *p=pre;
return(1);
}
else if(q->expn==expn)
{ *p=q;
return(0);
}
else
{ *p=pre;
return(-1);
}
}
/************************************************************/
/* 插入结点函数 */
/* 函数格式:void insert(Item *pre,Item *p) */
/* 功 能:在指定结点pre后插入一个新结点p */
/* 参 数:Item *pre-被插入结点的前驱结点 */
/* Item *p-要插入的新结点 */
/* 返 回 值:无 */
/************************************************************/
void insert(Item *pre,Item *p)
{
p->next=pre->next;
pre->next=p;
}
/************************************************************/
/* 输入多项式 */
/* 函数格式:Polyn Input(void) */
/* 功 能:从键盘输入多项式的系数和指数 */
/* 参 数:(无) */
/* 返 回 值:指向表示多项式的链表头指针 */
/************************************************************/

2 稀疏多项式运算 加,减,乘除(链表实现)
Polyn Input(void)
{
double coef;
int expn,flag;
Item *h,*p,*q,*pp;
CreateItem(h);/*产生头结点*/
h->next=NULL;
printf("input coef and expn(if end ,expn=-1)\n");
while(1)
{
scanf("%lf%d",&coef,&expn); /*输入多项式的系数和指数 */
if(expn==-1) break; /*若指数为-1,表示输入结束 */
if(InsertLocate(h,expn,&pp))/*返回值非0表示插入新结点 */
{ CreateItem(p);
p->coef=coef;
p->expn=expn;
insert(pp,p);
}
else if(Select("has the same expn,Replace older value?"))
pp->coef=coef; /*指数相同,替换系数 */
}
return h;
}
/************************************************************/
/* 撤消多项式 */
/* 函数格式:void Destroy(Item *h) */
/* 功 能:释放多项式的所有结点空间 */
/* 参 数:Polyn h--表示多项式链表的头指针 */
/* 返 回 值:(无) */
/************************************************************/
void Destroy(Polyn h)
{
Item *p=h,*q;
while(p!=NULL)
{
q=p;
p=p->next;
Delete(q);
}
}
/************************************************************/
/* 输出多项式 */
/* 函数格式:void Output(Polyn h,char *title) */
/* 功 能:按一定格式输出多项式 */
/* 参 数:Polyn h--表示多项式链表的头指针 */
/* char *title--字符串,表示多项式的名字 */
/* 返 回 值:(无) */
/************************************************************/
void Output(Polyn h,char *title)
{
int flag=1;
Item *p=h->next;
printf("%s=",title);
while(p)
{ if(flag) /*表示是否是多项式的第一项*/
{ flag=0;
if(p->expn==0) printf("%.2lf",p->coef);
else printf("%.2lfx^%d",p->coef,p->expn);
}
else
{ if(p->coef>0) printf("+");
if(p->expn==0) printf("%.2lf",p->coef);
else printf("%.2lfx^%d",p->coef,p->expn);
}
p=p->next;
}
printf("\n");
}
/************************************************************/
/* 判断两个多项式项的关系 */
/* 函数格式:int ItemComp(Item *x,Item *y) */
/* 功 能:根据两个多项式项的指数判断它们的关系 */
/* 参 数:Item x,Item y--两个多项式项 */
/* 返 回 值:-1--x小于y,0--x等于y,1--x大于y */
/************************************************************/
int ItemComp(Item *x,Item *y)
{ if(x->expn<y->expn)
return(-1);
else if(x->expn==y->expn)
return(0);
else return(1);
}
/************************************************************/
/* 两多项式多项式相加 */
/* 函数格式:Polyn AddPolyn(Polyn h1,Polyn h2) */
/* 功 能:实现两个多项式的相加运算 */
/* 参 数:Polyn h1--第一个多项式的链表头指针 */
/* Polyn h2--第二个多项式的链表头指针 */

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧