當前位置:首頁 » 編程語言 » 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;
}