eis/inc/zad/CCalculator.h

120 lines
2.6 KiB
C
Raw Normal View History

/*********************************************************************
*
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: FileZone.h
*
* <EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: Shanghai Baosight Software Co., Ltd.
*
*********************************************************************/
#ifndef _H_FileZone_H
#define _H_FileZone_H
#include <vector>
#include <dirent.h>
#include <stdarg.h>
#ifdef _WIN32
#include <Winsock2.h>
#include <string>
#include <cmath>
#else
#include <sys/stat.h>
#include <zlib/MemTrk.h>
#include <cmath>
#include <string>
#include <stack>
#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, // ȡ<><C8A1>
COS, // cos
EXP, // e^
FLOOR, //floor
HYPOT, // tangle
LN, // ln
LOG10, // lg
POW, // x^y
ROUND, //round
SIN, // sin
SQRT, // ƽ<><C6BD><EFBFBD><EFBFBD>
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<Symbol> 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