您的位置:首页 > 房产 > 建筑 > 保险代理公司_中信建设有限责任公司是国企还是央企_上海关键词优化方法_百度seo新规则

保险代理公司_中信建设有限责任公司是国企还是央企_上海关键词优化方法_百度seo新规则

2025/2/23 19:03:16 来源:https://blog.csdn.net/ox0080/article/details/145772025  浏览:    关键词:保险代理公司_中信建设有限责任公司是国企还是央企_上海关键词优化方法_百度seo新规则
保险代理公司_中信建设有限责任公司是国企还是央企_上海关键词优化方法_百度seo新规则

数学表达式解释器

示例需求

  1. 支持数字、变量、加减乘除运算
  2. 支持函数调用(如 max(2,3)
  3. 能够处理嵌套表达式(如 (x + 5) * max(y,10)

完整代码实现

#include <iostream>
#include <memory>
#include <unordered_map>
#include <vector>
#include <sstream>
#include <cctype>
#include <cmath>// ====================== 解释器核心组件 ======================
class Context {
public:std::unordered_map<std::string, double> variables;double getVariable(const std::string& name) const {auto it = variables.find(name);if (it == variables.end()) throw std::runtime_error("Undefined variable: " + name);return it->second;}
};class Expression {
public:virtual double interpret(const Context& ctx) const = 0;virtual ~Expression() = default;
};// ====================== 终结符表达式 ======================
class Number : public Expression {double value;
public:Number(double v) : value(v) {}double interpret(const Context&) const override { return value; }
};class Variable : public Expression {std::string name;
public:Variable(std::string n) : name(std::move(n)) {}double interpret(const Context& ctx) const override {return ctx.getVariable(name);}
};// ====================== 非终结符表达式 ======================
class AddExpr : public Expression {std::unique_ptr<Expression> left, right;
public:AddExpr(Expression* l, Expression* r) : left(l), right(r) {}double interpret(const Context& ctx) const override {return left->interpret(ctx) + right->interpret(ctx);}
};class MultiplyExpr : public Expression {std::unique_ptr<Expression> left, right;
public:MultiplyExpr(Expression

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com