mirror of
https://github.com/allankoechke/cparse.git
synced 2026-12-05 09:10:00 +00:00
Now it is possible to precompile an expression without binding all its variable values. And bind them only when the expression will be evaluated. This way different evaluations of a single expression may have different sets of variables. This method optimizes the case when you need to re-evaluate a single equation several times in different contexts.
65 lines
1.4 KiB
C++
65 lines
1.4 KiB
C++
// Source: http://www.daniweb.com/software-development/cpp/code/427500/calculator-using-shunting-yard-algorithm#
|
|
// Author: Jesse Brown
|
|
// Modifications: Brandon Amos
|
|
|
|
#ifndef _SHUNTING_YARD_H
|
|
#define _SHUNTING_YARD_H
|
|
|
|
#include <map>
|
|
#include <stack>
|
|
#include <string>
|
|
#include <queue>
|
|
|
|
struct TokenBase {
|
|
virtual ~TokenBase() {}
|
|
};
|
|
|
|
template<class T> class Token : public TokenBase {
|
|
public:
|
|
Token (T t) : val(t) {}
|
|
T val;
|
|
};
|
|
|
|
typedef std::queue<TokenBase*> TokenQueue_t;
|
|
|
|
class calculator {
|
|
private:
|
|
static std::map<std::string, int> opPrecedence;
|
|
static std::map<std::string, int> buildOpPrecedence();
|
|
|
|
public:
|
|
static double calculate(const char* expr,
|
|
std::map<std::string, double>* vars = 0);
|
|
|
|
private:
|
|
static double calculate(TokenQueue_t RPN,
|
|
std::map<std::string, double>* vars = 0);
|
|
static void cleanRPN(TokenQueue_t& rpn);
|
|
static TokenQueue_t toRPN(const char* expr,
|
|
std::map<std::string, double>* vars,
|
|
std::map<std::string, int> opPrecedence=opPrecedence);
|
|
|
|
private:
|
|
TokenQueue_t RPN;
|
|
public:
|
|
~calculator();
|
|
calculator(){}
|
|
calculator(const char* expr,
|
|
std::map<std::string, double>* vars = 0,
|
|
std::map<std::string, int> opPrecedence=opPrecedence);
|
|
void compile(const char* expr,
|
|
std::map<std::string, double>* vars = 0,
|
|
std::map<std::string, int> opPrecedence=opPrecedence);
|
|
double eval(std::map<std::string, double>* vars = 0);
|
|
std::string str();
|
|
};
|
|
|
|
#endif // _SHUNTING_YARD_H
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|