📄 快速弦截法.cpp
字号:
//*************************************
//程序功能:快速弦截法 求线性方程的解
//
//作 者:杭永东
//
//日 期: 2002.03.28
//*************************************
#include <math.h>
#include <iostream.h>
#include <stdio.h>
//*************************************
//
// 注:更改此宏定义的表达式可以解不同的线性方程
// 根据宏定义要求,x必须以(x)形式出现式中
//
//*************************************
#define f(x) x-tan((x))
long double x0,x1,e;
long double xq(long double x0,long double x1)
{
printf("\n%.9f",x0);
long double f0=f(x0);
long double f1=f(x1);
long double x2=x1-((x1-x0)/(f1-f0))*f1;
long double f2=f(x2);
if((((x2-x1)>0)? (x2-x1): (x1-x2))<e || ((f2>0) ?f2 :(0.0L-f2))<e)
return x2;
return xq(x1,x2);
}
int NoAnswer(long double a,long double b)
{
if(((f(a))*(f(b)))>0.0L)
{
cout<<"There isn't answer between "<<a<<" and "<<b;
return 1;
}
else
return 0;
}
main()
{
cout<<"Please input x0,x1,e:";
cin>>x0>>x1>>e;
if(NoAnswer(x0,x1))
return;
printf("\n%0.9f",xq(x0,x1));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -