12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- /*
- * Expression is a simple math expression interpreter.
- * expression support + - * / ^(pow) -(neg)calculate sign
- * expression support "()" to constrol the calculate order
- * the number in expression can only be the real number,don't support the scientific notation.
- * SetExprStr failure will break down the original expression
- * support 26 letter variable name(case insensitive)
- * use SetVar to set the variable value
- * the default variable value is 1
- * when GetResult ,if the expression is not set or the variable is not set, it will return an error code.
- * GetExprErrorStr get the expression error code string
- * GetResultErrorStr get the result error code string
- */
- namespace expInterpreter {
- enum AtomType{ATOM_ERROR = 0, ATOM_SYMBOL, ATOM_CONSTANT, ATOM_VARIABLE, ATOM_END};
- // the struct of a syntax unit, it can be a symbol, a constant or a variable
- typedef struct
- {
- AtomType type;
- union
- {
- char symbol;
- double constant;
- char variable;
- } value;
- } Atom;
- // read a syntax unit from the given string£¬begin at the start£¬finish at the end unit£¬return -1 if it is null.
- int read_atom(Atom *atom, const char *exprStr, int start);
- // expression
- class Expression
- {
- private:
- const static int poland_MAX;
- Atom *poland;
- int poland_N;
- double *var;
- public:
- Expression();
- ~Expression();
-
- const char* GetExprErrorStr(int err); // return the error string according to the error code
- const char* GetResultErrorStr(int err); // return the error string according to the error code
- int SetExprStr(const char *exprStr); // set the expression,generate the reverse polish notation,return 0 if succeed,error code if fail.
- void SetVar(char x, double value); // set the variable value
- int GetResult(double *result); // get the result according to the last set variable and expression,return 0 if succeed,error code if fail.
- const Atom *GetPoland();
- };
- }
|