尾插法建立一元多项式的链表
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验目的用尾插法建立一元多项式的链表验证是否实现并进行两个多项式的相加运算需求分析本程序用tc编写完成尾插法一元多项式的链表并验证及完成相加运算
实验报告
2009级数学与应用数学班2011年11月19日
姓名:学号:
1、实验目的
用尾插法建立一元多项式的链表,验证是否实现,并进行两个多项式的相加运算
2、需求分析
rear=s;
scanf("%d,ቤተ መጻሕፍቲ ባይዱd",&c,&e);
}
rear->next=NULL;
return(head);
}
main()
{
printf("%d",1+1);}
用尾插法建一个一元多项式链表
Void polyadd(Polylist polya,Polylist polyb)
{
Polynode *p,*q,*tail,*temp; int sum;
本程序用TC编写,完成尾插法一元多项式的链表,并验证,及完成相加运算。
1输入的形式和输入值的范围:输入多项式每一项的系数、指数,输入的所有系数和指数均为整数。
2输入的形式:输出多项式各项的系数、指数。
3程序所能达到的功能:完成单链表的建立,及相加操作。
4测试数据:1+1
多项式1: 2x^0+3x^3+7x^5
#define TURE 1;
#define FALSE 0;
typedef struct Polynode
{
int coef;
int exp;
struct Polynode *next;
}Polynode ,*Polylist;
Polylist polycreate()
{
Polynode *head,*rear,*s;
P=polya->next; q=polyb->next; tail=polya;
While(p!=NULL&&q!=NULL)
{
If(p->exp<q->exp)
{tail->exp=p;tail=p;p=p->next;}
Else if(p->exp==q->exp)
{sum=p->coef+q->coef;
If(sum!=0)
{p->coef=sum;tail->next=p;tail=p;
P=p->next;temp=q;q=q->next;free(temp);}
else
{temp=p;p=p->next;free(temp);
Temp=q;q=q->next;free(temp);
}
}
else
{tail->next=q;tail=q;q=q->next;}
}
If (p!=NULL) tail->next=p;
else tail->next=q;
}
多项式相加函数
Void printf (polylist p)
{
While (p!=NULL)
{printf (“%dx^%d+”,p->exp);
P=p->next;}
}
多项式输出函数
5、测试结果
2
2,0
3,3
7,5
0,0
4,1
3,3
-4,5
0,0
2x^0+4x^1+6x^3+3x^5
多项式2: 4x^1+3x^3+-4x^5
3、概要设计
本程序有两个程序段:
a.主函数main;
b.创建多项式函数polycreate;
c.多项式相加函数polyadd;
d.多项式输出函数print;
4、详细设计
#include<stdio.h>
#define OK 1;
#define error 0;
int c,e;
head=(Polynode*)malloc(sizeof(Polynode));
rear=head;
scanf("%d,%d",&c,&e);
while(c!=0)
{
s=(Polynode*)malloc(sizeof(Polynode));
s->coef=c;
s->exp=e;
rear->next=s;
实验报告
2009级数学与应用数学班2011年11月19日
姓名:学号:
1、实验目的
用尾插法建立一元多项式的链表,验证是否实现,并进行两个多项式的相加运算
2、需求分析
rear=s;
scanf("%d,ቤተ መጻሕፍቲ ባይዱd",&c,&e);
}
rear->next=NULL;
return(head);
}
main()
{
printf("%d",1+1);}
用尾插法建一个一元多项式链表
Void polyadd(Polylist polya,Polylist polyb)
{
Polynode *p,*q,*tail,*temp; int sum;
本程序用TC编写,完成尾插法一元多项式的链表,并验证,及完成相加运算。
1输入的形式和输入值的范围:输入多项式每一项的系数、指数,输入的所有系数和指数均为整数。
2输入的形式:输出多项式各项的系数、指数。
3程序所能达到的功能:完成单链表的建立,及相加操作。
4测试数据:1+1
多项式1: 2x^0+3x^3+7x^5
#define TURE 1;
#define FALSE 0;
typedef struct Polynode
{
int coef;
int exp;
struct Polynode *next;
}Polynode ,*Polylist;
Polylist polycreate()
{
Polynode *head,*rear,*s;
P=polya->next; q=polyb->next; tail=polya;
While(p!=NULL&&q!=NULL)
{
If(p->exp<q->exp)
{tail->exp=p;tail=p;p=p->next;}
Else if(p->exp==q->exp)
{sum=p->coef+q->coef;
If(sum!=0)
{p->coef=sum;tail->next=p;tail=p;
P=p->next;temp=q;q=q->next;free(temp);}
else
{temp=p;p=p->next;free(temp);
Temp=q;q=q->next;free(temp);
}
}
else
{tail->next=q;tail=q;q=q->next;}
}
If (p!=NULL) tail->next=p;
else tail->next=q;
}
多项式相加函数
Void printf (polylist p)
{
While (p!=NULL)
{printf (“%dx^%d+”,p->exp);
P=p->next;}
}
多项式输出函数
5、测试结果
2
2,0
3,3
7,5
0,0
4,1
3,3
-4,5
0,0
2x^0+4x^1+6x^3+3x^5
多项式2: 4x^1+3x^3+-4x^5
3、概要设计
本程序有两个程序段:
a.主函数main;
b.创建多项式函数polycreate;
c.多项式相加函数polyadd;
d.多项式输出函数print;
4、详细设计
#include<stdio.h>
#define OK 1;
#define error 0;
int c,e;
head=(Polynode*)malloc(sizeof(Polynode));
rear=head;
scanf("%d,%d",&c,&e);
while(c!=0)
{
s=(Polynode*)malloc(sizeof(Polynode));
s->coef=c;
s->exp=e;
rear->next=s;