Skip to content

Commit e44264a

Browse files
1.0.3发布
# ![image](https://user-images.githubusercontent.com/113756063/203919312-dcec4a61-2136-4af2-a361-66b2ed4e6a54.png) 数学表达式-cpp 更新日志 ## 更新版本:1.0.2 -> 1.0.3 * 更新版本: 1.0.2 -> 1.0.3 * 更新时间: 2024年01月08日 * 参与更新任务的作者列表 | 名称 | GitHub主页 | 联系方式 | |----------------|-----------------------------------|-------------------| | BeardedManZhao | https://github.com/BeardedManZhao | [email protected] | | ------ | ------ | ------ | ## 注意事项 1.0.2 以及 以后的版本中 针对函数的注册操作不能向后兼容,如果是在1.0.2版本以以后的版本 请使用下面的方式注册函数 ``` // 准备函数 这里的函数的作用是将 3 个参数求和 auto myFun = [](const ME::MEStack<double>& v) { double res = 0; for (int i = 0; i < v.size(); ++i){ res += v.get(i); } return res; }; // 注册函数 将我们的函数注册成为 DoubleValue 的名称 ME::FunctionManager::append("sum", myFun); ``` ## 更新日志 在旧版本中,针对 `2*-1` 这类的计算操作会发生一些异常,因此在这里我们进行了修复,本次修复不会影响任何操作,也不会带来任何不良影响,下面是旧版本中出现错误的代码,本次更新将修复此问题。 ```c++ #include <mathematical_expression.h> #include "FunctionManager.h" #include "Utils.h" int main() { system("chcp 65001"); // 构建一个数学表达式,表达式中使用到了函数 DoubleValue string s = "2 * -1"; // 获取到 数学表达式解析库 mathematical_expression me; // 获取到函数表达式计算组件 auto functionFormulaCalculation = me.getPrefixExpressionOperation(); // 检查数学表达式 functionFormulaCalculation.check(s); // 计算出结果 ME::CalculationNumberResults results = functionFormulaCalculation << s; // 将结果打印出来 cout << "计算层数:" << results.getResultLayers() << "\t计算结果:" << results << "\t计算来源:" << results.getCalculationSourceName() << endl; } ```
1 parent 40f8c5b commit e44264a

File tree

7 files changed

+98
-10
lines changed

7 files changed

+98
-10
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Start testing: Dec 02 20:49 ?D1��������?����??
1+
Start testing: Jan 08 11:13 ?D1��������?����??
22
----------------------------------------------------------
3-
End testing: Dec 02 20:49 ?D1��������?����??
3+
End testing: Jan 08 11:13 ?D1��������?����??

include/Utils.h

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ namespace StrUtils {
2727
*/
2828
bool IsAnOperator(char c);
2929

30+
/**
31+
* 将一个 ascii 数值转换成为字符串
32+
* @param ascii 需要被转换的 ascii 数值
33+
* @return 转换操作成功之后的字符串
34+
*/
35+
std::string asciiToChar(int ascii);
36+
3037
/**
3138
* 将一个字符串转换为浮点数值
3239
*

include/mathematical_expression.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "FunctionFormulaCalculation.h"
1313
#include "FunctionFormulaCalculationTwo.h"
1414

15-
#define VERSION "1.0.2.1-mathematical_expression-C++";
15+
#define VERSION "1.0.3-mathematical_expression-C++";
1616

1717
/**
1818
* 数学表达式解析计算库中的门户类,由该类获取到数学表达式计算库中的相关数据对象。

src/core/calculation/PrefixExpressionOperation.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ namespace ME {
6363
ME::MEStack<char> characterStack;
6464
// 开始格式化,将符号与操作数进行分类
6565
std::string stringBuilder;
66+
// 创建标记点 标记上一个是否是操作符
67+
bool backIsOpt = true;
6668
for (const auto &c: newFormula) {
67-
if (StrUtils::IsAnOperator(c)) {
69+
if (!backIsOpt && StrUtils::IsAnOperator(c)) {
70+
backIsOpt = true;
6871
// 如果是操作符,就先将上一个数值计算出来
6972
double number = StrUtils::stringToDouble(stringBuilder);
7073
if (characterStack.empty()) {
@@ -91,9 +94,10 @@ namespace ME {
9194
}
9295
// 清理所有的字符缓冲
9396
stringBuilder.clear();
94-
} else if (c == DECIMAL_POINT || StrUtils::IsANumber(c)) {
97+
} else if (c == DECIMAL_POINT || c == MINUS_SIGN || StrUtils::IsANumber(c)) {
9598
// 如果是数值的某一位,就将数值存储到变量中
9699
stringBuilder += c;
100+
backIsOpt = false;
97101
}
98102
}
99103
doubleStack.push(StrUtils::stringToDouble(stringBuilder));

src/utils/StrUtils.cpp

+27-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ namespace StrUtils {
2929
c == REMAINDER_SIGN;
3030
}
3131

32+
/**
33+
* 将一个 ascii 数值转换成为字符串
34+
* @param ascii 需要被转换的 ascii 数值
35+
* @return 转换操作成功之后的字符串
36+
*/
37+
std::string asciiToChar(int ascii) {
38+
return {char(ascii)};
39+
}
40+
3241
/**
3342
* 将一个数值字符传换成一个数值
3443
*
@@ -37,11 +46,14 @@ namespace StrUtils {
3746
*/
3847
int charToInteger(char c) {
3948
if (StrUtils::IsANumber(c)) {
40-
return c - 0x30;
49+
return c - 48;
4150
} else {
42-
std::string data = &"您在进行字符与数值之间的转换时,由于字符的不正确导致无法成功转换,错误字符:"[c];
43-
throw ME::MExceptional(data.append(
44-
&"\nWhen you are converting characters to numeric values, the conversion cannot be successful due to incorrect characters. Error characters:"[c]));
51+
std::string temp_ex_data = "您在进行字符与数值之间的转换时,由于字符的不正确导致无法成功转换";
52+
std::string temp_res = asciiToChar(c);
53+
temp_ex_data.append(
54+
"\nWhen you are converting characters to numeric values, the conversion cannot be successful due to incorrect characters.")
55+
.append(temp_res);
56+
throw ME::WrongFormat(temp_ex_data);
4557
}
4658
}
4759

@@ -58,6 +70,16 @@ namespace StrUtils {
5870
int floatSize = 0;
5971
bool isInt = true;
6072
unsigned long long length = s.length();
73+
if (length <= 0) {
74+
throw ME::WrongFormat(
75+
"您在调用 stringToDouble 函数的时候 不能传递空字符串!\nYou cannot pass an empty string when calling the stringToDouble function!");
76+
}
77+
// 判断是否为负数
78+
bool isF = s[0] == MINUS_SIGN;
79+
if (isF) {
80+
s = s.substr(1);
81+
length -= 1;
82+
}
6183
for (int i = 0; i < length; i++) {
6284
char c = s[i];
6385
if (c != DECIMAL_POINT && c != EMPTY) {
@@ -87,7 +109,7 @@ namespace StrUtils {
87109
// 计算出来数值本身
88110
const double res = intRes + floatRes / (double) NumberUtils::PowerOfTen(10, floatSize);
89111
// 判断是否为负数,如果不是负数直接返回值
90-
return s[0] == MINUS_SIGN ? -res : res;
112+
return isF ? -res : res;
91113
}
92114

93115
std::vector<std::string> splitByChar(const std::string &data, char splitChar) {

update/1.0.2_1.0.3.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ![image](https://user-images.githubusercontent.com/113756063/203919312-dcec4a61-2136-4af2-a361-66b2ed4e6a54.png) 数学表达式-cpp 更新日志
2+
3+
## 更新版本:1.0.2 -> 1.0.3
4+
5+
* 更新版本: 1.0.2 -> 1.0.3
6+
* 更新时间: 2024年01月08日
7+
* 参与更新任务的作者列表
8+
9+
| 名称 | GitHub主页 | 联系方式 |
10+
|----------------|-----------------------------------|-------------------|
11+
| BeardedManZhao | https://github.com/BeardedManZhao | [email protected] |
12+
| ------ | ------ | ------ |
13+
14+
## 注意事项
15+
16+
1.0.2 以及 以后的版本中 针对函数的注册操作不能向后兼容,如果是在1.0.2版本以以后的版本 请使用下面的方式注册函数
17+
18+
```
19+
// 准备函数 这里的函数的作用是将 3 个参数求和
20+
auto myFun = [](const ME::MEStack<double>& v) {
21+
double res = 0;
22+
for (int i = 0; i < v.size(); ++i){
23+
res += v.get(i);
24+
}
25+
return res;
26+
};
27+
// 注册函数 将我们的函数注册成为 DoubleValue 的名称
28+
ME::FunctionManager::append("sum", myFun);
29+
```
30+
31+
## 更新日志
32+
33+
在旧版本中,针对 `2*-1` 这类的计算操作会发生一些异常,因此在这里我们进行了修复,本次修复不会影响任何操作,也不会带来任何不良影响,下面是旧版本中出现错误的代码,本次更新将修复此问题。
34+
35+
```c++
36+
#include <mathematical_expression.h>
37+
#include "FunctionManager.h"
38+
#include "Utils.h"
39+
int main() {
40+
system("chcp 65001");
41+
// 构建一个数学表达式,表达式中使用到了函数 DoubleValue
42+
string s = "2 * -1";
43+
// 获取到 数学表达式解析库
44+
mathematical_expression me;
45+
// 获取到函数表达式计算组件
46+
auto functionFormulaCalculation = me.getPrefixExpressionOperation();
47+
// 检查数学表达式
48+
functionFormulaCalculation.check(s);
49+
// 计算出结果
50+
ME::CalculationNumberResults results = functionFormulaCalculation << s;
51+
// 将结果打印出来
52+
cout << "计算层数:" << results.getResultLayers() << "\t计算结果:" << results << "\t计算来源:" << results.getCalculationSourceName() << endl;
53+
}
54+
```
55+

0 commit comments

Comments
 (0)