Computer.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. CString m_operator;
  17. WORD m_level;
  18. WORD m_type;
  19. WORD m_startIndex;
  20. };
  21. typedef stack<COperator*> OperatorStack;
  22. /* 运算数类 */
  23. class COperand
  24. {
  25. public:
  26. COperand();
  27. ~COperand();
  28. BOOL operator ==(const COperand&od);
  29. CString m_name;
  30. BOOL m_IsConst;
  31. WORD m_startIndex;
  32. };
  33. typedef stack<COperand*> OperandStack;
  34. /* 计算基类 */
  35. class CComputer : public CObject
  36. {
  37. public:
  38. void Destroy();
  39. BOOL ExpressionIsError();
  40. CString GetDigitalString(double* variantValue=0,int num=-1);
  41. int GetErrorNumber();
  42. CString GetErrorInformation();
  43. double computer(double variantValue[],int num);
  44. void SetFormula(CString formula);
  45. CComputer(char*formula=0);
  46. int GetVariantTableSize(){return m_oprandANum;}
  47. const COperand* GetVariantTable();
  48. ~CComputer();
  49. public:
  50. int m_oprandNum;
  51. int m_oprandANum;
  52. int m_operatorNum;
  53. CString m_formula;
  54. COperator*m_operator;
  55. COperand*m_oprand;
  56. COperand*m_oprandA;
  57. int IsFormula();
  58. BOOL GetStack(OperatorStack &Op, OperandStack &Od);
  59. BOOL Initialize();
  60. double Computer(OperatorStack &Operator, OperandStack &Oprand);
  61. BOOL EmptyStack(OperatorStack st);
  62. BOOL EmptyStack(OperandStack st);
  63. BOOL GetOperandStack(OperandStack&Oprand,CString&string);
  64. virtual BOOL GetOperatorStack(OperatorStack&Operator,CString &string);
  65. virtual double computing(const COperator*op,const COperand*oprand);
  66. virtual double computing(const COperator*op,const COperand*Loprand,const COperand*Roprand);
  67. };
  68. /* 实时计算类 */
  69. class CRealComputer :public CComputer
  70. {
  71. public:
  72. CString m_strExpression;
  73. virtual BOOL GetOperatorStack(OperatorStack&Operator,CString &string);
  74. virtual double computing(const COperator*op,const COperand*oprand);
  75. virtual double computing(const COperator*op,const COperand*Loprand,const COperand*Roprand);
  76. virtual int IsFormula();
  77. double GetResult();
  78. };
  79. #endif //_COMPUTER_H_