/********************************************************************* * * �� ��: FileZone.h * * ��Ȩ����: Shanghai Baosight Software Co., Ltd. * *********************************************************************/ #ifndef _H_FileZone_H #define _H_FileZone_H #include #include #include #ifdef _WIN32 #include #include #include #else #include #include #include #include #include #endif using namespace baosight; const int PRI_A[] = { 5, 5, 7, 7, 7, 1, 8, 8, 7,/* 7, 7, 7,*/ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 4, 3, 0 }; const int PRI_B[] = { 4, 4, 6, 6, 6, 10, 1, 1, 8,/* 8, 8, 8,*/ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 3, 2, 0 }; class CCalculator { public: struct Symbol { enum SYMTYPE { NUMBER, OPERATOR } type; enum OPTYPE { ADD, // + SUBSTRACT, // - MULTIPLY, // * DIVIDE, // / MOD, // % LEFTBRACE, // ( RIGHTBRACE, // ) COMMA, // , ABS, // || /* ACOS, //acos ASIN, //asin ATAN, //atan */ CEIL, // ȡ�� COS, // cos EXP, // e^ FLOOR, //floor HYPOT, // tangle LN, // ln LOG10, // lg POW, // x^y ROUND, //round SIN, // sin SQRT, // ƽ���� TAN, //tan AND, // & OR, // | STACKEND, }; union { OPTYPE opType; double number; } content; }; public: CCalculator(void); ~CCalculator(void); CCalculator(const string &str); // set expression void SetExp(const string &str); // do calculation bool Calc(double &); private: string m_strExp; int m_curIndex; // stack to hold number and operator stack stkNum, stkOpt; bool DoCalcOfOpt(Symbol &sym); protected: // this is a Parser // return value: // 1, correct // 0, symbol wrong // -1, end of string // -2, string is null int GetNextSymbol(Symbol &sym); // get next character bool GetNextChar(char &ch); // peek adjacent character bool PeekChar(char &ch, bool bPrevious); // return priority of sym inside stack int PriA(Symbol &sym) { if (sym.type == Symbol::OPERATOR) return PRI_A[(int)sym.content.opType]; return -1; }; // return priority of sym outside stack int PriB(Symbol &sym) { if (sym.type == Symbol::OPERATOR) return PRI_B[(int)sym.content.opType]; return -1; }; }; #endif