#include<stdio.h> /*此函数的功能是实现两个多项式相加并计算多项式的值 */
#include<conio.h> /*输入多项式的COEF和EXP时应按 EXP(即幂级)的由大到小*/
#include<malloc.h> /*顺序输入 */
#include<math.h>
#define ERROR 1
#define OK 0
/*****************************//*定义用户类型*/
struct usertype
{
float coef;
int exp;
};
typedef struct usertype usertype;
/*****************************//*定义结点*/
struct NODE
{
usertype data;
struct NODE *next;
};
typedef struct NODE *linklist;
/******************************//*全局变量*/
/******************************//*追加结点*/
linklist append(linklist tail,usertype item)
{
tail->next=malloc(sizeof(struct NODE));
if(!tail)
{
printf("malloc error!\n");
return NULL;
}
tail=tail->next;
tail->data.coef=item.coef;
tail->data.exp=item.exp;
tail->next=NULL;
return tail;
}
/******************************//*输出结点信息*/
int getmessage(linklist head)
{
int num=0;
linklist p;
printf("\n结点存储的内容如下:\n");
for(p=head->next;p!=NULL;p=p->next)
{
num++;
printf("%d :COEF: %f \n EXP : %d\n",num,p->data.coef,p->data.exp);
}
if(!num)
printf("链表为空!\n");
return OK;
}
/******************************//*计算相加的结果*/
int add(linklist y1,linklist y2,linklist y3)
{
linklist point1,point2,point3;
usertype item;
point1=y1;
point2=y2;
point3=y3;
while(point1->next!=NULL&&point2->next!=NULL)
{
if(point1->next->data.exp==point2->next->data.exp)
{
item.coef=point1->next->data.coef+point2->next->data.coef;
if(fabs(item.coef)>0.000001)
{
item.exp=point1->next->data.exp;
point3=append(point3,item);
}
point1=point1->next;
point2=point2->next;
continue;
}
else
{
if(point1->next->data.exp<point2->next->data.exp)
{
item.exp=point2->next->data.exp;
item.coef=point2->next->data.coef;
point3=append(point3,item);
point2=point2->next;
continue;
}
if(point1->next->data.exp>point2->next->data.exp)
{
item.exp=point1->next->data.exp;
item.coef=point1->next->data.coef;
point3=append(point3,item);
point1=point1->next;
continue;
}
}
}
/***********************************//*复制y1的余项*/
while(point1->next!=NULL)
{
item.exp=point1->next->data.exp;
item.coef=point1->next->data.coef;
point3=append(point3,item);
point1=point1->next;
}
/***********************************//*复制y2的余项*/
while(point2->next!=NULL)
{
item.exp=point2->next->data.exp;
item.coef=point2->next->data.coef;
point3=append(point3,item);
point2=point2->next;
}
return OK;
}
/*********************************//*格式输出*/
int output(linklist head)
{
int num=0;
linklist p;
for(p=head->next;p!=NULL;p=p->next)
{
num++;
if(p->data.coef>0)
printf("+");
printf("%.2fx^%d",p->data.coef,p->data.exp);
}
if(!num)
printf("链表为空!\n");
printf("\n\n");
return OK;
}
/**********************************//*求多项式的值*/
double value(linklist head,double x)
{
double sum=0;
linklist p;
for(p=head->next;p!=NULL;p=p->next)
{
sum+=p->data.coef*pow(x,p->data.exp);
}
printf("多项式的值为:%.4lf\n",sum);
return sum;
}
/*********************************//*主函数*/
main()
{
double x;
linklist y1,y2,y3;
linklist tail1,tail2,tail3;
usertype a;
/**************************//*初始化*/
tail1=y1=malloc(sizeof(struct NODE));
if(!tail1)
{
printf("malloc error!!!");
return ERROR;
}
tail1->next=NULL;
tail2=y2=malloc(sizeof(struct NODE));
if(!tail2)
{
printf("malloc error!!!");
return ERROR;
}
tail2->next=NULL;
tail3=y3=malloc(sizeof(struct NODE));
if(!tail3)
{
printf("malloc error!!!");
return ERROR;
}
tail3->next=NULL;
/***************************//*输入多项式y1,y2*/
printf("输入y1(输入 0 0 表示结束):\n");
for(scanf("%f %d",&a.coef,&a.exp);fabs(a.coef)>0.000001;)
{
tail1=append(tail1,a);
scanf("%f %d",&a.coef,&a.exp);
}
printf("输入y2(输入 0 0 表示结束):\n");
for(scanf("%f %d",&a.coef,&a.exp);fabs(a.coef)>0.000001;)
{
tail2=append(tail2,a);
scanf("%f %d",&a.coef,&a.exp);
}
/***************************//*输出多项式的值*/
printf("y1=");
output(y1);
printf("y2=");
output(y2);
/****************************//*计算输出结果*/
add(y1,y2,y3);
printf("y1+y2=");
output(y3);
/****************************//*求值*/
printf("输入X的值:");
scanf("%lf",&x);
value(y3,x);
return OK;
}
评论0