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

📄 erfen.cpp

📁 这是二分法求根的源码
💻 CPP
字号:
/*该程序据第6页流程图完成二分法求根*/
                       #include "stdafx.h"
                       #include<stdio.h>
                       #include<math.h>
                       #include<iostream.h>
double f(double x)           
{
   double result;
     result=(pow(x,3)-x-1);   //求函数值
	  return result;
}
void root(double a,double b,double precision)
{
  int k=0;
  double x,y,y0;
  y0=f(a);
  do
  {   
	  k++;
	  x=(a+b)/2;
	  y=f(x);
      if(y*y0>0)
		  a=x;
	  else
		  b=x;
 }while((b-a)>=precision);
     cout<<"二分次数k="<<k-1<<endl;
       printf("x=%.4f,y=%.4f\n",x,y);
}
int main(int argc, char* argv[])
{
   float a,b,precision;
     printf("Please enter the range and the precision:\n");
	   scanf("%f%f%f",&a,&b,&precision);
	     root(a,b,precision);
	return 0;
}


⌨️ 快捷键说明

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