This commit is contained in:
Looly 2020-09-17 11:26:14 +08:00
parent 6b89c841d3
commit 85601e2446
5 changed files with 32 additions and 29 deletions

View File

@ -1,5 +1,6 @@
package cn.hutool.captcha.generator;
import cn.hutool.core.math.Calculator;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
@ -59,20 +60,8 @@ public class MathGenerator implements CodeGenerator {
return false;
}
final int a = Integer.parseInt(StrUtil.sub(code, 0, this.numberLength).trim());
final char operator = code.charAt(this.numberLength);
final int b = Integer.parseInt(StrUtil.sub(code, this.numberLength + 1, this.numberLength + 1 + this.numberLength).trim());
switch (operator) {
case '+':
return (a + b) == result;
case '-':
return (a - b) == result;
case '*':
return (a * b) == result;
default:
return false;
}
final int calculateResult = (int) Calculator.conversion(code);
return result == calculateResult;
}
/**

View File

@ -0,0 +1,14 @@
package cn.hutool.captcha;
import cn.hutool.captcha.generator.MathGenerator;
import org.junit.Test;
public class GeneratorTest {
@Test
public void mathGeneratorTest(){
final MathGenerator mathGenerator = new MathGenerator();
for (int i = 0; i < 1000; i++) {
mathGenerator.verify(mathGenerator.generate(), "0");
}
}
}

View File

@ -1,6 +1,7 @@
package cn.hutool.core.math;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import java.math.BigDecimal;
import java.util.Collections;
@ -25,28 +26,21 @@ public class Calculator {
* @return 计算结果
*/
public static double conversion(String expression) {
double result;
Calculator cal = new Calculator();
try {
final Calculator cal = new Calculator();
expression = transform(expression);
result = cal.calculate(expression);
} catch (Exception e) {
// e.printStackTrace();
// 运算错误返回NaN
return Double.NaN;
}
// return new String().valueOf(result);
return result;
return cal.calculate(expression);
}
/**
* 将表达式中负数的符号更改
*
* @param expression 例如-2+-1*(-3E-2)-(-1) 被转为 ~2+~1*(~3E~2)-(~1)
* @return 更改后的表达式
* @return 转换后的字符串
*/
private static String transform(String expression) {
char[] arr = expression.toCharArray();
expression = StrUtil.cleanBlank(expression);
expression = StrUtil.removeSuffix(expression, "=");
final char[] arr = expression.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '-') {
if (i == 0) {

View File

@ -10,4 +10,10 @@ public class CalculatorTest {
final double conversion = Calculator.conversion("(0*1--3)-5/-4-(3*(-2.13))");
Assert.assertEquals(10.64, conversion, 2);
}
@Test
public void conversationTest2(){
final double conversion = Calculator.conversion("77 * 12");
Assert.assertEquals(924.0, conversion, 2);
}
}

View File

@ -69,7 +69,7 @@ public class GlobalCookieManager {
Map<String, List<String>> cookieHeader;
try {
cookieHeader = cookieManager.get(getURI(conn), new HashMap<String, List<String>>(0));
cookieHeader = cookieManager.get(getURI(conn), new HashMap<>(0));
} catch (IOException e) {
throw new IORuntimeException(e);
}