1
0
mirror of https://github.com/whvcse/EasyCaptcha.git synced 2024-11-23 01:59:19 +08:00

更新项目配置和代码实现

This commit is contained in:
lilys0810 2024-11-14 23:06:34 +08:00
parent f6cb6062f5
commit 18f00f262a
5 changed files with 45 additions and 87 deletions

View File

@ -10,7 +10,7 @@
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="EasyCaptcha" target="6" />
<module name="EasyCaptcha" target="8" />
</bytecodeTargetLevel>
</component>
</project>

View File

@ -2,5 +2,7 @@
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

View File

@ -1,13 +0,0 @@
<component name="libraryTable">
<library name="Maven: junit:junit:4.7">
<CLASSES>
<root url="jar://$USER_HOME$/.m2/repository/junit/junit/4.7/junit-4.7.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$USER_HOME$/.m2/repository/junit/junit/4.7/junit-4.7-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$USER_HOME$/.m2/repository/junit/junit/4.7/junit-4.7-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -1,16 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.7" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>

View File

@ -1,137 +1,104 @@
package com.wf.captcha.base;
import java.math.BigDecimal;
/**
* 字符串计算器辅助类
* @link https://www.cnblogs.com/woider/p/5331391.html
*/
class ArithHelper {
// 默认除法运算精度
private static final int DEF_DIV_SCALE = 16;
private static final String ONE = "1";
// 这个类不能实例化
private ArithHelper() {
}
// 将double转换为BigDecimal
private static BigDecimal convertToBigDecimal(double value) {
return new BigDecimal(Double.toString(value));
}
// 将String转换为BigDecimal
private static BigDecimal convertToBigDecimal(String value) {
return new BigDecimal(value);
}
/**
* 提供精确的加法运算
*
* @param v1 被加数
* @param v2 加数
* @return 两个参数的和
*/
public static double add(double v1, double v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
return convertToBigDecimal(v1).add(convertToBigDecimal(v2)).doubleValue();
}
public static double add(String v1, String v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
return b1.add(b2).doubleValue();
return convertToBigDecimal(v1).add(convertToBigDecimal(v2)).doubleValue();
}
/**
* 提供精确的减法运算
*
* @param v1 被减数
* @param v2 减数
* @return 两个参数的差
*/
public static double sub(double v1, double v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
return convertToBigDecimal(v1).subtract(convertToBigDecimal(v2)).doubleValue();
}
public static double sub(String v1, String v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
return b1.subtract(b2).doubleValue();
return convertToBigDecimal(v1).subtract(convertToBigDecimal(v2)).doubleValue();
}
/**
* 提供精确的乘法运算
*
* @param v1 被乘数
* @param v2 乘数
* @return 两个参数的积
*/
public static double mul(double v1, double v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
return convertToBigDecimal(v1).multiply(convertToBigDecimal(v2)).doubleValue();
}
public static double mul(String v1, String v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
return b1.multiply(b2).doubleValue();
return convertToBigDecimal(v1).multiply(convertToBigDecimal(v2)).doubleValue();
}
/**
* 提供相对精确的除法运算当发生除不尽的情况时精确到 小数点以后10位以后的数字四舍五入
*
* @param v1 被除数
* @param v2 除数
* @return 两个参数的商
* 提供相对精确的除法运算当发生除不尽的情况时精确到小数点以后10位以后的数字四舍五入
*/
public static double div(double v1, double v2) {
return div(v1, v2, DEF_DIV_SCALE);
}
public static double div(String v1, String v2) {
java.math.BigDecimal b1 = new java.math.BigDecimal(v1);
java.math.BigDecimal b2 = new java.math.BigDecimal(v2);
return b1.divide(b2, DEF_DIV_SCALE, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
return div(v1, v2, DEF_DIV_SCALE);
}
/**
* 提供相对精确的除法运算当发生除不尽的情况时由scale参数指 定精度以后的数字四舍五入
*
* @param v1 被除数
* @param v2 除数
* @param scale 表示表示需要精确到小数点以后几位
* @return 两个参数的商
* 提供相对精确的除法运算由scale参数指定精度
*/
public static double div(double v1, double v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));
java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
validateScale(scale);
return convertToBigDecimal(v1).divide(convertToBigDecimal(v2), scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static double div(String v1, String v2, int scale) {
validateScale(scale);
return convertToBigDecimal(v1).divide(convertToBigDecimal(v2), scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 提供精确的小数位四舍五入处理
*
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
java.math.BigDecimal b = new java.math.BigDecimal(Double.toString(v));
java.math.BigDecimal one = new java.math.BigDecimal("1");
return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
validateScale(scale);
return convertToBigDecimal(v).divide(convertToBigDecimal(ONE), scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static double round(String v, int scale) {
validateScale(scale);
return convertToBigDecimal(v).divide(convertToBigDecimal(ONE), scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
// 校验scale参数
private static void validateScale(int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
java.math.BigDecimal b = new java.math.BigDecimal(v);
java.math.BigDecimal one = new java.math.BigDecimal("1");
return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
}
}