⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 二分法求根.cpp

📁 数值计算 中的各种算法: Lagrame插值,Newton迭代法,Runge-kutta,二分法求根,解线性方程组的Guass列主元消去法,自动选择步长Simpson法
💻 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 + -