二分法输入方程,c++
Input equation in bisection method, C++
我有这样的代码:
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
double f(double x);
double biseccion(double a, double b, double tolerancia, int maxiter);
int main()
{
double a, b, raiz;
double tolerancia=0.00000;
int maxiter=25;
cout << "Input begin of interval: ";
cin >> a;
cout << "Input end of interval: ";
cin >> b;
cout << "n";
cout << " # de"<<"n"<<"Iteration"<<"t"<<" A"<<"t"<<" B"<<"t"<<" C"<<"t"<<" f(c)"<<endl;
raiz=biseccion(a,b,tolerancia,maxiter);
cout << "n";
cout << "The root is: "<< raiz <<endl;
return 0;
}
double f(double x)
{
return x*x*x-x-2;
}
double biseccion(double a, double b, double tolerancia, int maxiter)
{
double c;
int numiter=1;
do
{
c=(a+b)/2;
if(f(a)*f(c)<0)
{
b=c;
}
else
{
a=c;
}
cout<<" "<<numiter<<"t"<<"t"<<a<<"t"<<b<<"t"<<c<<"t"<<f(c)<<endl;
numiter++;
}
while((abs(f(c))>tolerancia)&&(numiter<maxiter));
return c;
}
在我的代码中不是写"x*x*x-x-2",我希望用户在要求开始间隔之前输入它。我该怎么做呢?
我尝试使用一个变量来存储"x*x*x-x-2",但没有工作。
您需要解析输入,这可能不像您想象的那么容易,但是有一些库可以帮助您。
muparser.sourceforge.net/
code.google.com/p/expressionparser/
partow.net/programming/exprtk/index.html
也是c#中的一个解决方案,可能也会对您有所帮助。
在。net中有字符串数学计算器吗?
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 如何从用户那里获取方程的输入并在 c++ 中对其进行评估
- 二分法输入方程,c++
- c++中如何让用户输入的方程被求值