?? erfen.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 + -