用c语言做多项式函数 求多项式c语言
多项式计算 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;
}
用c语言算多项式函数
#include double x; double f(int n){ if(n==0)return 1.0; else if(n==1)return x; return (2*n-1)*x-(n-1)-(n-1)*f(n-2)/n;} int main(){ int a; printf("输入n,x\n"); scanf("%d%lf",a,x); printf("%lf",f(a)); return 0;}
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, degree);
printf("\nplease enter X value:");
scanf("%lf", x);
printf("the answer is %.2lf\n", eval_poly(coeff, degree, x));
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语言实现
#includestdio.h 声明部分:源代码含有2个文件
#includemalloc.h
typedef struct pnode // 定义指针//
{int coef; //定义系数//
int exp; //定义指数//
struct pnode *next;
}pnode;
pnode * creat() //creat函数用来存放多项式//
{int m,n;
pnode *head,*rear,*s;
head=(pnode *)malloc(sizeof(pnode));
rear=head;
printf("\n输入指数(按递增顺序输入):");
scanf("%d",m);
printf("输入一元式系数(0为退出):");
scanf("%d",n);
do
{
s=(pnode *)malloc(sizeof(pnode));
s-coef=n; //n为系数//
s-exp=m; //m为指数//
rear-next=s;
s-next=NULL;
rear=s;
printf("\n输入指数(按递增顺序输入):");
scanf("%d",m);
printf("输入一元式系数(0为退出):");
scanf("%d",n);
}while(n);
return head;
}
pnode * add(pnode *heada,pnode *headb)
{pnode *headc,*a,*b,*s,*rearc;
int sum;
a=heada-next;b=headb-next;
headc=(pnode *)malloc(sizeof(pnode));
rearc=headc;
//多项式的存放//都放到s中里//
while(a!=NULLb!=NULL) //指数相等,则系数相加。//
{
if(a-exp==b-exp)
{ sum=a-coef+b-coef;
if(sum)
{s=(pnode *)malloc(sizeof(pnode));
s-coef=sum;
s-exp=a-exp;
rearc-next=s;
rearc=s;
a=a-next;
b=b-next;}
else
{a=a-next;
b=b-next;
}
}
else if(a-expb-exp)
//a指数如果小于b,则a放到s中//
{ s=(pnode *)malloc(sizeof(pnode));
s-coef=a-coef;
s-exp=a-exp;
rearc-next=s;
//用下一个结点s取代下一个c//
rearc=s;
a=a-next;
}
else //如果a的指数大,则b放到s中//
{ s=(pnode *)malloc(sizeof(pnode));
s-coef=b-coef;
s-exp=b-exp;
rearc-next=s;
rearc=s;
b=b-next;
}
}
if(a)
{while(a!=NULL) //b空了放a中的项//
{s=(pnode *)malloc(sizeof(pnode));
s-coef=a-coef;
s-exp=a-exp;
rearc-next=s;
s-next=NULL;
rearc=s;
a=a-next;
}
}
else if(b)
{while(b!=NULL) //a空了放b中的项//
{s=(pnode *)malloc(sizeof(pnode));
s-coef=b-coef;
s-exp=b-exp;
rearc-next=s;
s-next=NULL;
rearc=s;
b=b-next;
}}
return headc;
}
void main()
{pnode *a,*b,*c;
printf("建立A:");
a=creat();
printf("\n建立B:");
b=creat();
c=add(a,b);
c=c-next;
printf("%dx^%d",c-coef,c-exp);
c=c-next;
while(c!=NULL)
{printf("+%dx^%d",c-coef,c-exp);
c=c-next;
}
}
分享标题:用c语言做多项式函数 求多项式c语言
标题链接:http://azwzsj.com/article/hicdji.html