使用exprtk进行四则运算,逻辑运算求平方根,简单定积分

#include <iostream>
#include <string>
#include <map>
#include "exprtk.hpp"
using namespace std;
typedef exprtk::symbol_table<double> symbol_table_t;
typedef exprtk::expression<double> expression_t;
typedef exprtk::parser<double> parser_t;
 

//四则运算,传入 2 * (3 + 4)
double evaluateArithmeticExpression(const std::string& expression_string);
//逻辑运算   x > 5 and y < 10 
bool evaluateLogicalExpression(const std::string& expression_string, double& x, double& y);
//sqrt 4.0
double evaluateMathExpression(const std::string& expression_string, double x);
//定积分  x^2  在0到1的定积分
template <typename T>
double calculateIntegral(const std::string& expression_string, T lower_bound, T upper_bound);
int main() {
    //四则
    std::string arithmetic_expr = "2 * (3 + 4)";
    double result_arithmetic = evaluateArithmeticExpression(arithmetic_expr);
    std::cout << "Result : " << result_arithmetic << std::endl;

    //逻辑
    std::string logical_expr = "x > 5 and y > 10";
    double x = 6.0;
    double y = 8.0;

    bool result_logical = evaluateLogicalExpression(logical_expr, x, y);
    if (result_logical) {
        std::cout << "Result: " <<  "true" <<"\n";
    }
    else {
        std::cout << "Result: " << "false"<<"\n";
    }
    //sqrt
    std::string expression_string = "sqrt(x)"; // Set the expression string
    double sqrt_x = 4.0; // Set the value of x
    double result = evaluateMathExpression(expression_string, sqrt_x);

    if (!std::isnan(result)) {
        std::cout << "Result: " << result << std::endl;
    }
    else {
        std::cout << "Error: Expression evaluation failed." << std::endl;
    }
    //定积分
    std::string expression_djf = "x^2"; // Expression to integrate
    double lower_bound = 0.0;
    double upper_bound = 1.0;

    double integral_result = calculateIntegral(expression_djf, lower_bound, upper_bound);

    if (!std::isnan(integral_result)) {
        std::cout << "Integral result: " << integral_result << std::endl;
    }
    else {
        std::cout << "Error: Integral calculation failed." << std::endl;
    }


    return 0;
}


// 函数用于计算四则运算表达式的结果
double evaluateArithmeticExpression(const std::string& expression_string) {
    symbol_table_t symbol_table;
    expression_t expression;

    symbol_table.add_constants();
    expression.register_symbol_table(symbol_table);

    exprtk::parser<double> parser;

    if (parser.compile(expression_string, expression)) {
        double result = expression.value();
        return result;
    }
    else {
        std::cout << "Arithmetic expression compilation failed!" << std::endl;
        return std::numeric_limits<double>::quiet_NaN(); // 返回 NaN 表示计算失败
    }
}

// 函数用于计算逻辑表达式的结果
bool evaluateLogicalExpression(const std::string& expression_string, double& x, double& y) {
    symbol_table_t symbol_table;
    expression_t expression;
    parser_t parser;

    symbol_table.add_variable("x", x);
    symbol_table.add_variable("y", y);
    symbol_table.add_constants();

    expression.register_symbol_table(symbol_table);

    if (parser.compile(expression_string, expression)) {
        return expression.value();
    }
    else {
       // std::cout << "Logical expression compilation failed!" << std::endl;
        return false;
    }
}

double evaluateMathExpression(const std::string& expression_string, double x) {
    typedef exprtk::symbol_table<double> symbol_table_t;
    typedef exprtk::expression<double> expression_t;
    typedef exprtk::parser<double> parser_t;

    // Create symbol table, expression, and parser instances
    symbol_table_t symbol_table;
    expression_t expression;
    parser_t parser;

    // Define the variable x in the symbol table
    symbol_table.add_variable("x", x);

    // Add the sqrt function to the symbol table
    symbol_table.add_function("sqrt", std::sqrt);

    // Register the symbol table with the expression
    expression.register_symbol_table(symbol_table);

    // Compile the expression from the input string
    if (parser.compile(expression_string, expression)) {
        return expression.value();
    }
    else {
        // Handle compilation error (e.g., by returning a special value or throwing an exception)
        return std::numeric_limits<double>::quiet_NaN(); // Return NaN for error
    }
}
template <typename T>
double calculateIntegral(const std::string& expression_string, T lower_bound, T upper_bound) {
    // Use the typedefs for T instead of double if you're using a template
//typedef exprtk::symbol_table<T> symbol_table_t;
  //  typedef exprtk::expression<T> expression_t;
   // typedef exprtk::parser<T> parser_t;

    symbol_table_t symbol_table;
    expression_t expression;
    parser_t parser;

    // Define the variable x in the symbol table
    T x_value = lower_bound; // Create a separate variable to hold the value of x
    symbol_table.add_variable("x", x_value);

    // Register the symbol table with the expression
    expression.register_symbol_table(symbol_table);

    // Parse the expression string
    if (parser.compile(expression_string, expression)) {
        T result = 0.0;
        T step = 0.0001; // Step size for numerical integration
        T x = lower_bound;

        // Numerical integration using the rectangle method (Riemann sum)
        while (x < upper_bound) {
            // Set the value of the variable x
            x_value = x; // Update the x_value that 'x' refers to
            result += expression.value() * step;
            x += step;
        }

        return result;
    }
    else {
        // Handle compilation error
        return std::numeric_limits<T>::quiet_NaN(); // Return NaN for error
    }
}


#exprtk##c++##数学计算#
全部评论

相关推荐

King987:模板换一个,这个有些丑,在校生的话建议丰富一下在校经历,各种比赛之类的,项目的话你名字是一点儿不包装呀,还有项目描述,像是直接从课程里抄出来的,这里建议自己再改改,说白了就是自己包装一下,改不好可以聊我
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务