ExpressionTranslation.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <stack>
  5. #include <map>
  6. #include <list>
  7. #include <algorithm>
  8. #include "LogLocalHelper.h"
  9. #include "Log4CPP.h"
  10. using namespace std;
  11. //去除所有空格
  12. void RemoveSomeChar(string& text, char ascii);
  13. //截取非逻辑字符串
  14. string GetKeyValueString(const string& str);
  15. void SplitKeyValueOprString(const string& str, string& key,string& value,string& opr);
  16. //****************节点结构体**************************
  17. //表达式二叉树形式:支持算数表达式、逻辑表达式解析(二者不可同时启用)
  18. struct TreeNode;
  19. //****************表达式类**************************
  20. using SMGetValueCallback = std::function<bool(string)>;
  21. class CcosExpression
  22. {
  23. public:
  24. enum EN_TreeType //二叉树节点类型
  25. {
  26. TT_Null,
  27. TT_Number,
  28. TT_bool,
  29. TT_Max
  30. };
  31. CcosExpression(EN_TreeType treeType = TT_bool);
  32. ~CcosExpression();
  33. //设置优先级
  34. //参数数组中的char代表二元逻辑运算符(必须是单字符的特殊ASCII符号),int代表运算符优先级(取值必须>=0)
  35. //其中,必须有:'#'优先级最小, '('优先级次之, ')'优先级最大,其余逻辑符合理的安排在'('与')'之间即可
  36. void SetPriorityMap(map<char, int>& Map_type_level);
  37. void GetPriorityMap(map<char, int>& Map_type_level);
  38. //构建二叉树,用于解析中缀表达式
  39. bool CreateTree(string strText, list<string>& nodeValueList);
  40. //销毁二叉树
  41. void DestroyTree(TreeNode* root);
  42. //打印二叉树
  43. void Show(vector<vector<string>>& strPrint);
  44. //计算二叉树
  45. template<typename T>
  46. bool CalculateExpression(T& value, SMGetValueCallback fun = nullptr)
  47. {
  48. bool ret{ false };
  49. try {
  50. if (m_pRoot)
  51. {
  52. m_funGetValue = fun;
  53. if (typeid(T) == typeid(bool) && m_eTreeType == TT_bool)
  54. {
  55. value = GetBoolValue(m_pRoot);
  56. ret = true;
  57. }
  58. else if ((typeid(T) == typeid(int) || typeid(T) == typeid(float) || typeid(T) == typeid(double)) && m_eTreeType == TT_Number)
  59. {
  60. value = GetNumberValue(m_pRoot);
  61. ret = true;
  62. }
  63. else
  64. {
  65. FERROR("treeType failed");
  66. ret = false;
  67. }
  68. m_funGetValue = nullptr;
  69. }
  70. }
  71. catch (...)
  72. {
  73. FERROR("CalculateExpression crash");
  74. }
  75. return ret;
  76. }
  77. protected:
  78. void SetRootNode(stack<char>& operatorStacks, vector<string>& dataArray, TreeNode*& curNode);//设计一个单元的二叉树(一个父节点,两个子节点)
  79. void SetSingleNode(vector<string>& dataArray, TreeNode*& curNode);//设计一个单节点的二叉树(一个父节点,无子节点)
  80. void SetRightTree(TreeNode* curNode, char operatorType, string rightData);//在当前节点的右子树最下方重新构建一个单元的二叉树
  81. void SetTopTree(stack<char>& operatorStacks, vector<string>& dataArray, TreeNode*& curNode);//在当前节点上方重新构建一个单元的二叉树
  82. void SetTree(stack<char>& operatorStacks, vector<string>& dataArray, TreeNode*& curNode, bool flag);//根据不同的情况(运算符的优先级)构建不同的二叉树单元
  83. int CheckPriority(char operatorType);//判断优先级
  84. bool IsLeftSymbol(stack<char> stacks);//判断当前栈中是否包含'('操作符
  85. int JudgeKeyValue(const string& data); //截取关键词
  86. double GetNumberValue(TreeNode* node); //计算算数表达式
  87. bool GetBoolValue(TreeNode* node); //计算逻辑表达式
  88. private:
  89. EN_TreeType m_eTreeType{ TT_bool }; //节点类型
  90. map<char, int> glo_PriorityMap; //优先级列表, 逻辑、算数运算符:优先级
  91. TreeNode* m_pRoot{ nullptr }; //根节点
  92. SMGetValueCallback m_funGetValue{ nullptr }; //获取条件当前值
  93. };