📄 二分法求根.cpp
字号:
// 黄泳晞(2003402054529) 二分法求根
// 程序中函数为 (x+2)*(x-2)
// 两个根分别为-2和2 由于求单根,输入区域在[-∞,2)或(-2,+∞]之间
#include<iostream.h>
class Hlf
{
public:
print()
{
cout<<"本程序中函数为 (x+2)*(x-2)"<<endl;
cout<<"两个根分别为-2和2 由于求单根,输入区域应在[-∞,2)或(-2,+∞]之间"<<endl;
cout<<"以下测试本程序"<<endl<<endl;
};
float F(float x)//防城函数 (x+2)*(x-2)
{
float temp;
temp=((x+2)*(x-2));
return temp;
};
acc() //算法函数
{
float a,b,esp,Ya,Yx,x;
cout<<"输入区域a到b:";
cin>>a>>b;
cout<<"输入精度:";
cin>>esp;
Ya=F(a);
do{
x=(b+a)/2;
Yx=F(x);
if((Ya*Yx)<0)
b=x;
if((Ya*Yx)>0)
{a=x;Ya=Yx;}
}while((Yx!=0)&&((b-a)>=esp));
cout<<"其根为:"<<x<<endl<<"把根代入函数为:"<<Yx<<endl;
};
};
main()
{
char a;
Hlf t;
t.print();
do{
t.acc();
cout<<endl<<"重新输入?(y/n)";
cin>>a;
}while(a=='y');
if(a=='n')
cout<<"ByeBye"<<endl<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -