?? bisection.cpp
字號:
// bisection.cpp : 定義控制臺應用程序的入口點。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double fabs(double,double);
double f(double);
double x1 ,x2 ,x = 0;
int k = 0;
do
{
cout<<"請輸入[a1,b1]區間a1和b1的值:";
cin >> x1 >> x2;
cout << "a1="<< x1 << " b1=" << x2<<endl;
}
while(f(x1) > 0 && f(x2) > 0 || x1 > x2);
printf("f(x)= x*x*x*x*x*x - x - 1\n");
do
{
x = (x1 + x2) / 2;
if(f(x1) * f(x) < 0)
x2 = x;
else
x1 = x;
k++;
if(k > 1000)
{
//cout<<"\n計算超時,找不到精確結果\n";
break;
}
else if(fabs(f(x),0) >= 1e-5)
{
cout<<"結果為:"<<x<<" 分半次數為:"<<k<<endl;
}
}
while(1);
system("pause");
return 0;
}
double f(double x)
{
return x*x*x*x*x*x - x - 1;
}
double fabs(double a,double b)
{
return (a>b?(a-b):(b-a));
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -