|
4 | 4 |
|
5 | 5 | ## introduce
|
6 | 6 |
|
| 7 | +This framework is an effective tool for Formula parsing. It can parse Formula including nested functions, including |
| 8 | +functions, and sequence step accumulation through the C++API. The return value is a numerical result object. At the same |
| 9 | +time, you can also perform comparison operations. When you compare again, the return value is a Boolean result object. |
| 10 | +If you are a Java or Python enthusiast, you can specifically |
| 11 | +visit [JavaAPI](https://github.com/BeardedManZhao/mathematical-expression.git) |
| 12 | +Or [Python API](https://github.com/BeardedManZhao/mathematical-expression-Py) Search for relevant information in. |
| 13 | + |
| 14 | +### How to use the library? |
| 15 | + |
| 16 | +There is an include file directory in the project, where all the Header file required by the library are stored. You can |
| 17 | +import the library files into your project, and then assemble the dll compiled by this framework. The following is an |
| 18 | +example of the configuration file for cmake. |
| 19 | + |
| 20 | +```cmake |
| 21 | +cmake_minimum_required(VERSION 3.23) |
| 22 | +project(MyCpp) |
| 23 | +
|
| 24 | +set(CMAKE_CXX_STANDARD 14) |
| 25 | +
|
| 26 | +# Set Header file directory (can be customized) |
| 27 | +include_directories(${PROJECT_SOURCE_DIR}/include) |
| 28 | +add_executable(MyCpp main.cpp) |
| 29 | +# Link to the project (link the library to the compiled target) |
| 30 | +target_link_libraries(${PROJECT_NAME} D:\\liming\\Project\\Clion\\MyCpp\\cmake-build-debug\\mathematical_expression_cpp.dll) |
| 31 | +``` |
| 32 | + |
| 33 | +After the integration operation is completed, you can try to enter the following code to determine whether the function |
| 34 | +of the library is normal. The following is a test code of the library. If the return value of the main function of the |
| 35 | +program after it runs is 0, it means the program exits normally, which means the library is loaded. |
| 36 | + |
| 37 | +```c++ |
| 38 | +#include "mathematical_expression.h" |
| 39 | + |
| 40 | +int main(){ |
| 41 | + system("chcp 65001"); |
| 42 | + // 打印 mathematical_expression 的版本信息 |
| 43 | + cout << mathematical_expression::getVERSION() << endl; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### Obtain calculation components directly through the mathematical expression library and perform calculations |
| 48 | + |
| 49 | +```c++ |
| 50 | +#include "mathematical_expression.h" |
| 51 | + |
| 52 | +int main(){ |
| 53 | + system("chcp 65001"); |
| 54 | + // Build two expressions that need to be evaluated |
| 55 | + string s1 = "1 + 20 - 2 + 4", s2 = "1 + 20 - (2 + 4)"; |
| 56 | + // Obtain me portal class |
| 57 | + mathematical_expression me; |
| 58 | + // Obtaining an expression evaluation component without parentheses |
| 59 | + ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation(); |
| 60 | + // Obtaining parenthesized expression evaluation component |
| 61 | + ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2(); |
| 62 | + // |
| 63 | + prefixExpressionOperation.check(s1); |
| 64 | + bracketsCalculationTwo.check(s2); |
| 65 | + // To start calculating two expressions, you can use the left shift operator to send the expression to the calculation component and obtain the result object |
| 66 | + ME::CalculationNumberResults r1 = prefixExpressionOperation << s1; |
| 67 | + ME::CalculationNumberResults r2 = bracketsCalculationTwo << s2; |
| 68 | + // Start viewing results |
| 69 | + cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl; |
| 70 | + cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r2 << "\t计算来源:" << r1.getCalculationSourceName() << endl; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +- Running results |
| 75 | + |
| 76 | +``` |
| 77 | +Active code page: 65001 |
| 78 | +计算层数:1 计算结果:23 计算来源:PrefixExpressionOperation |
| 79 | +计算层数:1 计算结果:15 计算来源:PrefixExpressionOperation |
| 80 | +
|
| 81 | +进程已结束,退出代码0 |
| 82 | +``` |
| 83 | + |
| 84 | +## Framework architecture |
| 85 | + |
| 86 | +### Portal class |
| 87 | + |
| 88 | +We can obtain the relevant computing components through the specified portal class object, which is slightly different |
| 89 | +from the implementation of Java API and Python API. Here, we use the get function to obtain the specified computing |
| 90 | +component object, rather than using a hash buffer pool. |
| 91 | + |
| 92 | +```c++ |
| 93 | +#include "mathematical_expression.h" |
| 94 | + |
| 95 | +int main(){ |
| 96 | + system("chcp 65001"); |
| 97 | + // Obtain me portal class |
| 98 | + mathematical_expression me; |
| 99 | + // Obtaining an expression evaluation component without parentheses |
| 100 | + ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation(); |
| 101 | + // Obtaining parenthesized expression evaluation component |
| 102 | + ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2(); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Computing Components |
| 107 | + |
| 108 | +### Parenthesis free expression |
| 109 | + |
| 110 | +- Class component:ME::PrefixExpressionOperation |
| 111 | + |
| 112 | +- Introduction |
| 113 | + A component designed for a mathematical expression without parentheses, but with operations such as addition, |
| 114 | + subtraction, multiplication, division, and remainder. This component can implement a function with priority |
| 115 | + calculation. Through prefix expression parsing and calculation, the operand and operator are stored on the stack, and |
| 116 | + at the same time, there is a calculation priority comparison. If the current priority is smaller, Just perform an |
| 117 | + operation on the previous operand and operator with the current operand to form a new value, and then push it onto the |
| 118 | + stack. |
| 119 | + |
| 120 | +- API usage examples |
| 121 | + The operators supported by this component are: a+b a-b a * b a/b a% b |
| 122 | + |
| 123 | +```c++ |
| 124 | +#include "mathematical_expression.h" |
| 125 | + |
| 126 | +int main(){ |
| 127 | + system("chcp 65001"); |
| 128 | + // Obtain me portal class |
| 129 | + mathematical_expression me; |
| 130 | + // Obtaining an expression evaluation component without parentheses |
| 131 | + ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation(); |
| 132 | + // 构建一个表达式 |
| 133 | + string s = "1 + 2 + 4 * 10 - 3"; |
| 134 | + // Start checking expression |
| 135 | + prefixExpressionOperation.check(s); |
| 136 | + // To start calculating two expressions, you can use the left shift operator to send the expression to the calculation component and obtain the result object |
| 137 | + ME::CalculationNumberResults r1 = prefixExpressionOperation << s; |
| 138 | + // Start viewing results |
| 139 | + cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl; |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +- Running results |
| 144 | + |
| 145 | + In the API call, the Running results of the function were printed, and it can be seen that the return value calculated |
| 146 | + by the component is a result set object, which stores a lot of information related to the calculation results. |
| 147 | + |
| 148 | +``` |
| 149 | +Active code page: 65001 |
| 150 | +计算层数:1 计算结果:40 计算来源:PrefixExpressionOperation |
| 151 | +
|
| 152 | +进程已结束,退出代码0 |
| 153 | +``` |
| 154 | + |
| 155 | +### 嵌套括号表达式 |
| 156 | + |
| 157 | +- Class component:ME::BracketsCalculationTwo |
| 158 | +- 介绍: |
| 159 | + |
| 160 | + 嵌套括号表达式解析组件,能够针对带有多个括号的数学表达式进行解析与结果计算,针对嵌套括号进行优先级的解析与计算,该组件依赖于“core.calculation.number.PrefixExpressionOperation”,在该组件中采用递归进行括号的解析,然后将最内层面的表达式提供给“core.calculation.number.PrefixExpressionOperation”进行计算。 |
| 161 | +- API使用示例 |
| 162 | + |
| 163 | + 该组件支持的运算符有: a+b a-b a*b a/b a%b ( ) |
| 164 | + |
| 165 | +```c++ |
| 166 | +#include "mathematical_expression.h" |
| 167 | + |
| 168 | +int main(){ |
| 169 | + system("chcp 65001"); |
| 170 | + // Obtain me portal class |
| 171 | + mathematical_expression me; |
| 172 | + // Obtaining an expression evaluation component without parentheses |
| 173 | + ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2(); |
| 174 | + // Build an expression |
| 175 | + string s = "1 + 2 + 4 * (10 - 3)"; |
| 176 | + // Start checking expression |
| 177 | + bracketsCalculationTwo.check(s); |
| 178 | + // To start calculating two expressions, you can use the left shift operator to send the expression to the calculation component and obtain the result object |
| 179 | + ME::CalculationNumberResults r1 = bracketsCalculationTwo << s; |
| 180 | + // Start viewing results |
| 181 | + cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl; |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +- Running results |
| 186 | + |
| 187 | + In the API call, the Running results of the function were printed, and it can be seen that the return value calculated |
| 188 | + by the component is a result set object, which stores a lot of information related to the calculation results. |
| 189 | + |
| 190 | +``` |
| 191 | +Active code page: 65001 |
| 192 | +计算层数:2 计算结果:31 计算来源:BracketsCalculation |
| 193 | +
|
| 194 | +进程已结束,退出代码0 |
| 195 | +``` |
| 196 | + |
| 197 | +<hr> |
| 198 | + |
| 199 | +更多信息 |
| 200 | + |
| 201 | +- date: 2023-06-20 |
| 202 | +- 切换至 [中文文档](https://github.com/BeardedManZhao/mathematical-expression/blob/main/README-Chinese.md) |
| 203 | +- [mathematical-expression-Java](https://github.com/BeardedManZhao/mathematical-expression.git) |
| 204 | +- [mathematical-expression-py](https://github.com/BeardedManZhao/mathematical-expression-Py) |
| 205 | + |
0 commit comments