Ⅰ 如何用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
}