添加两个函数
Addition of two functions
我正在用c++做一个解析函数,它接受字符串和双精度体作为参数,并返回字符串的"值"。
代码如下:
double evaluate (char * toParse, int length, double x)
{
// Case 'x'
if ((toParse[0] == 'x') &&
(length == 1))
{
return x;
}
// Case value
char * endptr;
double num = strtod(toParse, &endptr);
if(endptr - toParse == length)
{
return num;
}
// Parsing
int nBrackets = 0;
for (int i = 0; i < length; i++)
{
if (toParse[i] == '(')
{
nBrackets++;
}
else if (toParse[i] == ')')
{
nBrackets--;
}
// Remove brackets.
double _x = (toParse[0] == '(' && toParse[i-1] == ')' ) ?
evaluate(&toParse[1], i-2, x) : evaluate(toParse, i, x);
double _y = (toParse[i+1] == '(' && toParse[length-1] == ')' ) ?
evaluate(&toParse[i+2], length - (i+1) - 2, x) : evaluate (&toParse[i+1] , length - (i+1), x);
// Supports +, -, * and /
if (nBrackets == 0 &&
toParse[i] == '+')
{
return _x + _y;
}
else if (nBrackets == 0 &&
toParse[i] == '-')
{
return _x - _y;
}
else if (nBrackets == 0 &&
toParse[i] == '*')
{
return _x * _y;
}
else if (nBrackets == 0 &&
toParse[i] == '/')
{
return _x / _y;
}
}
return 0.;
}
int main()
{
cout << evaluate("((4*x)+7)-x", 11, 5.) << endl;
// Outputs 22, which sounds correct.
return 0;
}
它远非完美无瑕(对操作符没有优先级,如果字符串包含太多括号则不起作用,等等),但我想删除双x参数,并直接处理函数。(因为我想绘制函数,如果我不处理函数,我将不得不为x的每个值解析相同的字符串…)
有可能吗?我的意思是,像这样做:
double (double) operator+ (double f(double), double g(double))
{
double h (double x)
{
return f(x)+g(x);
}
return h;
}
当然,它不起作用。有什么想法吗?(类等)
谢谢。
可以使用函数指针。我不太明白,如果你想要一个函数指针作为参数,但这是你可以做到的:
typedef double (*handler) (double);
double add(handler x, handler y);
double sub(handler x, handler y);
double func1(double n);
double func2(double n);
int main()
{
double (*funcPtr[256]) (double);
funcPtr['+'] = func1;
funcPtr['-'] = func2;
double answer = funcPtr['+'](2));
}
double func1(double n)
{
return n;
}
double func2(double n)
{
return n;
}
double add(handler x, handler y)
{
return x(2) + y(2);
}
double sub(handler x, handler y)
{
return x(4) - y(2);
}
如果它不是你想要的,但像下面的代码让我知道,我会编辑我的答案:
funcPtr[toParse[i]](2, 2); // toParse[i] is '+' it will then call add(2,2)
256大小的funcPtr
数组相对于sizeof(char)
。您应该确保index
是数组中的实际值,否则您将访问越界
我想到了一个lambda。不幸的是,我认为从捕获函数参数的函数返回lambda是未定义的行为。你也不能重载内建类型,比如函数指针。你可以这样做:
#include <iostream>
#include <functional>
struct CombinedFunction{
enum class Operator{ add, subtract, multiply, divide } op;
CombinedFunction(std::function<double(double)> f1, std::function<double(double)> f2, Operator op) : f1(std::move(f1)), f2(std::move(f2)), op(op){
}
std::function<double(double)> f1, f2;
double operator()(double d){
switch (op){
case Operator::add:
return f1(d) + f2(d);
break;
case Operator::subtract:
//...
break;
//...
}
}
};
std::function<double(double)> operator+ (std::function<double(double)> f, std::function<double(double)> g){
return CombinedFunction(f, g, CombinedFunction::Operator::add);
}
double f(double x){
return 2 * x;
}
double g(double x){
return 3 * x;
}
int main(){
auto h = std::function<double(double)>(f) + g;
std::cout << h(2);
auto h2 = f + h + g;
std::cout << h2(2);
}
它不能很好地扩展,效率也不高。也许有一个模板解决方案可以在编译时完成所有这些。
相关文章:
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 两个函数模板候选项.将一个参数作为引用后,选择不太专业的模板
- 如何比较两个函数的速度和性能
- 如何在构造器的成员初始值设定项列表中调用两个函数?
- 如何将这两个函数组合成一个实现?
- 将具有相反操作数的两个函数重构为一个
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 具有相同主体的两个函数具有不同的名称
- 通过作为类的公共成员的两个函数将函数作为参数传递
- 如何使用排序和比较这两个函数在 c++ 中对字符数组进行排序?
- 是否可以在C++中将两个函数一起添加
- C++ 两个函数除了一行之外执行相同的操作
- 如何编写连接两个函数的函数
- 如何执行两个函数参数具有相同的模板类型
- 当两个函数位于一行中时,堆栈框架的样子
- 为什么这两个函数的行为不同
- 为什么当我有两个函数时编译器没有显示错误,一个将采用基类,一个将派生类作为参数
- 系统堆栈中的两个函数的递归调用(将不同数量的数组作为参数传递)有什么区别
- 如何在一个构造函数中使用两个可变参数模板参数来绑定两个函数
- 如何在c++中的两个函数中传递对象的同一实例