网站建设资讯

NEWS

网站建设资讯

c语言函数例题多项式求和 c语言中计算多项式的函数为

【二级C语言】多项式求和问题

#includestdio.h

在师宗等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、成都网站建设 网站设计制作定制制作,公司网站建设,企业网站建设,品牌网站建设,成都营销网站建设,成都外贸网站建设公司,师宗网站建设费用合理。

double fun(double m)

{

double Sn=1.0,i,t=1.0;

for(i=1.0; ;i++){

t=t*(1.5-i)*m/i;

Sn+=t;

if(t0.000001  t-0.000001)

break;

}

return Sn;

}

int main()

{

double x;//你这里用的居然是int,看了n久fun()函数发现后简直吐一口老血。。。

printf("请输入x:");

scanf("%lf",x);

printf("Sn=%lf\n",fun(x));

return 0;

}

我几乎是在你程序的基础上改的,虽然改了很多,但你应该看得懂~~主要就是把你fun()函数不简洁的地方去了^~^

C语言求多项式的和y=a1x1+a2x2+a3x3+a4x4

#includestdio.h

void main()

{

float a1,a2,a3,a4,y;

printf("enter a1,a2,a3,a4\n");

scanf("%f%f%f%f",a1,a2,a3,a4);

y=a1*1+a2*2+a3*3+a4*4;

printf("%f",y);

return 0;

}

两个多项式相加运算(用c语言)

#includestdio.h

#includemalloc.h

#define Null 0

typedef struct Node

{

int coeff;

int expo;

Node *next;

}listNode,*list;

list createList()

{

list head;

head = (list)malloc(sizeof(listNode));

head = NULL;

printf("want to create a new node?y/n\n");

char ch;

fflush(stdin);

scanf("%c",ch);

while(ch=='Y' || ch== 'y')

{

list p;

p = (list)malloc(sizeof(listNode));

printf("input coeff\n");

int coeff;

scanf("%d",coeff);

p-coeff = coeff;

printf("input expo\n");

int expo;

scanf("%d",expo);

p-expo = expo;

p-next = NULL;

//链表为空的时候,即添加首个元素

if(head == NULL)

{

head=p;//添加代码

}

else

{

list prev,curr;

curr = head;

prev = NULL;

//找到添加位置

while(curr!=NULL curr-expop-expo)

{

prev=curr;

curr=curr-next;//添加代码

}

if(curr!=NULL curr-expo == p-expo)

{

curr-coeff = curr-coeff + p-coeff;

printf("want to create a new node?y/n\n");

fflush(stdin);

scanf("%c",ch);

if(ch=='Y' || ch== 'y')

continue;

else

return head;

}

//插入结点,结点非首

if(prev != NULL)

{

p-next=curr;

prev-next=p;

//添加代码

}

//插入结点,结点为首

else

{

p-next=curr;

head=p;

//添加代码

}

}

printf("want to create a new node?y/n\n");

fflush(stdin);

scanf("%c",ch);

}

return head;

}

list add(list head1,list head2)

{

list head,newNode,ptr1,ptr2,ptr3;

ptr1 = head1;

ptr2 = head2;

head = NULL;

while(ptr1 != NULL ptr2 != NULL)

{

newNode = (list)malloc(sizeof(listNode));

if(ptr1-expo ptr2-expo)

{

newNode-coeff = ptr1-coeff;

newNode-expo = ptr1-expo;

newNode-next = NULL;

ptr1 = ptr1-next;

}

else if(ptr1-expo ptr2-expo)

{

newNode-coeff = ptr2-coeff;

newNode-expo = ptr2-expo;

newNode-next = NULL;

ptr2 = ptr2-next;//添加代码

}

else

{

newNode-coeff = ptr1-coeff + ptr2-coeff;

newNode-expo = ptr1-expo;

newNode-next = NULL;

ptr1 = ptr1-next;

ptr2 = ptr2-next;

}

if(head==NULL)

{

head = newNode;

}

else

{

ptr3 = head;

//添加代码

while(ptr3-next != NULL)

ptr3 = ptr3-next;

ptr3-next = newNode;

}

}

while(ptr1 != NULL)

{

newNode = (list)malloc(sizeof(listNode));

newNode-coeff = ptr1-coeff;

newNode-expo = ptr1-expo;

newNode-next = NULL;

ptr3 = head;

if(ptr3 == NULL)

head = ptr3 = newNode;

else

{

while(ptr3-next != NULL)

ptr3 = ptr3-next;

ptr3-next = newNode;//添加代码

}

ptr1 = ptr1-next;

}

while(ptr2 != NULL)

{

newNode = (list)malloc(sizeof(listNode));

newNode-coeff = ptr2-coeff;

newNode-expo = ptr2-expo;

ptr3 = head;

if(ptr3 == NULL)

head = ptr3 = newNode;

else

{

while(ptr3-next != NULL)

ptr3 = ptr3-next;

ptr3-next = newNode;

}

ptr2 = ptr2-next;

}

return head;

}

void display(list head)

{

list ptr = head;

while(ptr != NULL)

{

if(ptr != head )

printf("+");

printf("%d",ptr-coeff);

printf("x^");

printf("%d",ptr-expo);

ptr = ptr-next;

}

printf("\n");

}

int main(int argc, char* argv[])

{

list head,head1,head2;

printf("input the first list\n");

head1 = createList();

display(head1);

printf("input the second list\n");

head2 = createList();

display(head2);

head = add(head1,head2);

display(head);

return 0;

}

多项式求和的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

}

C语言:多项式求和

for (j=1;j=n;j++)

{

if (j%2==0)

{

j=j*(-1);

}

这段死循环了 0 - 2 - -2 - 2 - -2


文章标题:c语言函数例题多项式求和 c语言中计算多项式的函数为
文章地址:http://cdweb.net/article/doceepg.html