64,663
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <math.h>
using namespace std;
class Bifid{
private:
double x1, x2;
public:
Bifid(double a, double b) { x1=a; x2=b;}
double fun(double x){
return 1.0*pow(x,2) + 0.0*x - 5.0;
}
double erfenfa() {
if (fun(x1)==0) return x1;
if (fun(x2)==0) return x2;
if (fun(x1)*fun(x2) > 0) {
cout << "wu jie" << endl;
return 0; }
static double mid=(x1+x2)/2.0;
while(fabs(fun(mid))>1e-10) {
cout << x1 << ' ' << x2 << endl; //检测程序另外加的
mid=(x1+x2)/2.0;
if(fun(x1)*fun(mid) < 0 ) x1=mid;
else x2=mid; }
return mid;
}
};
int main()
{
Bifid a(0.0, 200.0);
cout << a.erfenfa() << endl;
return 0;
}
while(fabs(fun(mid))>1e-10) {
cout << x1 << ' ' << x2 << endl; //检测程序另外加的
mid=(x1+x2)/2.0;
if(fun(x1)*fun(mid) < 0 ) x1=mid;
else x2=mid; }
return mid;
}
#include <iostream>
#include <math.h>
using namespace std;
class Bifid{
private:
double x1, x2;
public:
Bifid(double a, double b) { x1=a; x2=b;}
double fun(double x){
return 1.0*pow(x,2) + 0.0*x - 5.0;
}
double erfenfa()
{
if (fun(x1)==0) return x1;
if (fun(x2)==0) return x2;
if (fun(x1)*fun(x2) > 0)
{
cout << "wu jie" << endl;
return 0;
}
static double mid=(x1+x2)/2.0;
while(fabs(fun(mid)) < 1e-10)
{
cout << x1 << ' ' << x2 << endl; //检测程序另外加的
mid=(x1+x2)/2.0;
if(fun(x1)*fun(mid) < 0 )
x1=mid;
else x2=mid;
}
return mid;
}
};
int main()
{
Bifid a(1.0, 200.0);
cout << a.erfenfa() << endl;
return 0;
}