编写一个用矩形法求定积分,这个程序哪里出错了。 答案总是显示0
#include"stdio.h"#include"math.h"doublefun1(doublex){return(sin(x));}doublefun2(doubl...
#include"stdio.h"
#include"math.h"
double
fun1 (double x)
{
return (sin (x));
}
double
fun2 (double x)
{
return (cos (x));
}
double
fun3 (double x)
{
return (exp (x));
}
double
jifen (double (*p) (double x), int a, int b)
{
int i;
double f = 0.0, h, x, y1,y2,area;
h = (b - a) / 10000;
x = a;
y1 = (*p) (x);
for (i = 1; i <= 10000; i++)
{
x += h;
y2 = (*p) (x);
area = y1*h ;
y1 = y2;
f += area;
}
return (f);
}
main ()
{
int a, b, t;
printf ("from which to which?\n");
scanf ("%d%d", &a, &b);
double x = 0.0, y;
printf ("input 1(sin),2(cos),3(exp):\n");
scanf ("%d", &t);
switch (t)
{
case 1:
y = jifen (fun1, a, b);printf ("the answer is %lf\n", y);
break;
case 2:
y = jifen (fun2, a, b);printf ("the answer is %lf\n", y);
break;
case 3:
y= jifen (fun3,a,b);printf("the answer is %lf\n",y);
break;
default:printf("error!\n");
}
} 展开
#include"math.h"
double
fun1 (double x)
{
return (sin (x));
}
double
fun2 (double x)
{
return (cos (x));
}
double
fun3 (double x)
{
return (exp (x));
}
double
jifen (double (*p) (double x), int a, int b)
{
int i;
double f = 0.0, h, x, y1,y2,area;
h = (b - a) / 10000;
x = a;
y1 = (*p) (x);
for (i = 1; i <= 10000; i++)
{
x += h;
y2 = (*p) (x);
area = y1*h ;
y1 = y2;
f += area;
}
return (f);
}
main ()
{
int a, b, t;
printf ("from which to which?\n");
scanf ("%d%d", &a, &b);
double x = 0.0, y;
printf ("input 1(sin),2(cos),3(exp):\n");
scanf ("%d", &t);
switch (t)
{
case 1:
y = jifen (fun1, a, b);printf ("the answer is %lf\n", y);
break;
case 2:
y = jifen (fun2, a, b);printf ("the answer is %lf\n", y);
break;
case 3:
y= jifen (fun3,a,b);printf("the answer is %lf\n",y);
break;
default:printf("error!\n");
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询