C语言二分法求方程(找问题)
#include<stdio.h>#include<math.h>#defineesp1e-6#definef(a)(((2*a)*a+3)*a-6)voidmain()...
#include<stdio.h>
#include<math.h>
#define esp 1e-6
#define f(a) (((2*a)*a+3)*a-6)
void main()
{
float m,n,r;
printf("Please input two fugures:\n");///**/
scanf("%lf%lf\n",&m,&n);
for(;;)
{
scanf("%lf%lf\n",&m,&n);
if(f(m)*f(n)>=0)
{
printf("There is no consequence!Please input again:\n");
scanf("%lf%lf\n",&m,&n);
}
else
break;
}
while(fabs(f(m)-f(n))>=esp)
{
r=(m+n)/2;
if(f(m)*f(r)<0)
n=r;
else
if(f(m)*f(r)>0)
m=r;
else
break;
}
printf("Root is %lf.\n",(m+n)/2);
}
我用的vs 2010,又没可能是编译器的问题? 展开
#include<math.h>
#define esp 1e-6
#define f(a) (((2*a)*a+3)*a-6)
void main()
{
float m,n,r;
printf("Please input two fugures:\n");///**/
scanf("%lf%lf\n",&m,&n);
for(;;)
{
scanf("%lf%lf\n",&m,&n);
if(f(m)*f(n)>=0)
{
printf("There is no consequence!Please input again:\n");
scanf("%lf%lf\n",&m,&n);
}
else
break;
}
while(fabs(f(m)-f(n))>=esp)
{
r=(m+n)/2;
if(f(m)*f(r)<0)
n=r;
else
if(f(m)*f(r)>0)
m=r;
else
break;
}
printf("Root is %lf.\n",(m+n)/2);
}
我用的vs 2010,又没可能是编译器的问题? 展开
1个回答
展开全部
#include<stdio.h>
#include<math.h>
#define esp 1e-6
#define f(a) (((2*a)*a+3)*a-6)
void main()
{
double m,n,r; //你在下面都用%lf输入/输出,所以这几个数应该是double
printf("Please input two fugures:\n");///**/
scanf("%lf%lf\n",&m,&n);
for(;;)
{
scanf("%lf%lf\n",&m,&n);
if(f(m)*f(n)>=0)
{
printf("There is no consequence!Please input again:\n");
scanf("%lf%lf\n",&m,&n);
}
else
break;
}
while(fabs(f(m)-f(n))>=esp)
{
r=(m+n)/2;
if(f(m)*f(r)<0)
n=r;
else
//if(f(m)*f(r)>0) //多余的
m=r;
//else //多余的
// break; //多余的
}
printf("Root is %lf.\n",(m+n)/2);
}
追问
应该不是这个问题,输入以后回车直接没反应,只显示输入的数据。依然谢谢啦!
追答
//在你的源程序上修改了一下,调试通过
#include<stdio.h>
#include<math.h>
#define esp 1e-6
#define f(a) (((2*(a))*(a)+3)*(a)-6) //这地方的每一个a的左右都要加括号才是正确的,要不然负数可能会出现问题
int main()
{
double m,n,r; //你在下面都用%lf输入/输出,所以这几个数应该是double
printf("Please input two fugures:\n");///**/
for(;;){
scanf("%lf%lf",&m,&n);
if(f(m)*f(n)>=0)
printf("There is no consequence!Please input again:\n");
else
break;
}
while(fabs(f(m)-f(n))>=esp)
{
r=(m+n)/2;
if(f(m)*f(r)<0)
n=r;
else
m=r;
}
printf("Root is %lf.\n",(m+n)/2);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询