Computer.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef _COMPUTER_H_
  2. #define _COMPUTER_H_
  3. #include <stack>
  4. using namespace std;
  5. #define LEVELS 6
  6. #define DERROR 0
  7. #define BIGNUMBER 0 //计算出错时,赋零
  8. #define UNARY 0
  9. #define BINARY 1
  10. /* 运算符类 */
  11. class COperator
  12. {
  13. public:
  14. COperator();
  15. ~COperator();
  16. // 运行符;
  17. CString m_strOperator;
  18. WORD m_nLevel;
  19. WORD m_nType;
  20. WORD m_nStartIndex;
  21. };
  22. typedef stack<COperator*> OperatorStack;
  23. /* 运算数类 */
  24. class COperand
  25. {
  26. public:
  27. COperand();
  28. ~COperand();
  29. BOOL operator ==(const COperand&od);
  30. CString m_strName;
  31. BOOL m_bIsConst;
  32. WORD m_nStartIndex;
  33. };
  34. typedef stack<COperand*> OperandStack;
  35. /* 计算基类 */
  36. class CComputer : public CObject
  37. {
  38. public:
  39. void Destroy();
  40. BOOL ExpressionIsError();
  41. CString GetDigitalString(double* variantValue=0,int num=-1);
  42. int GetErrorNumber();
  43. CString GetErrorInformation();
  44. double computer(double variantValue[],int num);
  45. void SetFormula(CString strFormula);
  46. CComputer(char *pszFormula=NULL);
  47. int GetVariantTableSize(){return m_nOprandANum;}
  48. const COperand* GetVariantTable();
  49. ~CComputer();
  50. public:
  51. int m_nOprandNum;
  52. int m_nOprandANum;
  53. int m_nOperatorNum;
  54. CString m_strFormula;
  55. COperator *m_pOperator;
  56. COperand *m_pOprand;
  57. COperand *m_pOprandA;
  58. int IsFormula();
  59. BOOL GetStack(OperatorStack &Op, OperandStack &Od);
  60. BOOL Initialize();
  61. double Computer(OperatorStack &Operator, OperandStack &Oprand);
  62. BOOL EmptyStack(OperatorStack st);
  63. BOOL EmptyStack(OperandStack st);
  64. BOOL GetOperandStack(OperandStack&Oprand,CString&string);
  65. virtual BOOL GetOperatorStack(OperatorStack&Operator,CString &string);
  66. virtual double computing(const COperator*op,const COperand*oprand);
  67. virtual double computing(const COperator*op,const COperand*Loprand,const COperand*Roprand);
  68. };
  69. /* 实时计算类 */
  70. class CRealComputer :public CComputer
  71. {
  72. public:
  73. CString m_strExpression;
  74. virtual BOOL GetOperatorStack(OperatorStack&Operator,CString &string);
  75. virtual double computing(const COperator*op,const COperand*oprand);
  76. virtual double computing(const COperator*op,const COperand*Loprand,const COperand*Roprand);
  77. virtual int IsFormula();
  78. double GetResult();
  79. };
  80. #endif //_COMPUTER_H_