简单多项式求值c语言编程(简单多项式求值c语言编程实例)

http://www.itjxue.com  2023-03-17 20:56  来源:未知  点击次数: 

C语言中一维多项式求值

计算多项式 p(x)=a(n-1)x(n-1)+a(n-2)x(n-2)+.....a1x+a0;

在指定点x处的函数值。

算法:

首先将多项式表述成如下嵌套的方式:

p(x)=(...((a(n-1)+a(n-2))x+a(n-3))x+....a1)x+a0;

然后依次从里向外算(因为x是已知的么),得到递推公式:

U(n-1)=a(n-1)

U(k)=U(k+1)x+a(k); K=n-2,n-3......1,0;

那当算到k=0时,得到的U(0)就是要求的值。

下面是用C语言实现的:

double plyv( double a[],double x,int n) //a[]是多项式的系数,n是数组长度。

{

double u;//一直存放递归结果;

Int i;

for(i=n-2;i=0;i--)

{

u=u*x+a[i];

}

return u;

}

#include

int main()

{

double a[3]={2,3,4};//根据多项式的形式定义数组长度以及个数,如果有的x项没有,则视系数为0;

double s;

double x;

s=plyv(a,x,3);//此为最后结果;

printf("%f",s);

return 0;

}

此题的解题重点在于:找到求解的递归关系,然后依据递归关系求解。

C语言 简单多项式的求值 题目是:对用户输入的任一整数,输出以下多项式 y=2x的平方+x

#include?stdio.h

int?main(){

int?x?=?0,?y?=?0;

scanf("%d",?x);

y?=?2?*?x?*?x?+?x?+?8;

printf("%d\n",?y);

return?0;

}

执行结果:

多项式计算 C语言编程

这个其实很简单,需要3个数组(暂时考虑int数组),长度都是10,分别保存多项式1、2和计算结果。初始化为全0。输入就按照你的假设吧。输入后三个数组分别为:

多项式1:[7, 0, -5, 2, 0, 0, 0, 0, 0, 0](x的0次幂系数是7,x的1次幂系数是2,以此类推,下同)

多项式2:[-8, 1, 3, 0, 0, 0, 0, 0, 0, 0]

计算结果:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0](还没算呢,当然都是0)

加法减法很好算,不赘述。乘法怎么算呢,你按照真实的数学计算步骤推一遍就知道了,你会把3x2、x、-8分别乘以2x3-5x2+7,最后把结果加起来。转换到程序中,就是把若干个数组加起来:

[-56, 0, 40, -16, 0, 0, 0, 0, 0, 0]

[0, 7, 0, -5, 2, 0, 0, 0, 0, 0]

[0, 0, 21, 0, -15, 6, 0, 0, 0, 0]

加起来就可以了。

至于提高水平,这个题目出得不好,因为多项式相除结果不唯一。比如说2x2 + 1除以x2 + 1,你可以说2x2 + 1 = 2(x2 + 1) - 1,也可以说2x2 + 1 = 1(x2 + 1) + x2。这样的题目数学上就意义不大,用程序去实现也达不到锻炼水平的作用。也许我理解有误?

C语言程序题:编写程序实现多项式计算

#include?stdio.h

#include?stdlib.h

#include?math.h

#define?EPS?1E-6

typedef?struct?item?{

double?coefficient;

int?power;

struct?item?*next;

}?*POLYNOMIAL,*pItem;

POLYNOMIAL?Create()?{?//?创建多项式

pItem?head,p;

double?coe;

int?pwr;

head?=?p?=?(pItem)malloc(sizeof(item));

while(1)?{

printf("系数?幂次(0?0结束)?:?");

scanf("%lf%d",coe,pwr);

if(fabs(coe)?=?EPS??!pwr)?break;

p-next?=?(pItem)malloc(sizeof(item));

p-next-coefficient?=?coe;

p-next-power?=?pwr;

p?=?p-next;

}

p-next?=?NULL;

return?head;

}

void?Sort(POLYNOMIAL?head)?{?//?按幂次降排序

pItem?pt,q,p?=?head;

while(p-next)?{

q?=?p-next;

while(q-next)?{

if(p-next-power??q-next-power)?{

pt?=?p-next;

p-next?=?q-next;

q-next?=?p-next-next;

p-next-next?=?pt;

}

else?q?=?q-next;

}

p?=?p-next;

}

}

void?Show(POLYNOMIAL?head)?{?//?显示多项式

POLYNOMIAL?p?=?head-next;

int?flag?=?1;

if(p?==?NULL)?return;

while(p)?{

if(flag)?{

if(fabs(p-coefficient)?=?EPS)?{

if(p-power?==?0)?printf("%.2lf?",p-coefficient);

else?if(p-power?==?1)?{

if(p-coefficient?==?1.0)?printf("x?");

else?if(p-coefficient?==?-1.0)?printf("-x?");

else?printf("%.2lfx?",p-coefficient);

}

else?if(p-coefficient?==?1.0)?printf("x^%d?",p-power);

else?if(p-coefficient?==?-1.0)?printf("-x^%d?",p-power);

else?printf("%.2lfx^%d?",p-coefficient,p-power);

flag?=?0;

}

}

else?if(p-coefficient??0.0??fabs(p-coefficient)?=?EPS)?{

if(p-power?==?0)?printf("+?%.2lf?",p-coefficient);

else?if(p-power?==?1)?{

if(p-coefficient?==?1.0)?printf("+?x?");

else?printf("+?%.2lfx?",p-coefficient);

}

else?if(p-coefficient?==?1.0)?printf("+?x^%d?",p-power);

else?printf("+?%.2lfx^%d?",p-coefficient,p-power);

}

else?if(p-coefficient??0.0??fabs(p-coefficient)?=?EPS)?{

if(p-power?==?0)?printf("-?%.2lf?",-p-coefficient);

else?if(p-power?==?1)?{

if(p-coefficient?==?-1.0)?printf("-?x?");

else?printf("-?%.2lfx?",-p-coefficient);

}

else?if(p-coefficient?==?-1.0)?printf("-?x^%d?",p-power);

else?printf("-?%.2lfx^%d?",-p-coefficient,p-power);

}

p?=?p-next;

}

printf("\n");

}

double?Power(double?x,int?n)?{

double?value?=?1.0;

int?i;

for(i?=?0;?i??n;?++i)?value?*=?x;

return?value;

}

double?Value(POLYNOMIAL?head,double?x)?{?//?多项式求值

POLYNOMIAL?p;

double?value?=?0.0;

for(p?=?head-next;?p;?p?=?p-next)

value?+=?p-coefficient?*?Power(x,p-power);

return?value;

}

POLYNOMIAL?Copy(POLYNOMIAL?A)?{

POLYNOMIAL?head,t,p;

head?=?t?=?(pItem)malloc(sizeof(item));

for(p?=?A-next;?p;?p?=?p-next)?{

t-next?=?(pItem)malloc(sizeof(item));

t-next-coefficient?=?p-coefficient;

t-next-power?=?p-power;

t?=?t-next;

}

t-next?=?NULL;

return?head;

}

POLYNOMIAL?Additive(POLYNOMIAL?A,?POLYNOMIAL?B)?{?//?多项式相加

POLYNOMIAL?head,p,q,t;

head?=?Copy(A);

for(p?=?B;?p-next;?p?=?p-next)?{

q?=?head;

while(q-next)?{

if(p-next-power?==?q-next-power)?{

q-next-coefficient?+=?p-next-coefficient;

if(fabs(q-next-coefficient)?=?EPS)?{

t?=?q-next;

q-next?=?t-next;

free(t);

}

break;

}

q?=?q-next;

}

if(q-next?==?NULL)?{

q-next?=?(pItem)malloc(sizeof(item));

q-next-coefficient?=?p-next-coefficient;

q-next-power?=?p-next-power;

q-next-next?=?NULL;

}

}

Sort(head);

return?head;

}

POLYNOMIAL?Subtract(POLYNOMIAL?A,?POLYNOMIAL?B)?{?//?多项式相减

POLYNOMIAL?head,p,q,t;

head?=?Copy(A);

for(p?=?B;?p-next;?p?=?p-next)?{

q?=?head;

while(q-next)?{

if(p-next-power?==?q-next-power)?{

q-next-coefficient?-=?p-next-coefficient;

if(fabs(q-next-coefficient)?=?EPS)?{

t?=?q-next;

q-next?=?t-next;

free(t);

}

break;

}

q?=?q-next;

}

if(q-next?==?NULL)?{

q-next?=?(pItem)malloc(sizeof(item));

q-next-coefficient?=?-p-next-coefficient;

q-next-power?=?p-next-power;

q-next-next?=?NULL;

}

}

Sort(head);

return?head;

}

POLYNOMIAL?Multiplication(POLYNOMIAL?A,?POLYNOMIAL?B)?{?//?多项式相乘

POLYNOMIAL?head,t,p,q;

head?=?t?=?(pItem)malloc(sizeof(item));

for(p?=?A-next;?p;?p?=?p-next)?{?//?完成相乘过程

for(q?=?B-next;?q;?q?=?q-next)?{

t-next?=?(pItem)malloc(sizeof(item));

t-next-coefficient?=?p-coefficient?*?q-coefficient;

t-next-power?=?p-power?+?q-power;

t?=?t-next;

}

}

t-next?=?NULL;

Sort(head);?//?排序

p?=?head;

while(p-next)?{?//?合并同类项

q?=?p-next;

while(q-next)?{

if(p-next-power?==?q-next-power)?{

p-next-coefficient?+=?q-next-coefficient;

t?=?q-next;

q-next?=?t-next;

free(t);

}

else?q?=?q-next;

}

p?=?p-next;

}

return?head;

}

void?FreeMemory(POLYNOMIAL?head)?{

POLYNOMIAL?q,p?=?head;

while(p)?{

q?=?p;

p?=?q-next;

free(q);

}

}

int?main()?{

printf("创建多项式A:\n");

POLYNOMIAL?A?=?Create();

Sort(A);

printf("A(x)?=?");Show(A);

printf("创建多项式B:\n");

POLYNOMIAL?B?=?Create();

Sort(B);

printf("B(x)?=?");Show(B);

POLYNOMIAL?C?=?Additive(A,B);

printf("C(x)?=?");Show(C);

POLYNOMIAL?D?=?Subtract(A,B);

printf("D(x)?=?");Show(D);

POLYNOMIAL?E?=?Multiplication(A,B);

printf("E(x)?=?");Show(E);

printf("A(%.2lf)?=?%.4lf\n",2.0,Value(A,2.0));

printf("B(%.2lf)?=?%.4lf\n",2.0,Value(B,2.0));

printf("C(%.2lf)?=?%.4lf\n",2.0,Value(C,2.0));

printf("D(%.2lf)?=?%.4lf\n",2.0,Value(D,2.0));

printf("E(%.2lf)?=?%.4lf\n",2.0,Value(E,2.0));

FreeMemory(A);

FreeMemory(B);

FreeMemory(C);

FreeMemory(D);

FreeMemory(E);

return?0;

}

(责任编辑:IT教学网)

更多