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

📄 3_5.cpp

📁 c++书籍的源代码
💻 CPP
字号:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
float f(float x);/*函数声明*/
const float EPS=1e-5f;
 int main()
{
 float root(float x1,float x2);/*函数声明*/
 float x1,x2,x;
 do
 {
  cout<<"请输入求根区间(x1,x2):"<<endl;
  cin>>x1>>x2;
 }while(f(x1)*f(x2)>=0);/*确保区间(x1, x2)内必有一个实根*/
 x=root(x1,x2); /*函数调用*/
 cout<<"方程的根是"<<setw(7)<<setprecision(4)<<x<<endl;
 return(0);
}
 float root(float x1,float x2) /*函数定义*/
{
 float xmid(float x1,float x2); /*函数声明*/
 float x,y,y1;
 do
 {
  x=xmid(x1,x2); 
  y1=f(x1);
  y=f(x);
  if(y*y1<0)/*f(x)与f(x1)异号*/
   x2=x;
  else
   x1=x;
 }while(fabs(y)>=EPS);
 return(x);
}
 float xmid(float x1,float x2) /*函数定义*/
{
 float x;
 x=(x1+x2)/2;
 return(x);
}
 float f(float x) /*函数定义*/
{
 float y;
 y=pow(x,3)-8*pow(x,2)+12*x-30;
 return(y);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -