admin 管理员组

文章数量: 1184232


2024年3月8日发(作者:linux镜像)

c++ eval 用法

在C++中,"eval"这个术语通常指的是对字符串表达式的动态求值。然而,C++标准库并没有直接提供类似于动态求值的内建功能。在C++中,代码通常在编译时被转换为机器代码,而不是在运行时解释执行。

如果你需要在运行时动态执行字符串表达式,你可能需要使用一些外部库或者自己实现一个解释器。以下是一个简单的例子,使用了C++17中的`std::variant`和递归下降解析器:

```cpp

#include

#include

#include

#include

class Eval {

public:

using Value = std::variant;

Value evaluate(const std::string& expression) {

pos = 0;

input = expression;

return parseExpression();

}

private:

size_t pos;

std::string input;

char getCurrentChar() {

return (pos < ()) ? input[pos] : '0';

}

void consumeChar() {

if (pos < ()) {

++pos;

}

}

int parseNumber() {

int result = 0;

while (isdigit(getCurrentChar())) {

result = result * 10 + (getCurrentChar() - '0');

consumeChar();

}

return result;

}

double parseDouble() {

double result = static_cast(parseNumber());

if (getCurrentChar() == '.') {

consumeChar();

double fraction = 0.1;

while (isdigit(getCurrentChar())) {

result += (getCurrentChar() - '0') * fraction;

fraction *= 0.1;

consumeChar();

}

}

return result;

}

Value parsePrimary() {

if (isdigit(getCurrentChar())) {

return parseDouble();

} else if (getCurrentChar() == '(') {

consumeChar(); // Consume '('

Value result = parseExpression();

consumeChar(); // Consume ')'

return result;

} else {

// Handle variables, functions, or other primary expressions here

throw std::runtime_error("Unexpected character");

}

}

Value parseTerm() {

Value result = parsePrimary();

while (getCurrentChar() == '*' || getCurrentChar() == '/') {

char op = getCurrentChar();

consumeChar();

Value right = parsePrimary();

if (op == '*') {

if (() == 0 && () == 0) {

result = std::get<0>(result) * std::get<0>(right);

} else {

throw std::runtime_error("Invalid operands for multiplication");

}

} else if (op == '/') {

if (() == 0 && () == 0) {

result = std::get<0>(result) / std::get<0>(right);

} else {

throw std::runtime_error("Invalid operands for division");

}

}

}

return result;

}

Value parseExpression() {

Value result = parseTerm();

while (getCurrentChar() == '+' || getCurrentChar() == '-') {

char op = getCurrentChar();

consumeChar();

Value right = parseTerm();

if (op == '+') {

if (() == 0 && () == 0) {

result = std::get<0>(result) + std::get<0>(right);

} else {

throw std::runtime_error("Invalid operands for addition");

}

} else if (op == '-') {

if (() == 0 && () == 0) {

result = std::get<0>(result) - std::get<0>(right);

} else {

throw std::runtime_error("Invalid operands for subtraction");

}

}

}

return result;

}

};

int main() {

Eval evaluator;

try {

std::string expression = "2 * (3 + 4) / 5";

Eval::Value result = te(expression);

if (() == 0) {

std::cout << "Result: " << std::get<0>(result) << std::endl;

} else {

std::cout << "Result: " << std::get<1>(result) << std::endl;

}

} catch (const std::runtime_error& e) {

std::cerr << "Error: " << () << std::endl;

}

return 0;

}

```

请注意,这只是一个非常基本的例子,仅支持简单的四则运算,而且错误处理非常有限。在实际应用中,你可能需要更复杂的解析器和错误处理机制,具体取决于你的需求。


本文标签: 动态 需要 表达式 字符串 可能