Skip to content

Commit

Permalink
1.0.0 版本上传
Browse files Browse the repository at this point in the history
  • Loading branch information
BeardedManZhao committed Jun 20, 2023
1 parent 46dea16 commit 523ac00
Show file tree
Hide file tree
Showing 20 changed files with 767 additions and 60 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ add_library(
src/core/manager/CalculationManagement.cpp include/CalculationManagement.h
src/core/calculation/PrefixExpressionOperation.cpp
src/exceptional/MExceptional.cpp include/MExceptional.h
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/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)

183 changes: 183 additions & 0 deletions README-Chinese.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,186 @@
## 介绍

本框架是一种针对数学公式解析的有效工具,能够通过C++的API解析包含嵌套函数,包含函数,数列步长累加等数学公式,返回值是一个数值的结果对象,同时也可以进行比较运算的操作,再进行比较的时候,返回值是一个布尔值结果对象。
如果您是一位 Java 或 python爱好者,可以专门前往 [JavaAPI](https://github.com/BeardedManZhao/mathematical-expression.git)
[PythonAPI](https://github.com/BeardedManZhao/mathematical-expression-Py) 中进行相关资料的查阅。

### 如何使用库?

在项目中有一个 include 文件目录,其中存储的就是库需要的所有头文件,您可以将其中的库文件导入到您的项目中,然后再集成本框架编译好的dll,下面是cmake的配置文件实例。

```cmake
cmake_minimum_required(VERSION 3.23)
project(MyCpp)
set(CMAKE_CXX_STANDARD 14)
# 设置头文件目录(可以自定义)
include_directories(${PROJECT_SOURCE_DIR}/head)
add_executable(MyCpp main.cpp)
# 与项目进行链接(将库链接到编译之后的目标中)
target_link_libraries(${PROJECT_NAME} D:\\liming\\Project\\Clion\\MyCpp\\cmake-build-debug\\mathematical_expression_cpp.dll)
```

集成操作完毕之后,您可以尝试输入以下代码来判断库的功能是否正常,下面是该库的一个测试代码,如果其运行之后的程序main函数返回值为0
代表程序正常退出,意味着库装载完毕。

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// 打印 mathematical_expression 的版本信息
cout << mathematical_expression::getVERSION() << endl;
}
```

### 通过 mathematical-expression 库直接获取到计算组件并进行计算

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// 构建需要计算的两种表达式
string s1 = "1 + 20 - 2 + 4", s2 = "1 + 20 - (2 + 4)";
// 获取到 me 门户类
mathematical_expression me;
// 获取到 无括号表达式计算组件
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
// 获取到 有括号表达式计算组件
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
// 开始检查表达式
prefixExpressionOperation.check(s1);
bracketsCalculationTwo.check(s2);
// 开始计算两个表达式 可以使用左移运算符将表达式送给计算组件 获取到结果对象
ME::CalculationNumberResults r1 = prefixExpressionOperation << s1;
ME::CalculationNumberResults r2 = bracketsCalculationTwo << s2;
// 开始进行结果查看
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r2 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
}
```

- 运行结果

```
Active code page: 65001
计算层数:1 计算结果:23 计算来源:PrefixExpressionOperation
计算层数:1 计算结果:15 计算来源:PrefixExpressionOperation
进程已结束,退出代码0
```

## 框架架构

### 门户类

我们可以通过指定的门户类对象获取到相关的各个计算组件,这里与 JavaAPI 和 PythonAPI 的实现有些不同,这里是使用的get函数获取到指定计算组件对象,而非使用
Hash 缓冲池。

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// 获取到 me 门户类
mathematical_expression me;
// 获取到 无括号表达式计算组件
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
// 获取到 有括号表达式计算组件
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
}
```

## 计算组件

### 无括号表达式

- 类组件:core.calculation.number.PrefixExpressionOperation
- 介绍

针对一个没有括号,但是有加减乘除以及取余等运算操作的数学表达式而设计的组件,该组件可以实现带有优先级计算的功能,其中通过前缀表达式解析计算,将操作数与操作符一同存储到栈,在存储的同时配有计算优先级比较,如果当下的优先级较小,就先将上一个操作数与操作符与当前操作数进行运算,形成一个新的数值,然后再入栈。
- API使用示例

该组件支持的运算符有: a+b a-b a*b a/b a%b

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// 获取到 me 门户类
mathematical_expression me;
// 获取到 无括号表达式计算组件
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
// 构建一个表达式
string s = "1 + 2 + 4 * 10 - 3";
// 开始检查表达式
prefixExpressionOperation.check(s);
// 开始计算两个表达式 可以使用左移运算符将表达式送给计算组件 获取到结果对象
ME::CalculationNumberResults r1 = prefixExpressionOperation << s;
// 开始进行结果查看
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
}
```

- 运行结果

在API调用中,对函数的运行结果进行了打印,可以看到,组件计算的返回值是一个结果集对象,在该对象中存储的就是很多有关计算结果相关的信息。

```
Active code page: 65001
计算层数:1 计算结果:40 计算来源:PrefixExpressionOperation
进程已结束,退出代码0
```

### 嵌套括号表达式

- 类组件:core.calculation.number.BracketsCalculation2
- 介绍:

嵌套括号表达式解析组件,能够针对带有多个括号的数学表达式进行解析与结果计算,针对嵌套括号进行优先级的解析与计算,该组件依赖于“core.calculation.number.PrefixExpressionOperation”,在该组件中采用递归进行括号的解析,然后将最内层面的表达式提供给“core.calculation.number.PrefixExpressionOperation”进行计算。
- API使用示例

该组件支持的运算符有: a+b a-b a*b a/b a%b ( )

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// 获取到 me 门户类
mathematical_expression me;
// 获取到 无括号表达式计算组件
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
// 构建一个表达式
string s = "1 + 2 + 4 * (10 - 3)";
// 开始检查表达式
bracketsCalculationTwo.check(s);
// 开始计算两个表达式 可以使用左移运算符将表达式送给计算组件 获取到结果对象
ME::CalculationNumberResults r1 = bracketsCalculationTwo << s;
// 开始进行结果查看
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
}
```

- 运行结果

在API调用中,对表达式的计算结果进行了打印,可以看到,组件计算的返回值是一个数值结果对象,在该对象中存储的就是很多有关计算结果相关的信息。

```
Active code page: 65001
计算层数:2 计算结果:31 计算来源:BracketsCalculation
进程已结束,退出代码0
```

<hr>

更多信息

- date: 2023-06-20
- Switch to [English Document](https://github.com/BeardedManZhao/mathematical-expression/blob/main/README.md)
- [mathematical-expression-Java](https://github.com/BeardedManZhao/mathematical-expression.git)
- [mathematical-expression-py](https://github.com/BeardedManZhao/mathematical-expression-Py)
199 changes: 199 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,202 @@

## introduce

This framework is an effective tool for Formula parsing. It can parse Formula including nested functions, including
functions, and sequence step accumulation through the C++API. The return value is a numerical result object. At the same
time, you can also perform comparison operations. When you compare again, the return value is a Boolean result object.
If you are a Java or Python enthusiast, you can specifically
visit [JavaAPI](https://github.com/BeardedManZhao/mathematical-expression.git)
Or [Python API](https://github.com/BeardedManZhao/mathematical-expression-Py) Search for relevant information in.

### How to use the library?

There is an include file directory in the project, where all the Header file required by the library are stored. You can
import the library files into your project, and then assemble the dll compiled by this framework. The following is an
example of the configuration file for cmake.

```cmake
cmake_minimum_required(VERSION 3.23)
project(MyCpp)
set(CMAKE_CXX_STANDARD 14)
# Set Header file directory (can be customized)
include_directories(${PROJECT_SOURCE_DIR}/include)
add_executable(MyCpp main.cpp)
# Link to the project (link the library to the compiled target)
target_link_libraries(${PROJECT_NAME} D:\\liming\\Project\\Clion\\MyCpp\\cmake-build-debug\\mathematical_expression_cpp.dll)
```

After the integration operation is completed, you can try to enter the following code to determine whether the function
of the library is normal. The following is a test code of the library. If the return value of the main function of the
program after it runs is 0, it means the program exits normally, which means the library is loaded.

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// 打印 mathematical_expression 的版本信息
cout << mathematical_expression::getVERSION() << endl;
}
```

### Obtain calculation components directly through the mathematical expression library and perform calculations

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// Build two expressions that need to be evaluated
string s1 = "1 + 20 - 2 + 4", s2 = "1 + 20 - (2 + 4)";
// Obtain me portal class
mathematical_expression me;
// Obtaining an expression evaluation component without parentheses
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
// Obtaining parenthesized expression evaluation component
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
//
prefixExpressionOperation.check(s1);
bracketsCalculationTwo.check(s2);
// 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
ME::CalculationNumberResults r1 = prefixExpressionOperation << s1;
ME::CalculationNumberResults r2 = bracketsCalculationTwo << s2;
// Start viewing results
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r2 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
}
```

- Running results

```
Active code page: 65001
计算层数:1 计算结果:23 计算来源:PrefixExpressionOperation
计算层数:1 计算结果:15 计算来源:PrefixExpressionOperation
进程已结束,退出代码0
```

## Framework architecture

### Portal class

We can obtain the relevant computing components through the specified portal class object, which is slightly different
from the implementation of Java API and Python API. Here, we use the get function to obtain the specified computing
component object, rather than using a hash buffer pool.

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// Obtain me portal class
mathematical_expression me;
// Obtaining an expression evaluation component without parentheses
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
// Obtaining parenthesized expression evaluation component
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
}
```

## Computing Components

### Parenthesis free expression

- Class component:ME::PrefixExpressionOperation

- Introduction
A component designed for a mathematical expression without parentheses, but with operations such as addition,
subtraction, multiplication, division, and remainder. This component can implement a function with priority
calculation. Through prefix expression parsing and calculation, the operand and operator are stored on the stack, and
at the same time, there is a calculation priority comparison. If the current priority is smaller, Just perform an
operation on the previous operand and operator with the current operand to form a new value, and then push it onto the
stack.

- API usage examples
The operators supported by this component are: a+b a-b a * b a/b a% b

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// Obtain me portal class
mathematical_expression me;
// Obtaining an expression evaluation component without parentheses
ME::PrefixExpressionOperation prefixExpressionOperation = me.getPrefixExpressionOperation();
// 构建一个表达式
string s = "1 + 2 + 4 * 10 - 3";
// Start checking expression
prefixExpressionOperation.check(s);
// 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
ME::CalculationNumberResults r1 = prefixExpressionOperation << s;
// Start viewing results
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
}
```

- Running results

In the API call, the Running results of the function were printed, and it can be seen that the return value calculated
by the component is a result set object, which stores a lot of information related to the calculation results.

```
Active code page: 65001
计算层数:1 计算结果:40 计算来源:PrefixExpressionOperation
进程已结束,退出代码0
```

### 嵌套括号表达式

- Class component:ME::BracketsCalculationTwo
- 介绍:

嵌套括号表达式解析组件,能够针对带有多个括号的数学表达式进行解析与结果计算,针对嵌套括号进行优先级的解析与计算,该组件依赖于“core.calculation.number.PrefixExpressionOperation”,在该组件中采用递归进行括号的解析,然后将最内层面的表达式提供给“core.calculation.number.PrefixExpressionOperation”进行计算。
- API使用示例

该组件支持的运算符有: a+b a-b a*b a/b a%b ( )

```c++
#include "mathematical_expression.h"

int main(){
system("chcp 65001");
// Obtain me portal class
mathematical_expression me;
// Obtaining an expression evaluation component without parentheses
ME::BracketsCalculationTwo bracketsCalculationTwo = me.getBracketsCalculation2();
// Build an expression
string s = "1 + 2 + 4 * (10 - 3)";
// Start checking expression
bracketsCalculationTwo.check(s);
// 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
ME::CalculationNumberResults r1 = bracketsCalculationTwo << s;
// Start viewing results
cout << "计算层数:" << r1.getResultLayers() << "\t计算结果:" << r1 << "\t计算来源:" << r1.getCalculationSourceName() << endl;
}
```

- Running results

In the API call, the Running results of the function were printed, and it can be seen that the return value calculated
by the component is a result set object, which stores a lot of information related to the calculation results.

```
Active code page: 65001
计算层数:2 计算结果:31 计算来源:BracketsCalculation
进程已结束,退出代码0
```

<hr>

更多信息

- date: 2023-06-20
- 切换至 [中文文档](https://github.com/BeardedManZhao/mathematical-expression/blob/main/README-Chinese.md)
- [mathematical-expression-Java](https://github.com/BeardedManZhao/mathematical-expression.git)
- [mathematical-expression-py](https://github.com/BeardedManZhao/mathematical-expression-Py)

Loading

0 comments on commit 523ac00

Please sign in to comment.