一个c++编程问题-二分法求方程的根
这是我自己写的一个用二分法求根的程序,用的是编译过程有三处错误,请某位高手拷去运行一下,指出有哪些错误,非常感谢!#include<iostream>usingnames...
这是我自己写的一个用二分法求根的程序,用的是编译过程有三处错误,请某位高手拷去运行一下,指出有哪些错误,非常感谢!
#include<iostream>
using namespace std;
double func (const double x);
double findRoot( double first, double last,const double e):
int main ()
{
double a,b ,c,root;
cout<<"Input a ";
cin>>a;
cout<<"Input b";
cin>>b;
cout<<"Input c";
cin>>c;
root=findRoot(a,b,c);
cout<<"The root is"<<root<<endl;
return 0;
}
double func(const double x)
{
double y;
y=x*(x+5)*(x-1);
return y;
}
double findRoot(double first, double last,const double e)
{
double mid,y0,y1,y2;
mid=(first+last)/2;
while((last-first)>e)
{
y0=func (mid);
y1=func(first);
y2=func(last);
if (y0==0) return mid;
else if(y0*y1<0) last=mid;
else left=mid;
mid=(first+last)/2;
}
return mid;
}
编译的错误一共有四个:
1>c:\documents and settings\jie.z\my documents\visual studio 2008\projects\数值积分2\数值积分2\二分法求方程的根.cpp(6) : error C2062: 意外的类型“int”
1>c:\documents and settings\jie.z\my documents\visual studio 2008\projects\数值积分2\数值积分2\二分法求方程的根.cpp(26) : error C2084: 函数“double findRoot(double,double,const double)”已有主体
1>c:\documents and settings\jie.z\my documents\visual studio 2008\projects\数值积分2\数值积分2\二分法求方程的根.cpp(26) : error C2084: 函数“double findRoot(double,double,const double)”已有主体
1>c:\documents and settings\jie.z\my documents\visual studio 2008\projects\数值积分2\数值积分2\二分法求方程的根.cpp(36) : error C2659: “=”: 作为左操作数
是“else first= mid” 啊 展开
#include<iostream>
using namespace std;
double func (const double x);
double findRoot( double first, double last,const double e):
int main ()
{
double a,b ,c,root;
cout<<"Input a ";
cin>>a;
cout<<"Input b";
cin>>b;
cout<<"Input c";
cin>>c;
root=findRoot(a,b,c);
cout<<"The root is"<<root<<endl;
return 0;
}
double func(const double x)
{
double y;
y=x*(x+5)*(x-1);
return y;
}
double findRoot(double first, double last,const double e)
{
double mid,y0,y1,y2;
mid=(first+last)/2;
while((last-first)>e)
{
y0=func (mid);
y1=func(first);
y2=func(last);
if (y0==0) return mid;
else if(y0*y1<0) last=mid;
else left=mid;
mid=(first+last)/2;
}
return mid;
}
编译的错误一共有四个:
1>c:\documents and settings\jie.z\my documents\visual studio 2008\projects\数值积分2\数值积分2\二分法求方程的根.cpp(6) : error C2062: 意外的类型“int”
1>c:\documents and settings\jie.z\my documents\visual studio 2008\projects\数值积分2\数值积分2\二分法求方程的根.cpp(26) : error C2084: 函数“double findRoot(double,double,const double)”已有主体
1>c:\documents and settings\jie.z\my documents\visual studio 2008\projects\数值积分2\数值积分2\二分法求方程的根.cpp(26) : error C2084: 函数“double findRoot(double,double,const double)”已有主体
1>c:\documents and settings\jie.z\my documents\visual studio 2008\projects\数值积分2\数值积分2\二分法求方程的根.cpp(36) : error C2659: “=”: 作为左操作数
是“else first= mid” 啊 展开
3个回答
推荐于2016-02-04
展开全部
//---------------------------------------------------------------------------
#include<iostream>
using namespace std;
double func (const double x);
double findRoot( double first, double last,const double e); /*注意这里,应该是分号而不是冒号*/
int main ()
{
double a,b ,c,root;
cout<<"Input a ";
cin>>a;
cout<<"Input b";
cin>>b;
cout<<"Input c";
cin>>c;
root=findRoot(a,b,c);
cout<<"The root is"<<root<<endl;
return 0;
}
double func(const double x)
{
double y;
y=x*(x+5)*(x-1);
return y;
}
double findRoot(double first, double last,const double e)
{
double mid,y0,y1,y2;
mid=(first+last)/2;
while((last-first)>e)
{
y0=func (mid);
y1=func(first);
y2=func(last);
if (y0==0) return mid;
else if(y0*y1<0) last=mid;
else first=mid; /*注意这里*/
mid=(first+last)/2;
}
return mid;
}
//---------------------------------------------------------------------------
#include<iostream>
using namespace std;
double func (const double x);
double findRoot( double first, double last,const double e); /*注意这里,应该是分号而不是冒号*/
int main ()
{
double a,b ,c,root;
cout<<"Input a ";
cin>>a;
cout<<"Input b";
cin>>b;
cout<<"Input c";
cin>>c;
root=findRoot(a,b,c);
cout<<"The root is"<<root<<endl;
return 0;
}
double func(const double x)
{
double y;
y=x*(x+5)*(x-1);
return y;
}
double findRoot(double first, double last,const double e)
{
double mid,y0,y1,y2;
mid=(first+last)/2;
while((last-first)>e)
{
y0=func (mid);
y1=func(first);
y2=func(last);
if (y0==0) return mid;
else if(y0*y1<0) last=mid;
else first=mid; /*注意这里*/
mid=(first+last)/2;
}
return mid;
}
//---------------------------------------------------------------------------
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询