diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
| commit | ac517a2b7625e0aa0862679e961c6349f859ea3b (patch) | |
| tree | e3e27a6b9bd5e2be6150088495c91fc91786ad9d /src/old/formula.h | |
| parent | f8d4301e9fa4f3709258505941e37fab2eadadc6 (diff) | |
| parent | bd865cee5f89116c1f054cd0e5c275e97c2d0a9b (diff) | |
| download | libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.gz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.bz2 libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.xz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.zip | |
The reorg is being put in trunk, I think it's ready. Now we just get to find
out how many applications won't work anymore :)
Diffstat (limited to 'src/old/formula.h')
| -rw-r--r-- | src/old/formula.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/old/formula.h b/src/old/formula.h new file mode 100644 index 0000000..939eb09 --- /dev/null +++ b/src/old/formula.h | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | #ifndef FORMULA_H | ||
| 2 | #define FORMULA_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | |||
| 6 | #include <math.h> | ||
| 7 | #include <stack> | ||
| 8 | #include "sbuffer.h" | ||
| 9 | |||
| 10 | #include "exceptionbase.h" | ||
| 11 | #include "hash.h" | ||
| 12 | |||
| 13 | subExceptionDecl( ParseException ); | ||
| 14 | |||
| 15 | /** | ||
| 16 | * Implements a very simple formula parser that allows use of variables and | ||
| 17 | * custom functions. This is based on a simple calculator-type parser that | ||
| 18 | * executes as it processes, accounting for operator precedence and grouping. | ||
| 19 | */ | ||
| 20 | class Formula | ||
| 21 | { | ||
| 22 | public: | ||
| 23 | Formula(); | ||
| 24 | virtual ~Formula(); | ||
| 25 | |||
| 26 | double run( char *sFormula ); | ||
| 27 | |||
| 28 | typedef Hash<std::string, double> varHash; | ||
| 29 | varHash hVars; | ||
| 30 | |||
| 31 | typedef struct Func | ||
| 32 | { | ||
| 33 | double operator()( double x ) | ||
| 34 | { | ||
| 35 | return 0.0; | ||
| 36 | } | ||
| 37 | } Func; | ||
| 38 | |||
| 39 | typedef Hash<std::string, Func> funcHash; | ||
| 40 | funcHash hFunc; | ||
| 41 | |||
| 42 | typedef struct FuncSin : Func | ||
| 43 | { | ||
| 44 | double operator()( double x ) | ||
| 45 | { | ||
| 46 | return sin( x ); | ||
| 47 | } | ||
| 48 | } FuncSin; | ||
| 49 | |||
| 50 | private: | ||
| 51 | enum | ||
| 52 | { | ||
| 53 | symEOS, | ||
| 54 | symAdd, | ||
| 55 | symSubtract, | ||
| 56 | symMultiply, | ||
| 57 | symDivide, | ||
| 58 | symOpenParen, | ||
| 59 | symCloseParen, | ||
| 60 | symNumber, | ||
| 61 | symVariable, | ||
| 62 | symExponent, | ||
| 63 | symModulus | ||
| 64 | }; | ||
| 65 | |||
| 66 | typedef uint8_t symType; | ||
| 67 | |||
| 68 | std::stack<symType> sOper; | ||
| 69 | std::stack<double> sValue; | ||
| 70 | |||
| 71 | private: | ||
| 72 | symType getPrec( symType nOper ); | ||
| 73 | symType nextToken( char **sBuf ); | ||
| 74 | void reduce( bool bCloseParen = false ); | ||
| 75 | }; | ||
| 76 | |||
| 77 | #endif | ||
