C 对给定值的多元组进行评估
我想计算一个给定值的多项式,所以我使用了一个链表来存储系数和指数,并且我定义了一个函数C 对给定值的多元组进行评估,c,math,linked-list,C,Math,Linked List,我想计算一个给定值的多项式,所以我使用了一个链表来存储系数和指数,并且我定义了一个函数evaluate()来计算给定值的多项式,但是当涉及到输出时,我确实遇到了一个问题,它总是显示值0.00000 我能得到一些帮助吗?提前谢谢 以下是完整的代码: #include <stdio.h> #include <stdlib.h> typedef struct list { int coef; int expo; struct list* next; }l
evaluate()
来计算给定值的多项式,但是当涉及到输出时,我确实遇到了一个问题,它总是显示值0.00000
我能得到一些帮助吗?提前谢谢
以下是完整的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int coef;
int expo;
struct list* next;
}list;
int compare(char* s1,char* s2)
{
int i=0;
char c1,c2;
do
{
c1= (*(s1+i));
c2= (*(s2+i));
i++;
if(c2 == '\0' || c1 == '\0')
return c1-c2;
}while(c1==c2);
return c1-c2;
}
int transform(char* arr)
{
int i,sign=0;
int s=0;
if((*arr) == '-') sign=1;
for(i=0;arr[i]!='\0';i++)
{
if(arr[i]>=0 && arr[i]<=9)
s=s*10+(arr[i]-'0');
}
if(sign) return -s;
return s;
}
list* insert(list* head,char* coef,int expo)
{
list* node=(list*)malloc(sizeof(list));
if(node==NULL)
{
printf("Stack Overflow");
exit(1);
}
node->next=NULL;
node->coef=transform(coef);
node->expo=expo;
if(head==NULL) return node;
list* temp=head;
while(temp && temp->next)
temp=temp->next;
temp->next=node;
return head;
}
float power_of(float x,int expo)
{
if(expo==0) return 1;
int i=1;
float s=1;
if(expo > 0)
while(i++ <= expo)
s*=x;
else if (expo < 0)
while(i++ <= -expo)
s*=1.0/x;
return s;
}
float evaluate(list* head,float x)
{
if(head==NULL) return 0;
float s=0.0;
while(head)
{
s+=(head->coef)*power_of(x,head->expo);
head=head->next;
}
return s;
}
int main()
{
list* head=NULL;
char coef[11];
int expo,i=1;
float x;
printf("insert the polynome <type end to stop>:\n");
do
{
printf("insert the coefficient %d> : ",i);
scanf("%s",coef);
if(compare(coef,"end"))
{
printf("insert the exposnant %d> : ",i++);
scanf("%d",&expo);
head=insert(head,coef,expo);
}
}while(compare(coef,"end"));
printf("insert a value to evaluate the polynome : ");
scanf("%f",&x);
float value=evaluate(head,x);
printf("output : %f ",value);
return 0;
}
#包括
#包括
类型定义结构列表
{
内系数;
国际博览会;
结构列表*下一步;
}名单;
整数比较(字符*s1,字符*s2)
{
int i=0;
字符c1,c2;
做
{
c1=(*(s1+i));
c2=(*(s2+i));
i++;
如果(c2='\0'| | c1='\0')
返回c1-c2;
}而(c1==c2);
返回c1-c2;
}
整数变换(字符*arr)
{
int i,符号=0;
int s=0;
如果(*arr)='-')符号=1;
对于(i=0;arr[i]!='\0';i++)
{
如果(arr[i]>=0&&arr[i]next=NULL;
节点->coef=变换(coef);
节点->世博会=世博会;
if(head==NULL)返回节点;
列表*温度=头部;
while(临时和临时->下一步)
温度=温度->下一步;
temp->next=节点;
回流头;
}
浮子功率(浮子x,国际博览会)
{
如果(expo==0)返回1;
int i=1;
浮点数s=1;
如果(世博会>0)
而(i++expo),;
头部=头部->下一步;
}
返回s;
}
int main()
{
列表*head=NULL;
char-coef[11];
国际博览会,i=1;
浮动x;
printf(“插入多项式:\n”);
做
{
printf(“插入系数%d>:”,i);
scanf(“%s”,coef);
如果(比较(coef,“结束”))
{
printf(“插入说明%d>:”,i++);
scanf(“%d”和expo);
head=插入(head、coef、expo);
}
}while(比较(coef,end));
printf(“插入一个值来计算多项式:”);
scanf(“%f”、&x);
浮动值=评估(水头,x);
printf(“输出:%f”,值);
返回0;
}
在transform()
中使用if((arr[i]>='0')&(arr[i]我没有按照代码进行操作,但我建议您将char-coef[11];
改为char-coef[100];
这是一个非常精确和无情的大小。同时将scanf(“%s”,coef);
改为scanf(“%99s”,coef);
或f(“%10s”另一件小事:如果你能使用滑出的double
@pmg Ow,就不要使用劣质的float
。。谢谢问题的解决。别忘了double
需要%lf
和scanf
。