Skip to content

Commit 523ac00

Browse files
committedJun 20, 2023
1.0.0 版本上传
1 parent 46dea16 commit 523ac00

20 files changed

+767
-60
lines changed
 

‎CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ add_library(
1313
src/core/manager/CalculationManagement.cpp include/CalculationManagement.h
1414
src/core/calculation/PrefixExpressionOperation.cpp
1515
src/exceptional/MExceptional.cpp include/MExceptional.h
16-
src/utils/NumberUtils.cpp src/utils/StrUtils.cpp src/dataContainer/MEStack.cpp include/MEStack.h include/PrefixExpressionOperation.h src/core/calculation/NumberCalculation.cpp include/NumberCalculation.h src/core/calculation/Calculation.cpp include/Calculation.h include/mathematical_expression.h src/core/calculation/BracketsCalculation.cpp include/BracketsCalculation.h)
16+
src/utils/NumberUtils.cpp src/utils/StrUtils.cpp src/dataContainer/MEStack.cpp include/MEStack.h include/PrefixExpressionOperation.h src/core/calculation/NumberCalculation.cpp include/NumberCalculation.h src/core/calculation/Calculation.cpp include/Calculation.h include/mathematical_expression.h src/core/calculation/BracketsCalculation.cpp include/BracketsCalculation.h src/core/calculation/BracketsCalculationTwo.cpp include/BracketsCalculationTwo.h src/core/mathematical_expression.cpp)
1717

‎README-Chinese.md

+183
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,186 @@
55
## 介绍
66

77
本框架是一种针对数学公式解析的有效工具,能够通过C++的API解析包含嵌套函数,包含函数,数列步长累加等数学公式,返回值是一个数值的结果对象,同时也可以进行比较运算的操作,再进行比较的时候,返回值是一个布尔值结果对象。
8+
如果您是一位 Java 或 python爱好者,可以专门前往 [JavaAPI](https://github.com/BeardedManZhao/mathematical-expression.git)
9+
[PythonAPI](https://github.com/BeardedManZhao/mathematical-expression-Py) 中进行相关资料的查阅。
10+
11+
### 如何使用库?
12+
13+
在项目中有一个 include 文件目录,其中存储的就是库需要的所有头文件,您可以将其中的库文件导入到您的项目中,然后再集成本框架编译好的dll,下面是cmake的配置文件实例。
14+
15+
```cmake
16+
cmake_minimum_required(VERSION 3.23)
17+
project(MyCpp)
18+
19+
set(CMAKE_CXX_STANDARD 14)
20+
21+
# 设置头文件目录(可以自定义)
22+
include_directories(${PROJECT_SOURCE_DIR}/head)
23+
add_executable(MyCpp main.cpp)
24+
# 与项目进行链接(将库链接到编译之后的目标中)
25+
target_link_libraries(${PROJECT_NAME} D:\\liming\\Project\\Clion\\MyCpp\\cmake-build-debug\\mathematical_expression_cpp.dll)
26+
```
27+
28+
集成操作完毕之后,您可以尝试输入以下代码来判断库的功能是否正常,下面是该库的一个测试代码,如果其运行之后的程序main函数返回值为0
29+
代表程序正常退出,意味着库装载完毕。
30+
31+
```c++
32+
#include "mathematical_expression.h"
33+
34+
int main(){
35+
system("chcp 65001");
36+
// 打印 mathematical_expression 的版本信息
37+
cout << mathematical_expression::getVERSION() << endl;
38+
}
39+
```
40+
41+
### 通过 mathematical-expression 库直接获取到计算组件并进行计算
42+
43+
```c++
44+
#include "mathematical_expression.h"
45+
46+
int main(){
47+
system("chcp 65001");
48+
// 构建需要计算的两种表达式
49+
string s1 = "1 + 20 - 2 + 4", s2 = "1 + 20 - (2 + 4)";
50+
// 获取到 me 门户类
51+
mathematical_expression me;
52+
// 获取到 无括号表达式计算组件
53+
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
54+
// 获取到 有括号表达式计算组件
55+
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
56+
// 开始检查表达式
57+
prefixExpressionOperation.check(s1);
58+
bracketsCalculationTwo.check(s2);
59+
// 开始计算两个表达式 可以使用左移运算符将表达式送给计算组件 获取到结果对象
60+
ME::CalculationNumberResults r1 = prefixExpressionOperation << s1;
61+
ME::CalculationNumberResults r2 = bracketsCalculationTwo << s2;
62+
// 开始进行结果查看
63+
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
64+
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r2 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
65+
}
66+
```
67+
68+
- 运行结果
69+
70+
```
71+
Active code page: 65001
72+
计算层数:1 计算结果:23 计算来源:PrefixExpressionOperation
73+
计算层数:1 计算结果:15 计算来源:PrefixExpressionOperation
74+
75+
进程已结束,退出代码0
76+
```
77+
78+
## 框架架构
79+
80+
### 门户类
81+
82+
我们可以通过指定的门户类对象获取到相关的各个计算组件,这里与 JavaAPI 和 PythonAPI 的实现有些不同,这里是使用的get函数获取到指定计算组件对象,而非使用
83+
Hash 缓冲池。
84+
85+
```c++
86+
#include "mathematical_expression.h"
87+
88+
int main(){
89+
system("chcp 65001");
90+
// 获取到 me 门户类
91+
mathematical_expression me;
92+
// 获取到 无括号表达式计算组件
93+
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
94+
// 获取到 有括号表达式计算组件
95+
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
96+
}
97+
```
98+
99+
## 计算组件
100+
101+
### 无括号表达式
102+
103+
- 类组件:core.calculation.number.PrefixExpressionOperation
104+
- 介绍
105+
106+
针对一个没有括号,但是有加减乘除以及取余等运算操作的数学表达式而设计的组件,该组件可以实现带有优先级计算的功能,其中通过前缀表达式解析计算,将操作数与操作符一同存储到栈,在存储的同时配有计算优先级比较,如果当下的优先级较小,就先将上一个操作数与操作符与当前操作数进行运算,形成一个新的数值,然后再入栈。
107+
- API使用示例
108+
109+
该组件支持的运算符有: a+b a-b a*b a/b a%b
110+
111+
```c++
112+
#include "mathematical_expression.h"
113+
114+
int main(){
115+
system("chcp 65001");
116+
// 获取到 me 门户类
117+
mathematical_expression me;
118+
// 获取到 无括号表达式计算组件
119+
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
120+
// 构建一个表达式
121+
string s = "1 + 2 + 4 * 10 - 3";
122+
// 开始检查表达式
123+
prefixExpressionOperation.check(s);
124+
// 开始计算两个表达式 可以使用左移运算符将表达式送给计算组件 获取到结果对象
125+
ME::CalculationNumberResults r1 = prefixExpressionOperation << s;
126+
// 开始进行结果查看
127+
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
128+
}
129+
```
130+
131+
- 运行结果
132+
133+
在API调用中,对函数的运行结果进行了打印,可以看到,组件计算的返回值是一个结果集对象,在该对象中存储的就是很多有关计算结果相关的信息。
134+
135+
```
136+
Active code page: 65001
137+
计算层数:1 计算结果:40 计算来源:PrefixExpressionOperation
138+
139+
进程已结束,退出代码0
140+
```
141+
142+
### 嵌套括号表达式
143+
144+
- 类组件:core.calculation.number.BracketsCalculation2
145+
- 介绍:
146+
147+
嵌套括号表达式解析组件,能够针对带有多个括号的数学表达式进行解析与结果计算,针对嵌套括号进行优先级的解析与计算,该组件依赖于“core.calculation.number.PrefixExpressionOperation”,在该组件中采用递归进行括号的解析,然后将最内层面的表达式提供给“core.calculation.number.PrefixExpressionOperation”进行计算。
148+
- API使用示例
149+
150+
该组件支持的运算符有: a+b a-b a*b a/b a%b ( )
151+
152+
```c++
153+
#include "mathematical_expression.h"
154+
155+
int main(){
156+
system("chcp 65001");
157+
// 获取到 me 门户类
158+
mathematical_expression me;
159+
// 获取到 无括号表达式计算组件
160+
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
161+
// 构建一个表达式
162+
string s = "1 + 2 + 4 * (10 - 3)";
163+
// 开始检查表达式
164+
bracketsCalculationTwo.check(s);
165+
// 开始计算两个表达式 可以使用左移运算符将表达式送给计算组件 获取到结果对象
166+
ME::CalculationNumberResults r1 = bracketsCalculationTwo << s;
167+
// 开始进行结果查看
168+
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
169+
}
170+
```
171+
172+
- 运行结果
173+
174+
在API调用中,对表达式的计算结果进行了打印,可以看到,组件计算的返回值是一个数值结果对象,在该对象中存储的就是很多有关计算结果相关的信息。
175+
176+
```
177+
Active code page: 65001
178+
计算层数:2 计算结果:31 计算来源:BracketsCalculation
179+
180+
进程已结束,退出代码0
181+
```
182+
183+
<hr>
184+
185+
更多信息
186+
187+
- date: 2023-06-20
188+
- Switch to [English Document](https://github.com/BeardedManZhao/mathematical-expression/blob/main/README.md)
189+
- [mathematical-expression-Java](https://github.com/BeardedManZhao/mathematical-expression.git)
190+
- [mathematical-expression-py](https://github.com/BeardedManZhao/mathematical-expression-Py)

‎README.md

+199
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,202 @@
44

55
## introduce
66

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

Comments
 (0)
Please sign in to comment.