当前位置:首页 » 编程语言 » c语言描述多项式
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言描述多项式

发布时间: 2022-10-31 09:22:10

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

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#defineEPS1E-6

typedefstructitem{
doublecoefficient;
intpower;
structitem*next;
}*POLYNOMIAL,*pItem;

POLYNOMIALCreate(){//创建多项式
pItemhead,p;
doublecoe;
intpwr;
head=p=(pItem)malloc(sizeof(item));
while(1){
printf("系数幂次(00结束):");
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;
returnhead;
}

voidSort(POLYNOMIALhead){//按幂次降排序
pItempt,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;
}
elseq=q->next;
}
p=p->next;
}
}

voidShow(POLYNOMIALhead){//显示多项式
POLYNOMIALp=head->next;
intflag=1;
if(p==NULL)return;
while(p){
if(flag){
if(fabs(p->coefficient)>=EPS){
if(p->power==0)printf("%.2lf",p->coefficient);
elseif(p->power==1){
if(p->coefficient==1.0)printf("x");
elseif(p->coefficient==-1.0)printf("-x");
elseprintf("%.2lfx",p->coefficient);
}
elseif(p->coefficient==1.0)printf("x^%d",p->power);
elseif(p->coefficient==-1.0)printf("-x^%d",p->power);
elseprintf("%.2lfx^%d",p->coefficient,p->power);
flag=0;
}
}
elseif(p->coefficient>0.0&&fabs(p->coefficient)>=EPS){
if(p->power==0)printf("+%.2lf",p->coefficient);
elseif(p->power==1){
if(p->coefficient==1.0)printf("+x");
elseprintf("+%.2lfx",p->coefficient);
}
elseif(p->coefficient==1.0)printf("+x^%d",p->power);
elseprintf("+%.2lfx^%d",p->coefficient,p->power);
}
elseif(p->coefficient<0.0&&fabs(p->coefficient)>=EPS){
if(p->power==0)printf("-%.2lf",-p->coefficient);
elseif(p->power==1){
if(p->coefficient==-1.0)printf("-x");
elseprintf("-%.2lfx",-p->coefficient);
}
elseif(p->coefficient==-1.0)printf("-x^%d",p->power);
elseprintf("-%.2lfx^%d",-p->coefficient,p->power);
}
p=p->next;
}
printf(" ");
}

doublePower(doublex,intn){
doublevalue=1.0;
inti;
for(i=0;i<n;++i)value*=x;
returnvalue;
}

doubleValue(POLYNOMIALhead,doublex){//多项式求值
POLYNOMIALp;
doublevalue=0.0;
for(p=head->next;p;p=p->next)
value+=p->coefficient*Power(x,p->power);
returnvalue;
}

POLYNOMIALCopy(POLYNOMIALA){
POLYNOMIALhead,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;
returnhead;
}

POLYNOMIALAdditive(POLYNOMIALA,POLYNOMIALB){//多项式相加
POLYNOMIALhead,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);
returnhead;
}

POLYNOMIALSubtract(POLYNOMIALA,POLYNOMIALB){//多项式相减
POLYNOMIALhead,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);
returnhead;
}

POLYNOMIALMultiplication(POLYNOMIALA,POLYNOMIALB){//多项式相乘
POLYNOMIALhead,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);
}
elseq=q->next;
}
p=p->next;
}
returnhead;
}

voidFreeMemory(POLYNOMIALhead){
POLYNOMIALq,p=head;
while(p){
q=p;
p=q->next;
free(q);
}
}

intmain(){
printf("创建多项式A: ");
POLYNOMIALA=Create();
Sort(A);
printf("A(x)=");Show(A);
printf("创建多项式B: ");
POLYNOMIALB=Create();
Sort(B);
printf("B(x)=");Show(B);
POLYNOMIALC=Additive(A,B);
printf("C(x)=");Show(C);
POLYNOMIALD=Subtract(A,B);
printf("D(x)=");Show(D);
POLYNOMIALE=Multiplication(A,B);
printf("E(x)=");Show(E);
printf("A(%.2lf)=%.4lf ",2.0,Value(A,2.0));
printf("B(%.2lf)=%.4lf ",2.0,Value(B,2.0));
printf("C(%.2lf)=%.4lf ",2.0,Value(C,2.0));
printf("D(%.2lf)=%.4lf ",2.0,Value(D,2.0));
printf("E(%.2lf)=%.4lf ",2.0,Value(E,2.0));
FreeMemory(A);
FreeMemory(B);
FreeMemory(C);
FreeMemory(D);
FreeMemory(E);
return0;
}

㈡ C语言,描述多项式加法

1. #include"malloc.h"
2. typedef struct Polyn /*定义多项式每一项的类型*/
3. {
4. float cofe; /*每一项系数*/
5. int expn; /*每一项的指数*/
6. struct Polyn *next;
7. struct Polyn *prior;
8. }*Pol.yn,SNode;
9. void setPolyn(Polyn *t1,Polyn *t2,int n) /*建立多项式*/
10. {
11. float a;
12. int i,b;
13. *t1=(SNode *)malloc(sizeof(SNode)); /*创建头节点*/
14. (*t1)->next=NULL;
15. for(i=0;i<n;i++)
16. {
17. *t2=(SNode *)malloc(sizeof(SNode));
18. scanf("%f,%d;",&a,&b);
19. (*t2)->cofe=a; (*t2)->expn=b;
20. (*t1)->next->prior=(*t2);
21. (*t2)->prior=(*t1);
22. (*t2)->next=(*t1)->next;
23. (*t1)->next=(*t2);
24. } /*创建每一项并连接成多项式*/
25. }
26. void print(Polyn *t) /*输出多项式*/
27. {
28. Polyn p;
29. for(p=(*t)->next;p!=NULL;p=p->next)
30. printf("%f,%d;",p->cofe,p->expn);
31. printf("\n");
32. }
33. void arrange(Polyn *t) /*化简多项式*/
34. {
35. float m1;
36. int m2;
37. Polyn p,q,r,s;
38. for(p=(*t)->next;p!=NULL;p=p->next)
39. for(q=p->next;q!=NULL;q=q->next)
40. if((p->expn)>(q->expn))
41. {
42. m1=p->cofe;p->cofe=q->cofe;q->cofe=m1;
43. m2=p->expn;p->expn=q->expn;q->expn=m2;
44. } /*冒泡法多项式指数排序*/
45. for(p=(*t)->next;p!=NULL;p=p->next)
46. if((p->expn)==(p->next->expn))
47. {
48. r=p->next;
49. p->cofe+=p->next->cofe;
50. p->next=p->next->next;
51. p=p->prior; /*指针指向上一结点(须重新处理现在处理 的结点)*/
52. free(r);
53. if((p->next->cofe)==0)
54. {
55. s=p->next;
56. p->next=p->next->next;
57. free(s);
58. }
59. } /*多项式相同指数项系数求和,化简多项式*/
60. }
61. void linkPolyn(Polyn *t1,Polyn *t2) /*两个多项式连接*/
62. {
63. Polyn p,q;
64. for(p=(*t2)->next;p!=NULL;p=p->next)
65. q=p;
66. p=(*t2)->next;
67. q->next=(*t1)->next;
68. (*t1)->next->prior=q;
69. (*t1)->next=p;
70. p->prior=(*t1);
71. }
72. main() /*主函数*/
73. {
74. Polyn La,la,Lb,lb;
75. int n,m; /*多项式项数*/
76. printf("enter La’s lenth:");
77. scanf("%d",&n);
78. setPolyn(&La,&la,n);
79. arrange(&La);
80. printf("after arrange La is:\n");
81. print(&La); /*输入多项式La,化简并输出*/
82. printf("enter Lb’s lenth:");
83. scanf("%d",&m);
84. setPolyn(&Lb,&lb,m);
85. arrange(&Lb);
86. printf("after arrange Lb is:\n");
87. print(&Lb); /*输入多项式Lb,化简并输出*/
88. linkPolyn(&La,&Lb); /*La与Lb连接,形成新的La*/
89. arrange(&La); /*化简La*/
90. printf("after add Polyn is :\n");
91. print(&La); /*输出结果*/
92. }

㈢ C语言实现的一元多项式的表示及相减


#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
int data;
struct Node *next;
}Node;//链表结点

typedef struct LinkList//定义链表数据类型
{
Node *head;//表头结点
void PrintMform()//输出多项式
{
Node *p=head->next;
int exp = head->data-1;
while(p!=NULL)
{
if(exp==head->data-1)
printf("%dX^%d",p->data,exp);
else
{
if(p->data>0)
printf("+%dX^%d",p->data,exp);
if(p->data==0)
printf("+0");
if(p->data<
0)
printf("%dX^%d",p->data,exp);
}
exp--;
p=p->next;
}
printf("\n");
}
void CreateByInput(int length)//通过输入元素建立链表
{
head=(Node *)malloc(sizeof(Node));
head->data=0;//表头节点存储链表真实长度
head->next=NULL;
int i,temp;
Node *p,*cur = head;
for(i=1;i<=length;i++)
{
printf("structing LinkList,Please input the value:\n");
scanf("%d",&temp);
p = (Node *)malloc(sizeof(Node));
p->data=temp;
p->next=cur->next;
cur->next=p;
cur = cur->next;
head->data++;

}
}
}LinkList;

void main()
{
LinkList L1,L2;
int length;//就是多项式的项数
printf("Please input the first LinkList's length:\n");
scanf("%d",&length);
printf("begin to struct the first LinkList\n");//开始构造第一个多项式
L1.CreateByInput(length);
printf("begin to struct the second LinkList\n");//开始构造第二个多项式
L2.CreateByInput(length);
printf("the first oxiangshi is:\n");
L1.PrintMform();//输出第一个多项式
printf("the second oxiangshi is:\n");
L2.PrintMform();//输出第二个多项式
Node *p = L1.head->next;/////////////////从这里开始
Node *q = L2.head->next;//是计算多项式L1-L2,结果存入L1
while(p!=NULL)
{
p->data-=q->data;
p=p->next;
q=q->next;
}/////////////////////////////////////到这里结束
printf("the substract is:\n");
L1.PrintMform();
system("pause");
}

㈣ c语言表达式如何表示多项式:5+2X2+4X5+6X8-10X9

C语言的数学表达式四则运算的符号:加减乘除 就是 +-*/
因此这个多项式
5+2X2+4X5+6X8-10X9 = 5+2*x*x+4*x*x*x*x*x+6**x*x*x*x*x*x*x*x-10*x*x*x*x*x*x*x*x*x.
因为C语言没有乘方运算符,需要使用数学库power()才行。

㈤ 如何用C语言实现设计和实现多项式运算

【知识点】
若矩阵A的特征值为λ1,λ2,...,λn,那么|A|=λ1·λ2·...·λn

【解答】
|A|=1×2×...×n= n!
设A的特征值为λ,对于的特征向量为α。
则 Aα = λα
那么 (A²-A)α = A²α - Aα = λ²α - λα = (λ²-λ)α
所以A²-A的特征值为 λ²-λ,对应的特征向量为α

A²-A的特征值为 0 ,2,6,...,n²-n

【评注】
对于A的多项式,其特征值为对应的特征多项式。
线性代数包括行列式、矩阵、线性方程组、向量空间与线性变换、特征值和特征向量、矩阵的对角化,二次型及应用问题等内容。

㈥ 怎么用C程序来进行多项式的四则运算

首先,需要一个结构用来在内存中存储多项式的项:
typedef
struct
tagItem_t
{
int
iExponential;
double
dCoefficient;
}
Item_t;
就可以。
其次,需要一个线型数据结构来表示多项式,数组、链表都可以。
当然,如果你选择数组的存储方式,也可以用数组下标来表示某个项的次数。比如:
0.3
*
x
^
3
+
2.5
*
x
+
1.0
就可以存储为:
double
adMultinomial[5]
=
{
1.0L,
2.5L,
0.0L,
0.3L
};
然后,就可以先实现加减法,再借助加减法实现乘除法。

㈦ C语言:用链表表示多项式并完成输入、运算和输出。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedefintElemType;

/*单项链表声明*/
typedefstructPolynNode{
intcoef;//系数
intexpn;//指数
structPolynNode*next;
}PolynNode,*PolynList;

/*位序(插表尾)输入n元素值建立带表结构单链线性表*/
/*指数系数输入*/
voidCreatePolyn(PolynList&L,intn)
{
inti;
PolynListp,q;
L=(PolynList)malloc(sizeof(PolynNode));//结点
L->next=NULL;
q=L;
printf("输入%d数据 ",n);
for(i=1;i<=n;i++)
{
p=(PolynList)malloc(sizeof(PolynNode));
scanf("%d%d",&p->coef,&p->expn);//指数系数输入
q->next=p;
q=q->next;
}
p->next=NULL;
}

//初始条件:单链表L已存
//操作结:依L每数据元素调用函数vi()旦vi()失败,则操作失败
voidPolynTraverse(PolynListL,void(*vi)(ElemType,ElemType))
{
PolynListp=L->next;
while(p)
{
vi(p->coef,p->expn);
if(p->next)
{
printf("+");//+号输项面没+
}
p=p->next;
}
printf(" ");
}

/*ListTraverse()调用函数(类型要致)*/
voidvisit(ElemTypec,ElemTypee)
{
if(c!=0)
{
printf("%dX^%d",c,e);//格式化输项式每项
}
}

/*项式相加原理:归并*/
/*参数:两已经存项式*/
/*返值:归并新项式结点*/
PolynListMergeList(PolynListLa,PolynListLb)
{
PolynListpa,pb,pc,Lc;
pa=La->next;
pb=Lb->next;
Lc=pc=La;//用La结点作Lc结点
while(pa&&pb)
{
if(pa->expn<pb->expn)
{
pc->next=pa;//指数相等pc指针连指数结点
pc=pa;
pa=pa->next;//指向该结点指针移
}
elseif(pa->expn>pb->expn)
{
pc->next=pb;//pc指针连指数结点
pc=pb;
pb=pb->next;//指向该结点指针移
}
else//(pa->expn=pb->expn)
{
pa->coef=pa->coef+pb->coef;//指数相等系数相加
pc->next=pa;
pc=pa;
pa=pa->next;//两指针都往移
pb=pb->next;
}
}

pc->next=pa?pa:pb;//插入剩余段

returnLc;
}

voidmain()
{
PolynListha,hb,hc;
printf("非递减输入项式ha");
CreatePolyn(ha,5);//位序输入n元素值

printf("非递减输入项式hb");
CreatePolyn(hb,5);//位序输入n元素值

printf("项式ha:");
PolynTraverse(ha,visit);
printf(" ");
printf("项式hb:");
PolynTraverse(hb,visit);
printf(" ");

hc=MergeList(ha,hb);
PolynTraverse(hc,visit);
}

㈧ 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语言实现多项式的加法和乘法

按题目要求应该是(1+X)*(1+X)=X2+1吧
可以用单链表表示多项的指数,比如1+X可以表示为0,1
X2+1可以表示为2,0,Xn+X(n-1)+...+1即n,n-1,.....0
所有的指数建议按大小排序,可以在单链表插入时进行。
加法,可以新建一个链表C做为结果,把链表A的内容复制到C,然后把另一个链表B中的每一项插入C,如果要插入的项已存在,则不插入并且删除这个结点。
乘法,新建一个链表C作为结果,要用2层循环遍历A和B中的每一项,对AB中的每个项都要算个加法,把和插入C中,如果这个和已存在,则不插入并且删除这个结点。

㈩ C语言多项式

#include <stdio.h>
#define DEGREE_MAX 8
void get_poly(double coeff[], int *degree)
{
int i;
printf("please enter the biggest degree:");
scanf("%d", degree);
for (i = *degree; i >= 0; i--) {
printf("enter %d `s Coefficient:", i);
scanf("%lf", &coeff[i]);
}
printf("\nyour polynomial is:\n");
for (i = *degree; i >= 0; i--) {
printf("%.2lfX^%d%c", coeff[i], i, (i > 0?' ' : '\n'));
}
}
double eval_poly(const double coeff[], int degree, double x)
{
int i;
double m = x, ret = 0;
ret += coeff[0];
for (i = 1; i <= degree; i++) {
ret += coeff[i]*m;
m *= x;
}
return ret;
}
int main() {
double coeff[DEGREE_MAX],x;
int degree;
get_poly(coeff, °ree);
printf("\nplease enter X value:");
scanf("%lf", &x);
printf("the answer is %.2lf\n", eval_poly(coeff, degree, x));
return 0;
}