Compare commits

...

2 Commits

@ -1,367 +1,383 @@
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" <modelVersion>4.0.0</modelVersion>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <groupId>com.coproject</groupId>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 <artifactId>math-learning</artifactId>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> <version>0.1.4</version>
<modelVersion>4.0.0</modelVersion> <properties>
<groupId>com.coproject</groupId> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<artifactId>math-learning</artifactId> <maven.compiler.source>17</maven.compiler.source>
<version>0.1.2</version> <maven.compiler.target>17</maven.compiler.target>
<javafx.version>21.0.3</javafx.version>
<properties> <!-- 允许通过 -DskipJlink=true 跳过 jlink避免 javafx:run 时误触发 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <skipJlink>false</skipJlink>
<maven.compiler.source>17</maven.compiler.source> </properties>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release> <dependencies>
<javafx.version>21.0.3</javafx.version> <!-- JavaFX UI -->
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format> <dependency>
</properties> <groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<dependencies> <version>${javafx.version}</version>
<!-- JavaFX UI --> <classifier>win</classifier>
<dependency> </dependency>
<groupId>org.openjfx</groupId> <dependency>
<artifactId>javafx-controls</artifactId> <groupId>org.openjfx</groupId>
<version>${javafx.version}</version> <artifactId>javafx-fxml</artifactId>
<classifier>win</classifier> <version>${javafx.version}</version>
</dependency> <classifier>win</classifier>
<dependency> </dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId> <!-- JSON storage -->
<version>${javafx.version}</version> <dependency>
<classifier>win</classifier> <groupId>com.fasterxml.jackson.core</groupId>
</dependency> <artifactId>jackson-databind</artifactId>
<version>2.17.1</version>
<!-- JSON storage --> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId> <!-- Mail sending (Jakarta Mail implementation) -->
<artifactId>jackson-databind</artifactId> <dependency>
<version>2.17.1</version> <groupId>com.sun.mail</groupId>
</dependency> <artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
<!-- Mail sending (Jakarta Mail implementation) --> </dependency>
<dependency>
<groupId>com.sun.mail</groupId> <!-- Commons Lang for utilities -->
<artifactId>jakarta.mail</artifactId> <dependency>
<version>2.0.1</version> <groupId>org.apache.commons</groupId>
</dependency> <artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<!-- Commons Lang for utilities --> </dependency>
<dependency> </dependencies>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <build>
<version>3.14.0</version> <finalName>${project.artifactId}</finalName>
</dependency> <plugins>
</dependencies> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<build> <artifactId>maven-compiler-plugin</artifactId>
<finalName>${artifactId}-${version}</finalName> <version>3.11.0</version>
<plugins> <configuration>
<!-- 编译器插件 --> <source>${maven.compiler.source}</source>
<plugin> <target>${maven.compiler.target}</target>
<groupId>org.apache.maven.plugins</groupId> </configuration>
<artifactId>maven-compiler-plugin</artifactId> </plugin>
<version>3.11.0</version> <!-- Enable `mvn javafx:run` for local development -->
<configuration> <plugin>
<source>${maven.compiler.source}</source> <groupId>org.openjfx</groupId>
<target>${maven.compiler.target}</target> <artifactId>javafx-maven-plugin</artifactId>
</configuration> <version>0.0.8</version>
</plugin> <configuration>
<mainClass>com.coproject.MainApp</mainClass>
<!-- Enable `mvn javafx:run` for local development --> </configuration>
<plugin> </plugin>
<groupId>org.openjfx</groupId> <plugin>
<artifactId>javafx-maven-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId>
<version>0.0.8</version> <artifactId>maven-jlink-plugin</artifactId>
<configuration> <version>3.2.0</version>
<mainClass>com.coproject.MainApp</mainClass> <configuration>
</configuration> <image>${project.build.directory}/jlink-image</image>
</plugin> <jlinkExecutable>${env.JAVA_HOME}/bin/jlink</jlinkExecutable>
<modulePaths>
<!-- 生成主 JAR --> <modulePath>${env.JAVA_HOME}/jmods</modulePath>
<plugin> <modulePath>${project.basedir}/lib/javafx-jmods-21.0.8/javafx-jmods-21.0.8</modulePath>
<groupId>org.apache.maven.plugins</groupId> </modulePaths>
<artifactId>maven-jar-plugin</artifactId> <!-- 仅包含 JDK 与 JavaFX 模块,第三方库不参与 jlink -->
<version>3.3.0</version> <addModules>javafx.base,javafx.graphics,javafx.controls,javafx.fxml</addModules>
<configuration> <noHeaderFiles>true</noHeaderFiles>
<outputDirectory>${project.build.directory}/dist</outputDirectory> <noManPages>true</noManPages>
<archive> <stripDebug>true</stripDebug>
<manifest> <skip>${skipJlink}</skip>
<addClasspath>true</addClasspath> </configuration>
<classpathPrefix>lib/</classpathPrefix> <executions>
<mainClass>com.coproject.MainApp</mainClass> <execution>
</manifest> <id>make-jlink-image</id>
</archive> <phase>package</phase>
</configuration> <goals>
</plugin> <goal>jlink</goal>
</goals>
<!-- 复制依赖到 lib 目录 --> <configuration>
<plugin> <!-- 在 execution 级别也应用 skip确保 -DskipJlink=true 生效 -->
<groupId>org.apache.maven.plugins</groupId> <skip>${skipJlink}</skip>
<artifactId>maven-dependency-plugin</artifactId> </configuration>
<version>3.6.1</version> </execution>
<executions> </executions>
<execution> </plugin>
<id>copy-dependencies</id>
<phase>package</phase> <plugin>
<goals> <groupId>org.apache.maven.plugins</groupId>
<goal>copy-dependencies</goal> <artifactId>maven-jar-plugin</artifactId>
</goals> <version>3.3.0</version>
<configuration> <configuration>
<outputDirectory>${project.build.directory}/dist/lib</outputDirectory> <outputDirectory>${project.build.directory}/dist</outputDirectory>
<includeScope>runtime</includeScope> <archive>
</configuration> <!-- 使用我们在 antrun 中生成的 manifest 文件,显式包含 lib 下所有依赖 -->
</execution> <manifestFile>${project.build.directory}/dist/manifest.mf</manifestFile>
</executions> </archive>
</plugin> </configuration>
<executions>
<!-- 复制 JavaFX Windows JAR 和配置文件 --> <execution>
<plugin> <id>force-jar</id>
<groupId>org.apache.maven.plugins</groupId> <phase>package</phase>
<artifactId>maven-resources-plugin</artifactId> <goals>
<version>3.3.1</version> <goal>jar</goal>
<executions> </goals>
<execution> </execution>
<id>copy-javafx-win</id> </executions>
<phase>package</phase> </plugin>
<goals> <!-- 备用方案:生成 jar-with-dependencies胖包 -->
<goal>copy-resources</goal> <plugin>
</goals> <groupId>org.apache.maven.plugins</groupId>
<configuration> <artifactId>maven-assembly-plugin</artifactId>
<outputDirectory>${project.build.directory}/dist/lib</outputDirectory> <version>3.6.0</version>
<resources> <configuration>
<resource> <archive>
<directory>${project.basedir}/lib/javafx-win</directory> <manifest>
<includes> <mainClass>com.coproject.MainApp</mainClass>
<include>*.jar</include> </manifest>
</includes> </archive>
</resource> <descriptorRefs>
</resources> <descriptorRef>jar-with-dependencies</descriptorRef>
</configuration> </descriptorRefs>
</execution> <finalName>${project.artifactId}-${project.version}</finalName>
<execution> <appendAssemblyId>true</appendAssemblyId>
<id>copy-config-files</id> <outputDirectory>${project.build.directory}/dist</outputDirectory>
<phase>package</phase> </configuration>
<goals> <executions>
<goal>copy-resources</goal> <execution>
</goals> <id>make-assembly</id>
<configuration> <phase>package</phase>
<outputDirectory>${project.build.directory}/dist</outputDirectory> <goals>
<resources> <goal>single</goal>
<resource> </goals>
<directory>${project.basedir}</directory> </execution>
<includes> </executions>
<include>smtp.env.example</include> </plugin>
<include>README.md</include> <!-- 生成包含所有依赖的可运行 JAR避免安装版缺失类路径导致启动失败 -->
<include>configure_smtp_qq.ps1</include> <plugin>
</includes> <groupId>org.apache.maven.plugins</groupId>
</resource> <artifactId>maven-shade-plugin</artifactId>
</resources> <version>3.5.0</version>
</configuration> <executions>
</execution> <execution>
</executions> <phase>package</phase>
</plugin> <goals>
<goal>shade</goal>
<!-- 创建数据目录 --> </goals>
<plugin> <configuration>
<groupId>org.apache.maven.plugins</groupId> <skip>true</skip>
<artifactId>maven-antrun-plugin</artifactId> <!-- 将产物写入 dist 并与 finalName 保持一致 -->
<version>3.1.0</version> <outputFile>${project.build.directory}/dist/${project.build.finalName}.jar</outputFile>
<executions> <shadedArtifactAttached>false</shadedArtifactAttached>
<execution> <createDependencyReducedPom>false</createDependencyReducedPom>
<id>create-data-directories</id> <transformers>
<phase>package</phase> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<configuration> <mainClass>com.coproject.MainApp</mainClass>
<target> </transformer>
<mkdir dir="${project.build.directory}/dist/data"/> </transformers>
<mkdir dir="${project.build.directory}/dist/data/papers"/> </configuration>
<copy file="${project.basedir}/smtp.env.example" </execution>
tofile="${project.build.directory}/dist/data/smtp.env" </executions>
overwrite="true"/> </plugin>
<echo file="${project.build.directory}/dist/data/papers/README.txt">试卷存储目录 <plugin>
将生成的试卷PDF文件保存在此目录中。 <groupId>org.apache.maven.plugins</groupId>
</echo> <artifactId>maven-dependency-plugin</artifactId>
</target> <version>3.6.1</version>
</configuration> <executions>
<goals> <execution>
<goal>run</goal> <id>copy-runtime-deps</id>
</goals> <phase>prepare-package</phase>
</execution> <goals>
<goal>copy-dependencies</goal>
<!-- 自动生成分发包 --> </goals>
<execution> <configuration>
<id>create-distribution</id> <outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
<phase>package</phase> <includeScope>runtime</includeScope>
<configuration> <!-- 复制全部运行时依赖,包含 JavaFX win JAR 到 dist/lib -->
<target> </configuration>
<echo>=== 开始生成分发包 ===</echo> </execution>
</executions>
<!-- 创建分发目录 --> </plugin>
<mkdir dir="${project.build.directory}/数学学习软件-${project.version}"/> <!-- Prepare external config file: copy and rename smtp.env.example -> data/smtp.env into dist -->
<plugin>
<!-- 生成正确的启动脚本 --> <groupId>org.apache.maven.plugins</groupId>
<echo file="${project.build.directory}/数学学习软件-${project.version}/run.bat">@echo off <artifactId>maven-antrun-plugin</artifactId>
chcp 65001 >nul <version>3.1.0</version>
title 数学学习软件 <executions>
echo 启动数学学习软件... <execution>
<id>generate-manifest-with-classpath</id>
REM 检查JAR文件是否存在 <phase>prepare-package</phase>
if not exist "math-learning-0.1.2.jar" ( <configuration>
echo 错误: 找不到主程序文件 math-learning-0.1.2.jar <target>
echo 请确保所有文件在同一个目录下 <!-- 确保 dist 与 dist/lib 目录存在 -->
pause <mkdir dir="${project.build.directory}/dist"/>
exit /b 1 <mkdir dir="${project.build.directory}/dist/lib"/>
) <property name="dist.lib" value="${project.build.directory}/dist/lib"/>
<path id="deps.jars">
REM 检查lib目录是否存在 <fileset dir="${dist.lib}" includes="*.jar"/>
if not exist "lib" ( </path>
echo 错误: 找不到依赖库目录 lib <pathconvert refid="deps.jars" property="deps.cp" pathsep=" ">
echo 请确保lib目录存在且完整 <map from="${project.build.directory}/dist/lib/" to="lib/"/>
pause <map from="${project.build.directory}/dist/lib\" to="lib/"/>
exit /b 1 </pathconvert>
) <echo file="${project.build.directory}/dist/manifest.mf">
Manifest-Version: 1.0
REM 运行程序 Main-Class: com.coproject.MainApp
echo 正在启动程序... Class-Path: ${deps.cp}
java --module-path "lib" --add-modules javafx.controls,javafx.fxml -jar "math-learning-0.1.2.jar" </echo>
</target>
REM 如果程序退出,暂停窗口 </configuration>
echo. <goals>
echo 程序已退出。 <goal>run</goal>
pause </goals>
</echo> </execution>
<execution>
<!-- 生成简化版启动脚本 --> <id>prepare-smtp-env</id>
<echo file="${project.build.directory}/数学学习软件-${project.version}/start.bat">@echo off <phase>package</phase>
chcp 65001 >nul <configuration>
cd /d %~dp0 <target>
java --module-path "lib" --add-modules javafx.controls,javafx.fxml -jar "math-learning-0.1.2.jar" <mkdir dir="${project.build.directory}/dist/data"/>
</echo> <copy file="${project.basedir}/smtp.env.example" tofile="${project.build.directory}/dist/data/smtp.env" overwrite="true"/>
<mkdir dir="${project.build.directory}/dist/data/papers"/>
<!-- 生成测试脚本 --> </target>
<echo file="${project.build.directory}/数学学习软件-${project.version}/测试运行.bat">@echo off </configuration>
echo 正在测试程序是否能正常运行... <goals>
cd /d %~dp0 <goal>run</goal>
</goals>
if exist "math-learning-0.1.2.jar" ( </execution>
echo ✓ 找到主程序文件 <execution>
) else ( <id>cleanup-dist-jars</id>
echo ✗ 找不到主程序文件 <phase>post-package</phase>
pause <configuration>
exit /b 1 <target>
) <delete>
<fileset dir="${project.build.directory}/dist" includes="math-learning-*.jar" excludes="${project.build.finalName}.jar"/>
if exist "lib" ( </delete>
echo ✓ 找到依赖库目录 </target>
) else ( </configuration>
echo ✗ 找不到依赖库目录 <goals>
pause <goal>run</goal>
exit /b 1 </goals>
) </execution>
</executions>
echo 正在启动程序... </plugin>
start start.bat <plugin>
</echo> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<!-- 复制主程序 JAR --> <version>3.3.1</version>
<copy todir="${project.build.directory}/数学学习软件-${project.version}"> <executions>
<fileset dir="${project.build.directory}/dist"> <execution>
<include name="math-learning-${project.version}.jar"/> <id>copy-javafx-win</id>
<include name="README.md"/> <phase>prepare-package</phase>
<include name="smtp.env.example"/> <goals>
<include name="configure_smtp_qq.ps1"/> <goal>copy-resources</goal>
</fileset> </goals>
</copy> <configuration>
<outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
<!-- 复制依赖库 --> <resources>
<copy todir="${project.build.directory}/数学学习软件-${project.version}/lib"> <resource>
<fileset dir="${project.build.directory}/dist/lib"/> <directory>${project.basedir}/lib/javafx-win</directory>
</copy> <includes>
<include>javafx-*-win.jar</include>
<!-- 复制数据目录 --> </includes>
<copy todir="${project.build.directory}/数学学习软件-${project.version}/data"> </resource>
<fileset dir="${project.build.directory}/dist/data"/> </resources>
</copy> </configuration>
</execution>
<!-- 生成详细使用说明 --> <execution>
<echo file="${project.build.directory}/数学学习软件-${project.version}/使用说明.txt">数学学习软件 v${project.version} <id>copy-app-README</id>
<phase>package</phase>
=== 基本信息 === <goals>
版本号: ${project.version} <goal>copy-resources</goal>
生成时间: ${maven.build.timestamp} </goals>
开发者: Coproject 团队 <configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
=== 系统要求 === <resources>
- 操作系统: Windows 7/10/11 <resource>
- Java版本: Java 17 或更高版本 <directory>${project.basedir}</directory>
- 内存: 至少 2GB 可用内存 <includes>
<include>README.md</include>
=== 启动方法 === </includes>
推荐使用以下方式启动: </resource>
1. 双击运行 run.bat (推荐,有错误提示) </resources>
2. 双击 start.bat (简洁版) </configuration>
3. 双击 测试运行.bat (先检查环境再启动) </execution>
<execution>
=== 功能特点 === <id>copy-config-guides</id>
✓ 小学数学题目生成 <phase>package</phase>
✓ 初中数学题目生成(含平方根号) <goals>
✓ 高中数学题目生成(含三角函数) <goal>copy-resources</goal>
✓ 选择题答题系统 </goals>
✓ 邮件发送验证码功能 <configuration>
✓ 试卷PDF导出功能 <outputDirectory>${project.build.directory}/dist</outputDirectory>
<resources>
=== 目录说明 === <resource>
math-learning-0.1.2.jar - 主程序文件 <directory>${project.basedir}</directory>
lib/ - 程序依赖库(请勿删除或移动) <includes>
data/ - 数据存储目录 <include>configure_smtp_qq.ps1</include>
├── smtp.env - 邮箱配置文件 </includes>
└── papers/ - 试卷存储目录 </resource>
run.bat - Windows启动脚本(详细版) </resources>
start.bat - Windows启动脚本(简洁版) </configuration>
测试运行.bat - 环境测试脚本 </execution>
README.md - 项目说明文档 </executions>
</plugin>
=== 配置说明 === <plugin>
1. 首次运行会自动创建配置文件 <groupId>org.panteleyev</groupId>
2. 邮箱配置请参考 smtp.env.example <artifactId>jpackage-maven-plugin</artifactId>
3. 生成的试卷保存在 data/papers/ 目录 <version>1.7.1</version>
<configuration>
=== 常见问题 === <input>${project.build.directory}/dist</input>
Q: 启动时提示"缺少JavaFX运行时组件" <resourceDir>${project.build.directory}/dist</resourceDir>
A: 请确保使用 .bat 文件启动不要直接双击JAR文件 <destination>${project.build.directory}/jpackage</destination>
<type>msi</type>
Q: 邮件发送失败 <name>数学学习软件</name>
A: 请检查 data/smtp.env 中的邮箱配置 <vendor>Coproject</vendor>
<appVersion>0.1.4</appVersion>
Q: 程序无法启动 <winConsole>true</winConsole>
A: 请先运行"测试运行.bat"检查环境 <winDirChooser>true</winDirChooser>
<winMenu>true</winMenu>
=== 技术支持 === <winShortcut>true</winShortcut>
如有问题请联系开发团队。 <verbose>true</verbose>
<!-- 与 maven-jar-plugin 输出一致:固定为 artifactId.jar -->
注意:请保持所有文件在同一个目录下,不要单独移动文件! <mainJar>${project.artifactId}.jar</mainJar>
</echo> <mainClass>com.coproject.MainApp</mainClass>
<!-- 让启动器包含 dist/lib 下所有依赖 -->
<!-- 创建ZIP压缩包 --> <classPath>lib/*</classPath>
<zip destfile="${project.build.directory}/数学学习软件-${project.version}.zip" <!-- JavaFX 模块所在路径 -->
basedir="${project.build.directory}/数学学习软件-${project.version}"/> <modulePath>app/lib</modulePath>
<!-- 类路径由主 JAR 的 MANIFEST.MF 提供antrun 生成 Class-Path -->
<echo>=== 分发包生成完成 ===</echo> <removeDestination>true</removeDestination>
<echo>分发包位置: ${project.build.directory}/数学学习软件-${project.version}.zip</echo> <!-- 显式添加 JavaFX 模块,避免运行期缺失导致退出码 1 -->
<echo>目录位置: ${project.build.directory}/数学学习软件-${project.version}/</echo> <javaOptions>
<echo> </echo> <javaOption>--module-path</javaOption>
<echo>用户运行说明:</echo> <javaOption>app/lib</javaOption>
<echo>- 解压ZIP文件到任意位置</echo> <javaOption>--add-modules</javaOption>
<echo>- 进入"数学学习软件-0.1.2"文件夹</echo> <javaOption>javafx.controls,javafx.fxml</javaOption>
<echo>- 双击 run.bat 启动程序</echo> </javaOptions>
</target> <!-- 不强制使用 jlink 镜像,默认使用当前 JDK 的 jpackage -->
</configuration> </configuration>
<goals> <executions>
<goal>run</goal> <execution>
</goals> <id>make-installer</id>
</execution> <phase>package</phase>
</executions> <goals>
</plugin> <goal>jpackage</goal>
</plugins> </goals>
</build> </execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<!-- 使用 -P no-jlink 强制跳过 jlink某些环境下 -DskipJlink=true 未被采纳) -->
<profile>
<id>no-jlink</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<skipJlink>true</skipJlink>
</properties>
</profile>
</profiles>
</project> </project>

@ -282,7 +282,7 @@ public class UnifiedQuestionEngine {
static class Elementary extends Base { static class Elementary extends Base {
public Elementary(Random random, DecimalFormat df) { super(random, df); } public Elementary(Random random, DecimalFormat df) { super(random, df); }
@Override public MathProblem generate() { @Override public MathProblem generate() {
int operandCount = random.nextInt(5) + 1; // 1-5 int operandCount = random.nextInt(3) + 3; // 3-5
int[] operands = new int[operandCount]; int[] operands = new int[operandCount];
String[] operators = new String[operandCount - 1]; String[] operators = new String[operandCount - 1];
for (int i = 0; i < operandCount; i++) operands[i] = random.nextInt(100) + 1; for (int i = 0; i < operandCount; i++) operands[i] = random.nextInt(100) + 1;
@ -302,85 +302,54 @@ public class UnifiedQuestionEngine {
// ---------- 初中题目生成器 ---------- // ---------- 初中题目生成器 ----------
static class Middle extends Base { static class Middle extends Base {
public Middle(Random random, DecimalFormat df) { super(random, df); } public Middle(Random random, DecimalFormat df) { super(random, df); }
@Override public MathProblem generate() {
// 始终生成 3-5 个操作数;当包含根号时,根号为其中一个或两个项
int operandCount = random.nextInt(3) + 3; // 3-5
@Override boolean includeSqrt = random.nextDouble() < 0.5; // 约半数题含根号
public MathProblem generate() { int sqrtCount = includeSqrt ? (1 + random.nextInt(2)) : 0; // 1-2 个根号项
int operandCount = random.nextInt(5) + 1; if (sqrtCount > operandCount) sqrtCount = operandCount; // 不超过操作数数量
int sqrtCount = 1 + random.nextInt(2); // 1-2个平方/根号项
if (sqrtCount > operandCount) sqrtCount = operandCount;
return buildMathProblem(operandCount, sqrtCount);
}
private MathProblem buildMathProblem(int operandCount, int sqrtCount) {
double[] operands = new double[operandCount]; double[] operands = new double[operandCount];
String[] operators = new String[operandCount - 1]; String[] operators = new String[operandCount - 1];
String[] termTexts = new String[operandCount]; String[] termTexts = new String[operandCount];
Set<Integer> sqrtPositions = selectSqrtPositions(operandCount, sqrtCount); // 随机选择根号项位置
buildOperandsAndTerms(operands, termTexts, sqrtPositions); Set<Integer> sqrtPos = new HashSet<>();
buildOperators(operators); while (sqrtPos.size() < sqrtCount) {
sqrtPos.add(random.nextInt(operandCount));
String expression = buildExpression(termTexts, operators);
double result = evaluateWithPrecedenceDouble(operands, operators);
return new MathProblem(expression, df.format(result), "初中");
}
private Set<Integer> selectSqrtPositions(int operandCount, int sqrtCount) {
Set<Integer> positions = new HashSet<>();
while (positions.size() < sqrtCount) {
positions.add(random.nextInt(operandCount));
} }
return positions;
}
private void buildOperandsAndTerms(double[] operands, String[] termTexts, Set<Integer> sqrtPositions) { // 构造各项
for (int i = 0; i < operands.length; i++) { for (int i = 0; i < operandCount; i++) {
if (sqrtPositions.contains(i)) { if (sqrtPos.contains(i)) {
buildPowerOrSqrtTerm(operands, termTexts, i); // 使用完全平方数,确保计算结果为整数,便于初中题目
int base = random.nextInt(9) + 2; // 2-10确保 √n 的 n ≤ 100
int square = base * base;
operands[i] = Math.sqrt(square); // = base
termTexts[i] = "√" + square;
} else { } else {
buildNormalTerm(operands, termTexts, i); int val = random.nextInt(100) + 1; // 1-100 的常数
operands[i] = val;
termTexts[i] = String.valueOf(val);
} }
} }
}
private void buildPowerOrSqrtTerm(double[] operands, String[] termTexts, int index) {
boolean useSquare = random.nextBoolean();
int base = random.nextInt(9) + 2; // 2-10
if (useSquare) { // 构造运算符(保持简单四则)
operands[index] = base * base;
termTexts[index] = base + "²";
} else {
int square = base * base;
operands[index] = base;
termTexts[index] = "√" + square;
}
}
private void buildNormalTerm(double[] operands, String[] termTexts, int index) {
int val = random.nextInt(100) + 1;
operands[index] = val;
termTexts[index] = String.valueOf(val);
}
private void buildOperators(String[] operators) {
String[] ops = {"+", "-", "*", "/"}; String[] ops = {"+", "-", "*", "/"};
for (int i = 0; i < operators.length; i++) { for (int i = 0; i < operandCount - 1; i++) {
operators[i] = ops[random.nextInt(ops.length)]; operators[i] = ops[random.nextInt(ops.length)];
} }
}
private String buildExpression(String[] termTexts, String[] operators) { // 拼接表达式文本
StringBuilder expr = new StringBuilder(); StringBuilder expr = new StringBuilder();
for (int i = 0; i < termTexts.length; i++) { for (int i = 0; i < operandCount; i++) {
expr.append(termTexts[i]); expr.append(termTexts[i]);
if (i < operators.length) { if (i < operandCount - 1) expr.append(" ").append(operators[i]).append(" ");
expr.append(" ").append(operators[i]).append(" ");
}
} }
return expr.toString();
double result = evaluateWithPrecedenceDouble(operands, operators);
return new MathProblem(expr.toString(), df.format(result), "初中");
} }
} }
@ -388,8 +357,8 @@ public class UnifiedQuestionEngine {
static class High extends Base { static class High extends Base {
public High(Random random, DecimalFormat df) { super(random, df); } public High(Random random, DecimalFormat df) { super(random, df); }
@Override public MathProblem generate() { @Override public MathProblem generate() {
// 生成包含三角函数与常数混合运算的表达式(1-5个操作数至少包含一个三角函数 // 生成包含三角函数与常数混合运算的表达式(3-5个操作数至少包含一个三角函数
int operandCount = random.nextInt(5) + 1; // 1-5 int operandCount = random.nextInt(3) + 3; // 3-5
int trigIndex = random.nextInt(operandCount); int trigIndex = random.nextInt(operandCount);
double[] operands = new double[operandCount]; double[] operands = new double[operandCount];
@ -444,9 +413,6 @@ public class UnifiedQuestionEngine {
// 混合三角表达式(包含常数与一个三角项)选项符号化 // 混合三角表达式(包含常数与一个三角项)选项符号化
private ChoiceQuestion toMixedTrigSumChoice(MathProblem p) { private ChoiceQuestion toMixedTrigSumChoice(MathProblem p) {
String expr = p.getExpression(); String expr = p.getExpression();
if (isPureTrigFunction(expr)) {
return createPureTrigChoiceQuestion(expr, p);
}
String[] tokens = expr.split(" "); String[] tokens = expr.split(" ");
// 解析操作数与运算符 // 解析操作数与运算符
@ -556,44 +522,6 @@ public class UnifiedQuestionEngine {
int correctIndex = options.indexOf(correct); int correctIndex = options.indexOf(correct);
return new ChoiceQuestion(p.getExpression() + " = ?", options, correctIndex); return new ChoiceQuestion(p.getExpression() + " = ?", options, correctIndex);
} }
// 判断是否为纯三角函数题目
private boolean isPureTrigFunction(String expr) {
return expr.matches("^(sin|cos|tan)\\d+$");
}
// 创建纯三角函数选择题
// 创建纯三角函数选择题
private ChoiceQuestion createPureTrigChoiceQuestion(String expr, MathProblem p) {
String func = expr.substring(0, 3);
int angle = Integer.parseInt(expr.substring(3));
String trigSym = getSymbolicTrig(func, angle);
// 正确答案
String correct = trigSym;
// 生成干扰项 - 只使用符号形式,避免数值
Set<String> optionsSet = new LinkedHashSet<>();
optionsSet.add(correct);
String[] symPool = {"0", "1/2", "√2/2", "√3/2", "1", "1/√3", "√3"};
for (String sym : symPool) {
if (optionsSet.size() >= 4) break;
if (!sym.equals(trigSym)) {
optionsSet.add(sym);
}
}
// 确保选项数量为4个
while (optionsSet.size() < 4) {
optionsSet.add("1"); // 或其他默认值
}
List<String> options = new ArrayList<>(optionsSet);
Collections.shuffle(options, random);
int correctIndex = options.indexOf(correct);
return new ChoiceQuestion(expr + " = ?", options, correctIndex);
}
// 解析常见符号三角值为数值(仅用于没有根号的情况) // 解析常见符号三角值为数值(仅用于没有根号的情况)
private double parseSymbolicToDouble(String s) { private double parseSymbolicToDouble(String s) {

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.coproject.MainApp
Class-Path: lib/commons-lang3-3.14.0.jar lib/jackson-annotations-2.17.1.jar lib/jackson-core-2.17.1.jar lib/jackson-databind-2.17.1.jar lib/jakarta.activation-2.0.1.jar lib/jakarta.mail-2.0.1.jar lib/javafx-base-21.0.3-win.jar lib/javafx-base-21.0.3.jar lib/javafx-controls-21.0.3-win.jar lib/javafx-controls-21.0.3.jar lib/javafx-fxml-21.0.3-win.jar lib/javafx-graphics-21.0.3-win.jar lib/javafx-graphics-21.0.3.jar

Binary file not shown.
Loading…
Cancel
Save