Ⅰ 如何用c語言編寫一個多項式的輸入與建立並輸出
用函數來寫就可以了~!樓主把你想要的多項式寫出來吧!
如有更多的疑問請hi我
在這里給自己新創建的團隊「C菜鳥助手」打個小廣告,
各位如果有C/C++方面不懂的可以一起討論!
謝謝,祝各位生活愉快!
Ⅱ 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語言 N階勒讓得多項式
要是給出x求出結果很容易int lerangde(int n,int x)
{
if(n==0)return 1;
else if(n == 1)return x;
else
return ((2n-1)*x-lerangde(n-1,x)-(n-1)*lerangde(n-2,x))/n ;
} 要用程序把含有x的表達式寫出來太難了,因為是迭代的,你手寫一下啊N=100,你方便些寫來么?
Ⅳ 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語言實現設計和實現多項式運算
【知識點】
若矩陣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語言從文件讀取多項式
#include <stdio.h>
#include <malloc.h>
struct node
{
int a;
int b;//a,b分別代表系數與指數
struct node *next;
};
struct node *head;
void main()
{
FILE *fp;
fp=fopen("wan.txt","r");
int n,i;
fscanf(fp,"%d\n",&n); //記錄多項式的個數
struct node *p,*p1;
head=p=(node *)malloc(sizeof(node));
fscanf(fp,"%d %d\n",&p->a,&p->b);
for(i=1;i<n;i++)
{
p1=(node *)malloc(sizeof(node));
p->next=p1;
p=p1;
fscanf(fp,"%d %d\n",&p->a,&p->b);
}
p->next=NULL;
for(p=head;p!=NULL;p=p->next)
{
printf("%d %d\n",p->a,p->b);
//輸出多項式
}
}//不知道是不是你想要的答案,如果不是,請在說清楚點
Ⅶ 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;
}
Ⅷ C語言,多項式相乘
#include<stdio.h>
#include<stdlib.h>
typedefstructnode{
intcoefficient,power;
structnode*next;
}term;
term*new_term(intcoefficient,intpower){
term*t=(term*)malloc(sizeof(term));
t->next=NULL;
t->coefficient=coefficient;
t->power=power;
returnt;
}
voidfree_term(term*t){
free(t);
}
typedefstructlist{
termhead;
}polynomial;
voidinit_polynomial(polynomial*p){
p->head.next=NULL;
}
voidclear_polynomial(polynomial*p){
term*t=p->head.next;
term*del;
while(t!=NULL){
del=t;
t=t->next;
free_term(del);
}
p->head.next=NULL;
}
voidinsert_polynomial(polynomial*p,term*t){
t->next=p->head.next;
p->head.next=t;
}
voidsort(polynomial*p){
term*t;
term*next;
intfinish=0,temp;
while(!finish){
finish=1;
t=p->head.next;
while(t!=NULL){
next=t->next;
if(next!=NULL){
if(t->power<next->power){
temp=t->coefficient;
t->coefficient=next->coefficient;
next->coefficient=temp;
temp=t->power;
t->power=next->power;
next->power=temp;
finish=0;
}
}
t=next;
}
}
}
voidcombine(polynomial*p){
term*t=p->head.next;
term*next;
while(t!=NULL){
next=t->next;
if(next!=NULL&&next->power==t->power){
t->coefficient+=next->coefficient;
t->next=next->next;
free_term(next);
}
else{
t=next;
}
}
}
voidmultiply(polynomial*p1,polynomial*p2,polynomial*p3){
term*t1=p1->head.next;
term*t2;
clear_polynomial(p3);
init_polynomial(p3);
while(t1!=NULL){
t2=p2->head.next;
while(t2!=NULL){
insert_polynomial(p3,new_term(t1->coefficient*t2->coefficient,t1->power+t2->power));
t2=t2->next;
}
t1=t1->next;
}
sort(p3);
combine(p3);
}
voidinput(polynomial*p){
intcoef,power;
charc;
init_polynomial(p);
while(true){
scanf("%d%d",&coef,&power);
insert_polynomial(p,new_term(coef,power));
c=getchar();
if(c==' ')break;
}
sort(p);
combine(p);
}
voidoutput(polynomial*p){
term*t=p->head.next;
while(t!=NULL){
printf("%d%d",t->coefficient,t->power);
t=t->next;
}
}
intmain(){
inti;
polynomialp[3];
for(i=0;i<3;i++){
init_polynomial(&p[i]);
}
for(i=0;i<2;i++){
input(&p[i]);
}
multiply(&p[0],&p[1],&p[2]);
output(&p[2]);
}
Ⅸ c語言計算多項式的值
思路:
因為這是個加法的多項式,用變數s累加,s的最初值是0,第1次向上加1!,第2次加2!,...,第n磁加n! s+=jc;階乘變數jc在循環中計算。
每項用循環變數i控制,第1次循環取值1,第2次循環取值2,...,第20次循環取值20,這個變數就是要求的階乘數,i的值每次加1
階乘是個累乘的運算,用變數jc,初始值是1,第1次向上乘以循環變數值1,就是1!,第2次是前邊計算好的1!*2=2!,...,第20次就是19!*20=20!,jc*=i;
由於20!數據比較大,對於32bit的int型變數存放不下,可以考慮用double型數據計算,省得溢出。
因此程序主體演算法為:
int i;
double s,jc;
s=0; jc=1;
for ( i=1;i<=20;i++ ) { jc*=i; s+=jc; }
printf("%lf\n",s);
Ⅹ 多項式求和的c語言程序
#include <stdio.h>
int Fluction(int);//聲明實現多項式 1-1/2+1/3-1/4+1/5-1/6+...的功能函數
double sum;//定義全局變數(其實一般不推薦定義全局變數)
int main()
{
int m,n;//m個測試實例,求前 n項和
while(scanf("%d",&m)!=EOF)
{
for(int i=1;i<=m;i++)//輸入 m個測試實例,所以循環 m次
{
scanf("%d",&n);
Fluction(n);//調用函數,傳參 n
printf("%.2lf\n",sum);//保留兩位小數輸出
}
}
}
int Fluction(int t)//函數定義,實現 1-1/2+1/3-1/4+1/5-1/6+...多項式
{
int sign=1;//定義符號
double x;
sum=0;
for(int i=1;i<=t;i++)//要求前幾項的和就循環幾次
{
x=(double)sign/i;//強制轉變類型
sum+=x;
sign*=-1;
}
return sum;//一定要定義它返回 sum的值,否則,函數會自動返回 0
}