📄 ex04-05.cpp
字号:
//EX04-06.cpp
//find the root of y=exp(-x)-x using the method of "BISECTION"
#include <iostream.h> //cout,cin
#include <math.h> // exp()
#include <iomanip.h> // setw()
#include <conio.h> // getch()
float root(float x);
void main()
{ float xl,xu,xr; //xl:lower xu:upper xr:root
int count = 0;
cout << "Enter xl,xu:";
cin >> xl >> xu;
cout << "=============================\n";
cout << "No xl xu xr\n";
cout << "=============================\n";
if (root(xl)*root(xu)<0)
{ do
{ xr = (xl + xu)/2;
count++;
if (root(xl)*root(xr)<0)
xu = xr;
if (root(xl)*root(xr)>0)
xl = xr;
cout << setw(2) << count << " "
<< setw(8) << setiosflags(ios::fixed) << setprecision(6)<< xl << " "
<< setw(8) << setiosflags(ios::fixed) << setprecision(6)<<xu << " "
<< setw(8) << setiosflags(ios::fixed) << setprecision(6)<<xr << endl;
} while (count<=20);
cout << "=============================\n";
} //for < 0
getch(); //pause
}
//--------- function of exp(-x)-x
float root(float x)
{ return exp(-x)-x; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -