#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
/*
问题1:假设多项式:A19(x)=8+3x+9x10+5x17+7x19,写出算法实现!
*/
struct node{
float index;
float coef;
struct node *link;
};
typedef struct node NODE;
NODE *create(void)
{NODE *newnode,*head,*p;
float index,coef;
head=(NODE *)malloc(sizeof(NODE));
head->link=NULL;
p=head;
printf("/n输入多项式的系数和指数,以0,0结束:");
scanf("%f,%f",&index,&coef);
while((index!=0)&&(coef!=0))
{newnode=(NODE *)malloc(sizeof(NODE));
newnode->index=index;
newnode->coef =coef;
newnode->link=NULL;
p->link =newnode;
p=p->link;
printf("/n输入下一项:");
scanf("%f,%f",&index,&coef);
}
return head;
}
/*根据给定的表达式和x的值,此函数计算出最后的表达式的值*/
float fun(NODE *head,float x)
{
float sum=0;
NODE *p=head->link;
while(p!=NULL)
{
sum+=p->index*pow(x,p->coef);
p=p->link;
}
return sum;
}
int main( )
{
NODE *head;
float x;
head=create();
printf("/n请输入x的值:");
scanf("%f",&x);
printf("/n最后结果为A19(%.0f)=%.f.",x,fun(head,x));
getch();
}
/*该程序在Tc,C++builder下调试通过*/
//---------------------------------------------------------------------------