Compare commits

..

5 Commits

Binary file not shown.

@ -30,7 +30,7 @@
## 技术架构 ## 技术架构
### 开发环境 ### 开发环境
- **语言**Java 11 - **语言**Java 21
- **构建工具**Maven - **构建工具**Maven
- **GUI框架**Swing - **GUI框架**Swing
- **数据格式**JSON - **数据格式**JSON
@ -38,25 +38,25 @@
### 项目结构 ### 项目结构
``` ```
src/ src/
├── model/ # 数据模型层 ├── model/ # 数据模型层
│ ├── UserManager.java # 用户管理 │ ├── UserManager.java # 用户管理
│ ├── QuestionMaker.java # 题目生成器 │ ├── QuestionMaker.java # 题目生成器
│ ├── Question.java # 题目模型 │ ├── Question.java # 题目模型
│ ├── Grade.java # 年级枚举 │ ├── Grade.java # 年级枚举
│ ├── PrimaryMaker.java # 小学题目生成 │ ├── PrimaryMaker.java # 小学题目生成
│ ├── MiddleMaker.java # 初中题目生成 │ ├── MiddleMaker.java # 初中题目生成
│ └── HighMaker.java # 高中题目生成 │ └── HighMaker.java # 高中题目生成
├── ui/ # 用户界面层 ├── ui/ # 用户界面层
│ ├── MathLearningApp.java # 主程序入口 │ ├── MathLearningApp.java # 主程序入口
│ ├── LoginFrame.java # 登录界面 │ ├── LoginFrame.java # 登录界面
│ ├── RegisterDialog.java # 注册对话框 │ ├── RegisterDialog.java # 注册对话框
│ ├── GradeSelectionFrame.java # 年级选择界面 │ ├── GradeSelectionFrame.java # 年级选择界面
│ ├── QuizFrame.java # 答题界面 │ ├── QuizFrame.java # 答题界面
│ ├── ResultFrame.java # 结果界面 │ ├── ResultFrame.java # 结果界面
│ ├── UserSettingsDialog.java # 用户设置 │ ├── UserSettingsDialog.java # 用户设置
│ └── ChangePasswordDialog.java # 修改密码 │ └── ChangePasswordDialog.java # 修改密码
├── others/ # 资源文件 ├── others/ # 资源文件
└── Math.exe # 可执行文件 └── Math.exe # 可执行文件
``` ```
### 依赖库 ### 依赖库
@ -66,9 +66,20 @@ src/
## 安装与运行 ## 安装与运行
### 环境要求 ### 平台支持
- Java 11 或更高版本 ![Windows](https://img.shields.io/badge/Windows-Supported-0078D6?logo=windows)
- Maven 3.6 或更高版本
| 操作系统 | 版本要求 | 支持状态 | 应用 |
|---------|-------------|--------|---------|
| Windows | 11 | ✅ 完全支持 | exe应用程序 |
运行前,请确保系统已安装以下环境:
**Windows:**
- **openjdk** 21.0.5(2024-10-15 LTS)
- **OpenJDK Runtime Environment** build 21.0.5+11-LTS
- **OpenJDK 64-Bit Server VM** build 21.0.5+11-LTS, mixed mode, sharing
- **Maven** 3.6或更高版本
### 直接运行 ### 直接运行
- 双击Math.exe程序 - 双击Math.exe程序
@ -97,7 +108,7 @@ mvn package
### 练习流程 ### 练习流程
1. 登录成功后选择年级系统会根据注册信息自动推荐以及题目数量10-30 1. 登录成功后选择年级系统会根据注册信息自动推荐以及题目数量10-30
2. 进入答题界面开始练习 2. 进入答题界面开始练习
3. 选择答案后点击"下一题"继续 3. 选择答案后点击“下一题”继续
4. 完成所有题目后自动生成并显示成绩 4. 完成所有题目后自动生成并显示成绩
### 用户设置 ### 用户设置
@ -120,8 +131,14 @@ mvn package
要添加新的题目类型,需要: 要添加新的题目类型,需要:
1. 在相应的`*Maker.java`类中添加题目生成逻辑 1. 在相应的`*Maker.java`类中添加题目生成逻辑
2. 在`QuestionMaker.java`中更新表达式解析逻辑 2. 在`QuestionMaker.java`中更新表达式解析逻辑
3. 更新`Grade.java`枚举(如果需要新的年级) 3. 如果需要新的年级,更新`Grade.java`枚举
### 自定义界面 ### 自定义界面
所有UI组件都使用Swing开发可以通过修改对应的`*.java`文件来自定义界面样式和布局。 所有UI组件都使用Swing开发可以通过修改对应的`*.java`文件来自定义界面样式和布局。
### 分工
后端:张一帆
前端:赵俊杰
邮箱注册实现:张一帆,赵俊杰
调试:赵俊杰,张一帆

@ -0,0 +1,143 @@
# 小初高数学学习系统
## 项目简介
这是一个基于Java Swing开发的数学学习软件专门为小学、初中和高中学生设计。系统提供个性化的数学题目生成、答题练习和成绩记录功能支持用户注册、登录和密码管理。
## 功能特性
### 用户管理
- **用户注册**:支持邮箱、用户名、密码和年级选择注册
- **用户登录**:支持邮箱或用户名登录
- **密码修改**:安全的密码修改功能
- **数据持久化**用户数据使用JSON格式存储
### 题目生成
- **分级题目**:根据用户年级(小学、初中、高中)生成相应难度的数学题目
- **智能去重**:避免重复题目,确保练习效果
- **题目保存**:自动保存生成的题目到用户专属文件夹
### 答题系统
- **选择题形式**每题提供4个选项
- **实时评分**:答题后立即显示结果
- **成绩记录**:记录每次练习的成绩
### 年级适配
- **小学**:基础四则运算
- **初中**:包含平方、平方根运算
- **高中**:包含三角函数运算
## 技术架构
### 开发环境
- **语言**Java 21
- **构建工具**Maven
- **GUI框架**Swing
- **数据格式**JSON
### 项目结构
```
src/
├── model/ # 数据模型层
│ ├── UserManager.java # 用户管理
│ ├── QuestionMaker.java # 题目生成器
│ ├── Question.java # 题目模型
│ ├── Grade.java # 年级枚举
│ ├── PrimaryMaker.java # 小学题目生成
│ ├── MiddleMaker.java # 初中题目生成
│ └── HighMaker.java # 高中题目生成
├── ui/ # 用户界面层
│ ├── MathLearningApp.java # 主程序入口
│ ├── LoginFrame.java # 登录界面
│ ├── RegisterDialog.java # 注册对话框
│ ├── GradeSelectionFrame.java # 年级选择界面
│ ├── QuizFrame.java # 答题界面
│ ├── ResultFrame.java # 结果界面
│ ├── UserSettingsDialog.java # 用户设置
│ └── ChangePasswordDialog.java # 修改密码
├── others/ # 资源文件
└── Math.exe # 可执行文件
```
### 依赖库
- **Gson 2.10.1**JSON数据处理
- **JavaMail 1.6.2**:邮件功能支持
- **JAF 1.1.1**JavaBeans激活框架
## 安装与运行
### 平台支持
![Windows](https://img.shields.io/badge/Windows-Supported-0078D6?logo=windows)
| 操作系统 | 版本要求 | 支持状态 | 应用 |
|---------|-------------|--------|---------|
| Windows | 11 | ✅ 完全支持 | exe应用程序 |
运行前,请确保系统已安装以下环境:
**Windows:**
- **openjdk** 21.0.5(2024-10-15 LTS)
- **OpenJDK Runtime Environment** build 21.0.5+11-LTS
- **OpenJDK 64-Bit Server VM** build 21.0.5+11-LTS, mixed mode, sharing
- **Maven** 3.6或更高版本
### 直接运行
- 双击Math.exe程序
### 编译运行
```bash
# 使用Maven编译
mvn compile
# 运行程序
mvn exec:java
# 打包为可执行JAR
mvn package
```
## 使用说明
### 首次使用
1. 启动程序后进入登录界面
2. 点击"注册"按钮创建新账户
3. 填写邮箱、验证码
4. 验证完成后填写用户名、密码并选择年级
5. 注册成功后返回登录界面登录
### 练习流程
1. 登录成功后选择年级系统会根据注册信息自动推荐以及题目数量10-30
2. 进入答题界面开始练习
3. 选择答案后点击“下一题”继续
4. 完成所有题目后自动生成并显示成绩
### 用户设置
- 在登录后界面可以修改密码
- 系统会自动保存练习记录
## 数据存储
### 用户数据
- 位置:`users.json`
- 格式JSON格式存储用户信息
### 题目记录
- 位置:`questions/{邮箱}/` 目录
- 格式:按时间戳命名的文本文件
## 开发说明
### 扩展题目类型
要添加新的题目类型,需要:
1. 在相应的`*Maker.java`类中添加题目生成逻辑
2. 在`QuestionMaker.java`中更新表达式解析逻辑
3. 如果需要新的年级,更新`Grade.java`枚举
### 自定义界面
所有UI组件都使用Swing开发可以通过修改对应的`*.java`文件来自定义界面样式和布局。
### 分工
后端:张一帆
前端:赵俊杰
邮箱注册实现:张一帆,赵俊杰
调试:赵俊杰,张一帆

@ -1,81 +0,0 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mathlearning</groupId>
<artifactId>math-learning-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>数学学习软件</name>
<description>小初高数学学习系统</description>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Gson for JSON processing -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<!-- JavaMail for email functionality -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!-- JAF (JavaBeans Activation Framework) for JavaMail -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>ui.MathLearningApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>ui.MathLearningApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,19 @@
1. 78 / 96 - 22
2. 15 - 17 * (49 / 15)
3. 66 + 28 / 8 + (1 / 25)
4. 16 * 2
5. 26 * (98 * 90)
6. 4 + 89 * (70 / 3)
7. 41 + 36 + 55 + 9
8. 79 * 72 - 19 * 32
9. 2 - 2
10. (32 - 82) + 16

@ -0,0 +1,19 @@
1. 70^2 / 64
2. 11^2 + 31 + 85^2 - 61
3. √95 - 73^2 + 54^2
4. 53 - (√44 / 53 / 5^2) * 10^2
5. 5^2 * 75^2 / √24 - √20 + 74^2
6. √74 / 82
7. 31 * 5 * √42 / 53^2 + √19
8. 48 + 24^2 / 12^2 - 87
9. (37 * 38^2 + √82 / 27) + 32
10. 95 - √72

@ -0,0 +1,19 @@
1. 47 * sin(70) * 75 - cos(16)
2. sin(65) * 38 - 16 / 68
3. 78 * sin(50) / cos(70) * tan(52)
4. (cos(70) + sin(12) + 85) - cos(46)
5. cos(60) / tan(4)
6. 70 * sin(28) * sin(7)
7. tan(74) - cos(55) - cos(42) + tan(38)
8. tan(62) + 45
9. 52 + sin(33)
10. sin(40) * 29

@ -0,0 +1,19 @@
1. 5 - 41
2. 12 / (22 / 21 + 5) + 60
3. 88 * (22 * 75)
4. 5 - 23
5. 72 * 61 / 59 + 40
6. 2 * 50
7. (37 * 99) - 92 * 43
8. 85 * 88
9. 38 / 98 - 75 - 71 / 74
10. 63 / (44 - 25)

@ -0,0 +1,59 @@
1. 6 + 63 - 75 - 63 / 89
2. 38 - (63 * 76) + 41
3. 31 * 90 - 8 + 28 + 19
4. 21 - 94
5. 66 + (49 / 9)
6. 36 * 30 * 74
7. 63 / 1 / 57 - (12 - 60)
8. (91 - 68 + 66) * 7 * 83
9. 68 - 47 / 15 / 84
10. 38 - 47 * 19
11. 4 * 19
12. 48 / 38 - 62
13. 27 - 13
14. 32 * 61 / 25 + 11
15. 10 - 71
16. 92 - (27 + 99 - 57)
17. 9 / (79 - 59)
18. 76 - (68 + 73 + 20)
19. 97 - 1
20. 66 - 43 * 19 / 2 / 2
21. 78 / 35 / 94 + 12 - 16
22. 54 + 96
23. 34 - 5
24. 4 - 39
25. 58 / 17 / 45 * 51
26. 3 + (71 - 43)
27. 21 - (93 - 83)
28. 46 / 10 / 20 + 42
29. 35 + (37 - 90)
30. 83 * 82 - 11

@ -0,0 +1,59 @@
1. (58 + 75) / 19
2. 99 * 13 / 10
3. 97 + 77
4. (93 + 10) - 26
5. 21 + 77 - 78 / (56 - 28)
6. 97 + 19
7. 97 * (31 + 25 * 33 * 68)
8. 90 / 18 - 80 - 51
9. 53 / 28 - 39 + 21
10. 58 - 94
11. 10 - 85
12. 100 - 42 * 9 - 66 - 49
13. 22 / (19 + 65 / 17)
14. 48 + (5 + 2) - 25
15. 91 * 9 * (26 + 70) / 93
16. 20 + 3 / 42
17. 24 / 39 * 69
18. (88 + 55) + 14 * 42
19. 90 / 16 / 9 * 75
20. 84 * 65 / (84 - 70)
21. 30 * (85 / 83)
22. 89 / 15 - 55 - 60 + 37
23. (12 * 66) / 82
24. (27 + 47) / 85
25. 12 * 80 + 27 * 86 * 80
26. 47 + 88 + (45 * 29)
27. 93 + 7 + 28 + 85 / 92
28. 9 * 4 + (62 / 31)
29. 6 - 26
30. (58 * 70) + 55

@ -0,0 +1,19 @@
1. 14 * 25 * (59 * 79)
2. 21 / 91 / 4 - (87 * 16)
3. 46 / 20 + 87 * 59
4. 86 / (7 * 59)
5. 85 / 35 * 30
6. 9 - 88
7. 27 + 10
8. 94 - 76 / 59
9. 37 / (5 / 89)
10. 25 * 46 + 28

@ -0,0 +1,19 @@
1. (39 + 54^2 * 23) / 1 * 50
2. 69^2 - 82
3. √36 + 49
4. √44 + 45 * 95
5. 10 - 76^2 - 70^2
6. √72 * √98 * 16
7. (27^2 / 48) / 10
8. 9 + √40 - 25 / √69 / 13^2
9. 73 * (8^2 / 61 - 96)
10. √34 / (√64 * 79)

@ -0,0 +1,19 @@
1. 57 / cos(76)
2. sin(5) - sin(22)
3. sin(90) + sin(66) * 79
4. (sin(42) + tan(30)) - cos(68)
5. tan(96) - tan(54) / tan(30)
6. tan(24) / 73
7. 76 + sin(86) - sin(98) * 25
8. 16 / cos(84) * 22
9. cos(77) + sin(21) * 14
10. sin(19) * cos(53) / 28

@ -0,0 +1,19 @@
1. 40 * 27 + 23 - 30
2. 27 / 53
3. 55 + 18 / 51 + 70 * 20
4. 66 - 81
5. 68 + (87 / 23)
6. 48 / 62
7. (79 + 74 * 6) + 99
8. 66 * 67
9. 20 + 5 - 43 * 28 * 16
10. (46 + 78) + 1

@ -0,0 +1,19 @@
1. 81 / 75 - 19
2. 95 + 44
3. 6 + 8 / 22 + (57 / 34)
4. 22 + 85 / 59
5. 60 + (30 * 8)
6. 63 - (38 / 14 * 93)
7. 64 / 88 * 54
8. 79 * (34 + 5) - 47
9. 73 - 84 * 29 + 26 - 44
10. 9 * 75 - 38 / 25 * 95

@ -0,0 +1,59 @@
1. 52 + 30 + 60 - 7 / 65
2. 23 / 47 - 30
3. 36 / 9
4. 95 + 60 - 100 - 96
5. 19 * 7
6. 99 / 90 - 100 * 63 + 21
7. 63 / (85 - 71)
8. 74 * (38 * 43 * 87) / 91
9. 66 * 9
10. (93 / 61) / 62 + 45 + 72
11. 92 * 27 - 23 * 75 / 90
12. 14 / 53 + 93 * 62
13. 54 * 43 / (39 - 87 + 57)
14. 15 * 35 * 2 * 87
15. 1 / 70 + (26 * 63)
16. 74 / (1 / 42 / 83) - 21
17. 79 - 26 - 36 + (85 + 4)
18. 61 + (57 / 14 - 47) * 47
19. 3 + 96
20. 13 / 71 + 82 / (78 + 34)
21. 82 - 18
22. 77 / 93 * 8 * 8
23. 30 + 16 - 32 + 22 + 50
24. 34 * (7 - 14 - 44)
25. 9 - 23 / (37 * 28)
26. 8 / 78
27. 81 + 18 / (34 + 28)
28. 60 + 76 * 10 - 27
29. 51 - (62 / 92 * 12)
30. 53 * 88

@ -0,0 +1,19 @@
1. √27 / 1^2
2. 68 / √67
3. √52 / 59^2 / √95 + 37
4. 49 * (√15 + 5^2)
5. √61 + 84^2
6. √32 - 39^2 * 63^2 * 92^2 / √38
7. √92 + 24^2 + 91
8. √57 * 82^2 - 18 - 4^2 * √88
9. 20 - 76 + 29 * √88 * 85
10. √95 + 3^2

@ -0,0 +1,19 @@
1. tan(19) - sin(59) + cos(77)
2. cos(36) / tan(52) + tan(21)
3. (1 - 17 * 45 - cos(30)) + 68
4. 30 + cos(93) - 94
5. 26 * (sin(13) / cos(33)) - sin(28)
6. tan(82) - 7 - 33 * cos(86)
7. sin(99) * 97
8. 41 + tan(8) + tan(96) + 23 / sin(37)
9. 21 + cos(59)
10. tan(56) * 36 * sin(37) + 77

@ -0,0 +1,19 @@
1. 31 * (35 + 41)
2. 20 + 42 / 41 / (83 + 75)
3. 50 - 89
4. 42 * 55
5. 93 / 14 + 6 / 28
6. (14 / 64) + 94
7. 99 * 38 - 33 / 57 * 84
8. 46 - 23 / 94 - (28 - 45)
9. 49 + 23
10. 78 + (94 / 21 / 65)

@ -0,0 +1,19 @@
1. ( 18 / 41 ) + 99
2. 90 + 68 - ( 36 / 50 + 47 )
3. 98 - ( 87 + 3 / 44 )
4. 76 * 44 - 23 * 81 / 33
5. 69 * 4 * 73 + 47 - 47
6. 94 + 59
7. 62 - 62 + 22
8. 42 * 38 * 77 + 10
9. 3 - ( 3 / 49 ) / 10
10. 80 + 9 / 13 + 90

@ -0,0 +1,19 @@
1. 37 - 37
2. 48 - 8 + 15 + 99 + 48
3. 55 * 64 * 67 + 11 + 11
4. 88 * 44
5. 44 - 44
6. ( 71 - 16 ) / 64
7. 27 - 27
8. ( 33 / 51 ) * 83
9. 93 / 58
10. 99 * 52

@ -0,0 +1,19 @@
1. 77 * ( 87 / 13 + 74 )
2. 95 - 4 * 16
3. 28 / ( 51 + 88 )
4. 56 * 30
5. 67 + ( 53 - 53 )
6. ( 67 / 78 + 92 ) / 24 * 5
7. 10 + 60 / 96
8. 87 + 48 / 85
9. 55 / 79 + 72
10. ( 88 + 48 ) - 48

@ -0,0 +1,19 @@
1. √42 + √99
2. √85 - 9^2 / 4 * 89 * 49^2
3. 7^2 - 7^2 / (17^2 / √96)
4. √32 / 92 - √37 + 20
5. 75 * √93
6. 29 / (24^2 + 46 / 89)
7. √56 * 17^2 + √80
8. 95^2 - 43 - √77 * 13 * √60
9. 1^2 + 9^2
10. (56^2 - 24) * 71^2

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: ui.MathLearningApp

@ -1,38 +1,40 @@
package model; package model;
/**
*
*/
public class HighMaker extends Student { public class HighMaker extends Student {
final static String[] binaryOps = {"+", "-", "*", "/"}; // 操作符 private static final String[] BINARY_OPS = {"+", "-", "*", "/"};
final static String[] trigOps = {"sin", "cos", "tan"}; private static final String[] TRIG_OPS = {"sin", "cos", "tan"};
int operandCount;
String[] questionParts; // 操作数 private int operandCount;
boolean[] specialOpFlags; // 特殊操作符索引序列 private String[] questionParts;
int parenStart; private boolean[] specialOpFlags;
int parenEnd; private int parenStart;
private int parenEnd;
public HighMaker(String name, String password, String path) { public HighMaker(String name, String password, String path) {
super(name, password, path); super(name, password, path);
} }
// 数据预处理 /**
*
*/
public void getRandom() { public void getRandom() {
this.operandCount = random.nextInt(4) + 2; this.operandCount = random.nextInt(4) + 2;
this.questionParts = new String[this.operandCount]; this.questionParts = new String[this.operandCount];
// 使用基类方法随机决定特殊操作符的数量和位置
this.specialOpFlags = randomMaker(this.operandCount); this.specialOpFlags = randomMaker(this.operandCount);
// 生成题目各部分
for (int i = 0; i < operandCount; i++) { for (int i = 0; i < operandCount; i++) {
int operand = random.nextInt(100) + 1; int operand = random.nextInt(100) + 1;
if (specialOpFlags[i]) { if (specialOpFlags[i]) {
String op = trigOps[random.nextInt(trigOps.length)]; String op = TRIG_OPS[random.nextInt(TRIG_OPS.length)];
questionParts[i] = op + "(" + operand + ")"; questionParts[i] = op + "(" + operand + ")";
} else { } else {
questionParts[i] = String.valueOf(operand); questionParts[i] = String.valueOf(operand);
} }
} }
// 使用基类方法生成有效括号
int[] parenPos = bracketMaker(this.operandCount); int[] parenPos = bracketMaker(this.operandCount);
this.parenStart = parenPos[0]; this.parenStart = parenPos[0];
this.parenEnd = parenPos[1]; this.parenEnd = parenPos[1];
@ -53,11 +55,10 @@ public class HighMaker extends Student {
} }
if (i < operandCount - 1) { if (i < operandCount - 1) {
String op = binaryOps[random.nextInt(binaryOps.length)]; String op = BINARY_OPS[random.nextInt(BINARY_OPS.length)];
question.append(" ").append(op).append(" "); question.append(" ").append(op).append(" ");
} }
} }
return question.toString(); return question.toString();
} }
} }

@ -1,31 +1,33 @@
package model; package model;
/**
*
*/
public class MiddleMaker extends Student { public class MiddleMaker extends Student {
final static String[] binaryOps = {"+", "-", "*", "/"}; // 操作符 private static final String[] BINARY_OPS = {"+", "-", "*", "/"};
final static String[] unaryOps = {"^2", "√"}; private static final String[] UNARY_OPS = {"^2", "√"};
int operandCount;
String[] questionParts; // 操作数 private int operandCount;
boolean[] specialOpFlags; // 特殊操作符索引序列 private String[] questionParts;
int parenStart; private int parenStart;
int parenEnd; private int parenEnd;
public MiddleMaker(String name, String password, String path) { public MiddleMaker(String name, String password, String path) {
super(name, password, path); super(name, password, path);
} }
// 数据预处理 /**
*
*/
public void getRandom() { public void getRandom() {
this.operandCount = random.nextInt(4) + 2; this.operandCount = random.nextInt(4) + 2;
this.questionParts = new String[this.operandCount]; this.questionParts = new String[this.operandCount];
boolean[] specialOpFlags = randomMaker(this.operandCount);
// 使用基类方法随机决定特殊操作符的数量和位置
this.specialOpFlags = randomMaker(this.operandCount);
// 生成题目各部分
for (int i = 0; i < this.operandCount; i++) { for (int i = 0; i < this.operandCount; i++) {
int operand = random.nextInt(100) + 1; int operand = random.nextInt(100) + 1;
if (this.specialOpFlags[i]) { if (specialOpFlags[i]) {
String op = unaryOps[random.nextInt(unaryOps.length)]; String op = UNARY_OPS[random.nextInt(UNARY_OPS.length)];
if (op.equals("√")) { if (op.equals("√")) {
this.questionParts[i] = op + operand; this.questionParts[i] = op + operand;
} else { } else {
@ -36,7 +38,6 @@ public class MiddleMaker extends Student {
} }
} }
// 使用基类方法生成有效括号
int[] parenPos = bracketMaker(this.operandCount); int[] parenPos = bracketMaker(this.operandCount);
this.parenStart = parenPos[0]; this.parenStart = parenPos[0];
this.parenEnd = parenPos[1]; this.parenEnd = parenPos[1];
@ -56,10 +57,9 @@ public class MiddleMaker extends Student {
question.append(")"); question.append(")");
} }
if (i < operandCount - 1) { if (i < operandCount - 1) {
question.append(" ").append(binaryOps[random.nextInt(binaryOps.length)]).append(" "); question.append(" ").append(BINARY_OPS[random.nextInt(BINARY_OPS.length)]).append(" ");
} }
} }
return question.toString(); return question.toString();
} }
} }

@ -2,141 +2,185 @@ package model;
import java.util.Stack; import java.util.Stack;
/**
*
*/
public class PrimaryMaker extends Student { public class PrimaryMaker extends Student {
final static char[] operators = {'+', '-', '*', '/'}; // 操作符 private static final char[] OPERATORS = {'+', '-', '*', '/'};
int operandCount; // 操作数个数 private static final int MIN_OPERANDS = 2;
int[] operands; // 操作数列表 private static final int MAX_OPERANDS = 5;
int parenStart; // 左括号索引 private static final int MAX_OPERAND_VALUE = 100;
int parenEnd; // 右括号
private int operandCount;
private int[] operands;
private int parenStart;
private int parenEnd;
public PrimaryMaker(String name, String password, String path) { public PrimaryMaker(String name, String password, String path) {
super(name, password, path); super(name, password, path);
} }
// 数据预处理
public void getRandom() {
// 随机决定操作数个数
this.operandCount = random.nextInt(4) + 2;
this.operands = new int[operandCount];
for (int j = 0; j < operandCount; j++) {
operands[j] = random.nextInt(100) + 1;
}
// 使用基类方法生成有效括号
int[] parenPos = bracketMaker(this.operandCount);
this.parenStart = parenPos[0];
this.parenEnd = parenPos[1];
}
@Override @Override
protected String makeOneQuestion() { protected String makeOneQuestion() {
String question; String question;
while (true) { while (true) {
question = generateSingleQuestion(); question = generateSingleQuestion();
try { if (isQuestionValid(question)) {
if (isQuestionValid(question)) { break;
break;
}
} catch (Exception e) {
// 如果表达式求值出错,重新生成
} }
} }
return question; return question;
} }
private String generateSingleQuestion() { private String generateSingleQuestion() {
getRandom(); generateRandomParameters();
StringBuilder question = new StringBuilder(); StringBuilder question = new StringBuilder();
for (int j = 0; j < this.operandCount; j++) { for (int j = 0; j < operandCount; j++) {
if (j == this.parenStart) { appendOperandWithParentheses(question, j);
question.append("( ");
} if (j < operandCount - 1) {
question.append(operands[j]); appendOperator(question, j);
if (j == this.parenEnd) {
question.append(" )");
} }
}
return question.toString();
}
if (j < this.operandCount - 1) { private void generateRandomParameters() {
char op = operators[random.nextInt(operators.length)]; operandCount = random.nextInt(MAX_OPERANDS - MIN_OPERANDS + 1) + MIN_OPERANDS;
operands = new int[operandCount];
// 确保减法时被减数大于减数 for (int j = 0; j < operandCount; j++) {
if (op == '-' && operands[j] < operands[j + 1]) { operands[j] = random.nextInt(MAX_OPERAND_VALUE) + 1;
int temp = operands[j]; }
operands[j] = operands[j + 1];
operands[j + 1] = temp;
}
if (op == '/') { int[] parenPos = bracketMaker(operandCount);
this.operands[j + 1] = this.operands[j + 1] == 0 ? 1 : this.operands[j + 1]; parenStart = parenPos[0];
} parenEnd = parenPos[1];
question.append(" ").append(op).append(" "); }
}
private void appendOperandWithParentheses(StringBuilder question, int index) {
if (index == parenStart) {
question.append("( ");
}
question.append(operands[index]);
if (index == parenEnd) {
question.append(" )");
}
}
private void appendOperator(StringBuilder question, int currentIndex) {
char op = OPERATORS[random.nextInt(OPERATORS.length)];
if (op == '-') {
ensureSubtractionIsValid(currentIndex);
}
if (op == '/') {
ensureDivisionIsValid(currentIndex);
}
question.append(" ").append(op).append(" ");
}
private void ensureSubtractionIsValid(int currentIndex) {
if (operands[currentIndex] < operands[currentIndex + 1]) {
int temp = operands[currentIndex];
operands[currentIndex] = operands[currentIndex + 1];
operands[currentIndex + 1] = temp;
}
}
private void ensureDivisionIsValid(int currentIndex) {
if (operands[currentIndex + 1] == 0) {
operands[currentIndex + 1] = 1;
} }
return question.toString();
} }
private boolean isQuestionValid(String expression) { private boolean isQuestionValid(String expression) {
// 检查中间过程和最终结果是否为负数
try { try {
evaluateExpression(expression, true); evaluateExpression(expression, true);
return true;
} catch (ArithmeticException e) { } catch (ArithmeticException e) {
return false; // 捕获到负数异常 return false;
} }
return true;
} }
private double evaluateExpression(String expression, boolean checkNegative) throws ArithmeticException { private double evaluateExpression(String expression, boolean checkNegative)
throws ArithmeticException {
Stack<Double> numbers = new Stack<>(); Stack<Double> numbers = new Stack<>();
Stack<Character> ops = new Stack<>(); Stack<Character> ops = new Stack<>();
for (int i = 0; i < expression.length(); i++) { for (int i = 0; i < expression.length(); i++) {
char c = expression.charAt(i); char c = expression.charAt(i);
if (c == ' ') continue; if (c == ' ') {
continue;
}
if (Character.isDigit(c)) { if (Character.isDigit(c)) {
StringBuilder sb = new StringBuilder(); i = parseAndPushNumber(expression, i, numbers);
while (i < expression.length() && (Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
sb.append(expression.charAt(i++));
}
i--;
numbers.push(Double.parseDouble(sb.toString()));
} else if (c == '(') { } else if (c == '(') {
ops.push(c); ops.push(c);
} else if (c == ')') { } else if (c == ')') {
while (ops.peek() != '(') { processClosingParenthesis(numbers, ops, checkNegative);
double result = applyOp(ops.pop(), numbers.pop(), numbers.pop());
if (checkNegative && result < 0) {
throw new ArithmeticException("Negative intermediate result");
}
numbers.push(result);
}
ops.pop();
} else if (isOperator(c)) { } else if (isOperator(c)) {
while (!ops.isEmpty() && hasPrecedence(c, ops.peek())) { processOperator(c, numbers, ops, checkNegative);
double result = applyOp(ops.pop(), numbers.pop(), numbers.pop());
if (checkNegative && result < 0) {
throw new ArithmeticException("Negative intermediate result");
}
numbers.push(result);
}
ops.push(c);
} }
} }
processRemainingOperators(numbers, ops, checkNegative);
double finalResult = numbers.pop();
validateResult(finalResult, checkNegative);
return finalResult;
}
private int parseAndPushNumber(String expression, int startIndex,
Stack<Double> numbers) {
StringBuilder sb = new StringBuilder();
int i = startIndex;
while (i < expression.length()
&& (Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
sb.append(expression.charAt(i++));
}
numbers.push(Double.parseDouble(sb.toString()));
return i - 1;
}
private void processClosingParenthesis(Stack<Double> numbers, Stack<Character> ops,
boolean checkNegative) {
while (ops.peek() != '(') {
double result = applyOp(ops.pop(), numbers.pop(), numbers.pop());
validateResult(result, checkNegative);
numbers.push(result);
}
ops.pop();
}
private void processOperator(char currentOp, Stack<Double> numbers,
Stack<Character> ops, boolean checkNegative) {
while (!ops.isEmpty() && hasPrecedence(currentOp, ops.peek())) {
double result = applyOp(ops.pop(), numbers.pop(), numbers.pop());
validateResult(result, checkNegative);
numbers.push(result);
}
ops.push(currentOp);
}
private void processRemainingOperators(Stack<Double> numbers, Stack<Character> ops,
boolean checkNegative) {
while (!ops.isEmpty()) { while (!ops.isEmpty()) {
double result = applyOp(ops.pop(), numbers.pop(), numbers.pop()); double result = applyOp(ops.pop(), numbers.pop(), numbers.pop());
if (checkNegative && result < 0) { validateResult(result, checkNegative);
throw new ArithmeticException("Negative intermediate result");
}
numbers.push(result); numbers.push(result);
} }
}
double finalResult = numbers.pop(); private void validateResult(double result, boolean checkNegative) {
if (checkNegative && finalResult < 0) { if (checkNegative && result < 0) {
throw new ArithmeticException("Negative final result"); throw new ArithmeticException("Negative result");
} }
return finalResult;
} }
private boolean isOperator(char c) { private boolean isOperator(char c) {
@ -152,15 +196,22 @@ public class PrimaryMaker extends Student {
private double applyOp(char op, double b, double a) { private double applyOp(char op, double b, double a) {
switch (op) { switch (op) {
case '+': return a + b; case '+':
return a + b;
case '-': case '-':
if (a < b) throw new ArithmeticException("Negative result in subtraction"); if (a < b) {
throw new ArithmeticException("Negative result in subtraction");
}
return a - b; return a - b;
case '*': return a * b; case '*':
return a * b;
case '/': case '/':
if (b == 0) throw new UnsupportedOperationException("Cannot divide by zero"); if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
return a / b; return a / b;
default:
return 0;
} }
return 0;
} }
} }

@ -1,64 +1,57 @@
package model; package model;
/** /**
* *
*
*/ */
public class Question { public record Question(String expression, String questionText, String[] options, int correctAnswer) {
private final String questionText;
private final String[] options;
private final int correctAnswer;
private final String expression;
/** /**
* *
* *
* @param expression * @param expression
* @param questionText * @param questionText
* @param options (4) * @param options (4)
* @param correctAnswer (0-3) * @param correctAnswer (0-3)
*/ */
public Question(String expression, String questionText, String[] options, public Question {
int correctAnswer) {
this.expression = expression;
this.questionText = questionText;
this.options = options;
this.correctAnswer = correctAnswer;
} }
/** /**
* *
* *
* @return * @return
*/ */
public String getQuestionText() { @Override
public String questionText() {
return questionText; return questionText;
} }
/** /**
* *
* *
* @return * @return
*/ */
public String[] getOptions() { @Override
public String[] options() {
return options; return options;
} }
/** /**
* *
* *
* @return * @return
*/ */
public int getCorrectAnswer() { @Override
public int correctAnswer() {
return correctAnswer; return correctAnswer;
} }
/** /**
* *
* *
* @return * @return
*/ */
public String getExpression() { @Override
public String expression() {
return expression; return expression;
} }
} }

@ -14,8 +14,15 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
/**
*
*/
public class QuestionMaker { public class QuestionMaker {
private static final int max_attempts=100; private static final int MAX_ATTEMPTS = 100;
private static final String QUESTIONS_DIR = "questions";
private static final String FILE_SEPARATOR = File.separator;
private static final int OPTION_COUNT = 4;
private Student student; private Student student;
private final Random random; private final Random random;
@ -23,265 +30,377 @@ public class QuestionMaker {
this.random = new Random(); this.random = new Random();
} }
public List<Question> makeQuestions(Grade grade, /**
int sum, String email) { *
startStudent(grade,email); *
* @param grade
* @param sum
* @param email
* @return
*/
public List<Question> makeQuestions(Grade grade, int sum, String email) {
initializeStudent(grade, email);
List<Question> questions = new ArrayList<>(); List<Question> questions = new ArrayList<>();
Set<String> exQuestions=loadQuestions(email); Set<String> existingQuestions = loadQuestions(email);
Set<String> currQuestions=new HashSet<>(); Set<String> currentQuestions = new HashSet<>();
int attempt=0; int attempt = 0;
while(questions.size()<sum && attempt<max_attempts){
String expression=student.makeOneQuestion();
if(!exQuestions.contains(expression) &&
!currQuestions.contains(expression)) {
try {
double result = checkExpression(expression,grade);
Question question = createQuestion(expression, result);
questions.add(question);
exQuestions.add(expression);
currQuestions.add(expression);
} catch (Exception e) {
} while (questions.size() < sum && attempt < MAX_ATTEMPTS) {
String expression = student.makeOneQuestion();
if (isUniqueExpression(expression, existingQuestions, currentQuestions)) {
tryAddQuestion(expression, grade, questions, existingQuestions,
currentQuestions);
} }
attempt++; attempt++;
} }
saveQuestions(email,new ArrayList<>(currQuestions));
saveQuestions(email, new ArrayList<>(currentQuestions));
return questions; return questions;
} }
private void startStudent(Grade grade, String email) { private void initializeStudent(Grade grade, String email) {
String path="questions"+File.separator+email; String path = QUESTIONS_DIR + FILE_SEPARATOR + email;
switch(grade){ switch (grade) {
case primary: case primary:
student=new PrimaryMaker("primary","",path); student = new PrimaryMaker("primary", "", path);
break; break;
case middle: case middle:
student=new MiddleMaker("middle","",path); student = new MiddleMaker("middle", "", path);
break; break;
case high: case high:
student=new HighMaker("high","",path); student = new HighMaker("high", "", path);
break; break;
default: default:
throw new IllegalArgumentException("Unknown grade: " + grade);
} }
} }
private double checkExpression(String expression, Grade grade) { private boolean isUniqueExpression(String expression,
String expressions=expression.replace(" ",""); Set<String> existingQuestions, Set<String> currentQuestions) {
return !existingQuestions.contains(expression)
&& !currentQuestions.contains(expression);
}
private void tryAddQuestion(String expression, Grade grade,
List<Question> questions, Set<String> existingQuestions,
Set<String> currentQuestions) {
try {
double result = evaluateExpression(expression, grade);
Question question = createQuestion(expression, result);
questions.add(question);
existingQuestions.add(expression);
currentQuestions.add(expression);
} catch (Exception e) {
// Skip invalid expressions
}
}
private double evaluateExpression(String expression, Grade grade) {
String processedExpression = preprocessExpression(expression, grade);
return parseAndEvaluate(processedExpression);
}
private String preprocessExpression(String expression, Grade grade) {
String result = expression.replace(" ", "");
if (grade == Grade.middle) { if (grade == Grade.middle) {
// 初中:处理平方和平方根 result = preprocessMiddleSchoolExpression(result);
// 先处理括号内的平方:(5)^2 -> (5*5)
expressions = expressions.replaceAll("\\((\\d+)\\)\\^2", "($1*$1)");
// 处理普通数字的平方5^2 -> (5*5)
expressions = expressions.replaceAll("(\\d+)\\^2", "($1*$1)");
// 处理平方根√16 -> Math.sqrt(16)
expressions = expressions.replaceAll("√(\\d+)", "Math.sqrt($1)");
} else if (grade == Grade.high) { } else if (grade == Grade.high) {
// 高中:处理三角函数 result = preprocessHighSchoolExpression(result);
expressions = expressions.replaceAll("sin\\((\\d+)\\)", "Math.sin(Math.toRadians($1))");
expressions = expressions.replaceAll("cos\\((\\d+)\\)", "Math.cos(Math.toRadians($1))");
expressions = expressions.replaceAll("tan\\((\\d+)\\)", "Math.tan(Math.toRadians($1))");
} }
return evaluation(expressions);
return result;
} }
private double evaluation(String expressions) { private String preprocessMiddleSchoolExpression(String expression) {
return new Object(){ // Handle squares: (5)^2 -> (5*5) and 5^2 -> (5*5)
int pos=-1; String result = expression.replaceAll("\\((\\d+)\\)\\^2", "($1*$1)");
int ch; result = result.replaceAll("(\\d+)\\^2", "($1*$1)");
void nextChar() { // Handle square roots: √16 -> Math.sqrt(16)
ch = (++pos < expressions.length()) ? expressions.charAt(pos) : -1; result = result.replaceAll("√(\\d+)", "Math.sqrt($1)");
} return result;
}
boolean eat(int charToEat) { private String preprocessHighSchoolExpression(String expression) {
while (ch == ' ') { String result = expression.replaceAll("sin\\((\\d+)\\)",
nextChar(); "Math.sin(Math.toRadians($1))");
} result = result.replaceAll("cos\\((\\d+)\\)",
if (ch == charToEat) { "Math.cos(Math.toRadians($1))");
nextChar(); result = result.replaceAll("tan\\((\\d+)\\)",
return true; "Math.tan(Math.toRadians($1))");
} return result;
return false; }
private double parseAndEvaluate(String expression) {
return new ExpressionParser(expression).parse();
}
private static class ExpressionParser {
private final String expression;
private int pos = -1;
private int ch;
ExpressionParser(String expression) {
this.expression = expression;
}
double parse() {
nextChar();
double result = parseExpression();
if (pos < expression.length()) {
throw new RuntimeException("Unexpected: " + (char) ch);
} }
return result;
}
double parse() { private void nextChar() {
ch = (++pos < expression.length()) ? expression.charAt(pos) : -1;
}
private boolean eat(int charToEat) {
while (ch == ' ') {
nextChar(); nextChar();
double x = parseExpression();
if (pos < expressions.length()) {
throw new RuntimeException("Unexpected: " + (char) ch);
}
return x;
} }
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parseExpression() { private double parseExpression() {
double x = parseTerm(); double x = parseTerm();
for (;;) { while (true) {
if (eat('+')) { if (eat('+')) {
x += parseTerm(); x += parseTerm();
} else if (eat('-')) { } else if (eat('-')) {
x -= parseTerm(); x -= parseTerm();
} else { } else {
return x; return x;
}
} }
} }
}
double parseTerm() { private double parseTerm() {
double x = parseFactor(); double x = parseFactor();
for (;;) { while (true) {
if (eat('*')) { if (eat('*')) {
x *= parseFactor(); x *= parseFactor();
} else if (eat('/')) { } else if (eat('/')) {
x /= parseFactor(); x /= parseFactor();
} else { } else {
return x; return x;
}
} }
} }
}
double parseFactor() { private double parseFactor() {
if (eat('+')) { if (eat('+')) {
return parseFactor(); return parseFactor();
} }
if (eat('-')) { if (eat('-')) {
return -parseFactor(); return -parseFactor();
} }
double x; int startPos = pos;
int startPos = this.pos;
if (eat('(')) { if (eat('(')) {
x = parseExpression(); return parseParenthesizedExpression();
eat(')'); } else if (isDigit()) {
} else if ((ch >= '0' && ch <= '9') || ch == '.') { return parseNumber(startPos);
while ((ch >= '0' && ch <= '9') || ch == '.') { } else if (isUpperCaseLetter()) {
nextChar(); return parseMathFunction(startPos);
} } else if (isLowerCaseLetter()) {
x = Double.parseDouble(expressions.substring(startPos, this.pos)); return parseCustomFunction(startPos);
} else if (ch >= 'A' && ch <= 'Z') { } else {
// 处理 Math.xxx 函数 throw new RuntimeException("Unexpected: " + (char) ch);
while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '.') { }
nextChar(); }
}
String func = expressions.substring(startPos, this.pos); private boolean isDigit() {
eat('('); return (ch >= '0' && ch <= '9') || ch == '.';
x = parseExpression(); }
eat(')');
private boolean isUpperCaseLetter() {
if (func.equals("Math.sqrt")) { return ch >= 'A' && ch <= 'Z';
x = Math.sqrt(x); }
} else if (func.equals("Math.sin")) {
x = Math.sin(x);
} else if (func.equals("Math.cos")) {
x = Math.cos(x);
} else if (func.equals("Math.tan")) {
x = Math.tan(x);
} else if (func.equals("Math.toRadians")) {
x = Math.toRadians(x);
} else {
throw new RuntimeException("Unknown function: " + func);
}
} else if (ch >= 'a' && ch <= 'z') {
while (ch >= 'a' && ch <= 'z') {
nextChar();
}
String function = expressions.substring(startPos, this.pos);
x = parseFactor();
if (function.equals("sqrt")) {
x = Math.sqrt(x);
} else if (function.equals("sin")) {
x = Math.sin(x);
} else if (function.equals("cos")) {
x = Math.cos(x);
} else if (function.equals("tan")) {
x = Math.tan(x);
} else {
throw new RuntimeException("Unknown function: " + function);
}
} else {
throw new RuntimeException("Unexpected: " + (char) ch);
}
return x; private boolean isLowerCaseLetter() {
return ch >= 'a' && ch <= 'z';
}
private double parseParenthesizedExpression() {
double x = parseExpression();
eat(')');
return x;
}
private double parseNumber(int startPos) {
while (isDigit()) {
nextChar();
}
return Double.parseDouble(expression.substring(startPos, pos));
}
private double parseMathFunction(int startPos) {
while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '.') {
nextChar();
}
String func = expression.substring(startPos, pos);
eat('(');
double x = parseExpression();
eat(')');
return applyMathFunction(func, x);
}
private double parseCustomFunction(int startPos) {
while (ch >= 'a' && ch <= 'z') {
nextChar();
}
String function = expression.substring(startPos, pos);
double x = parseFactor();
return applyCustomFunction(function, x);
}
private double applyMathFunction(String func, double value) {
switch (func) {
case "Math.sqrt":
return Math.sqrt(value);
case "Math.sin":
return Math.sin(value);
case "Math.cos":
return Math.cos(value);
case "Math.tan":
return Math.tan(value);
case "Math.toRadians":
return Math.toRadians(value);
default:
throw new RuntimeException("Unknown function: " + func);
}
}
private double applyCustomFunction(String function, double value) {
switch (function) {
case "sqrt":
return Math.sqrt(value);
case "sin":
return Math.sin(value);
case "cos":
return Math.cos(value);
case "tan":
return Math.tan(value);
default:
throw new RuntimeException("Unknown function: " + function);
} }
}.parse(); }
} }
private Question createQuestion(String expression, double result) { private Question createQuestion(String expression, double result) {
String text="计算: "+expression+" = ? "; String text = "计算: " + expression + " = ? ";
double[] options=new double[4]; double[] options = generateOptions(result);
int currIndex=random.nextInt(4); int correctIndex = random.nextInt(OPTION_COUNT);
options[correctIndex] = result;
String[] optionStrings = formatOptions(options);
return new Question(expression, text, optionStrings, correctIndex);
}
options[currIndex]=result; private double[] generateOptions(double correctAnswer) {
double[] options = new double[OPTION_COUNT];
for(int i=0;i<4;i++) { for (int i = 0; i < OPTION_COUNT; i++) {
if(i!=currIndex) { double offset = random.nextDouble() * 20 - 10;
double offset=(random.nextDouble()*20-10); if (Math.abs(offset) < 1) {
if(Math.abs(offset)<1) { offset = random.nextBoolean() ? 5 : -5;
offset=random.nextBoolean() ? 5 : -5 ;
}
options[i]=offset+result;
} }
options[i] = offset + correctAnswer;
} }
String[] optionStrings=new String[4]; return options;
for(int i=0;i<4;i++) { }
optionStrings[i]=String.format("%c. %.2f",
private String[] formatOptions(double[] options) {
String[] optionStrings = new String[OPTION_COUNT];
for (int i = 0; i < OPTION_COUNT; i++) {
optionStrings[i] = String.format("%c. %.2f",
(char) ('A' + i), options[i]); (char) ('A' + i), options[i]);
} }
return optionStrings;
return new Question(expression, text, optionStrings, currIndex);
} }
private Set<String> loadQuestions(String email) { private Set<String> loadQuestions(String email) {
Set<String> questions=new HashSet<>(); Set<String> questions = new HashSet<>();
File userDir = new File("questions" + File.separator + email); File userDir = getUserDirectory(email);
if(!userDir.exists()||!userDir.isDirectory()) { if (!userDir.exists() || !userDir.isDirectory()) {
return questions; return questions;
} }
File[] files = userDir.listFiles((dir, name) -> name.endsWith(".txt")); File[] files = userDir.listFiles((dir, name) -> name.endsWith(".txt"));
if(files==null) { if (files == null) {
return questions; return questions;
} }
for(File file:files) { for (File file : files) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) { loadQuestionsFromFile(file, questions);
String line; }
while ((line = reader.readLine()) != null) {
line = line.trim(); return questions;
if (line.matches("\\d+\\.\\s+.*")) { }
String question = line.substring(line.indexOf('.') + 1).trim();
questions.add(question); private void loadQuestionsFromFile(File file, Set<String> questions) {
} try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String question = extractQuestion(line);
if (question != null) {
questions.add(question);
} }
} catch (IOException e) {
System.err.println("读取文件失败: " + e.getMessage());
} }
} catch (IOException e) {
System.err.println("读取文件失败: " + e.getMessage());
} }
return questions; }
private String extractQuestion(String line) {
line = line.trim();
if (line.matches("\\d+\\.\\s+.*")) {
return line.substring(line.indexOf('.') + 1).trim();
}
return null;
} }
private void saveQuestions(String email, List<String> questions) { private void saveQuestions(String email, List<String> questions) {
File userDir = new File("questions" + File.separator + email); File userDir = getUserDirectory(email);
if(!userDir.exists()) { ensureDirectoryExists(userDir);
if(!userDir.mkdirs()) {
String fileName = generateFileName();
File file = new File(userDir, fileName);
writeQuestionsToFile(file, questions);
}
private File getUserDirectory(String email) {
return new File(QUESTIONS_DIR + FILE_SEPARATOR + email);
}
private void ensureDirectoryExists(File directory) {
if (!directory.exists()) {
if (!directory.mkdirs()) {
System.err.println("创建目录失败"); System.err.println("创建目录失败");
return;
} }
} }
}
private String generateFileName() {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
String fileName = now.format( return now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")) + ".txt";
DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")) + ".txt"; }
File file = new File(userDir, fileName);
private void writeQuestionsToFile(File file, List<String> questions) {
try (PrintWriter writer = new PrintWriter(new FileWriter(file))) { try (PrintWriter writer = new PrintWriter(new FileWriter(file))) {
for (int i = 0; i < questions.size(); i++) { for (int i = 0; i < questions.size(); i++) {
writer.println((i + 1) + ". " + questions.get(i)); writer.println((i + 1) + ". " + questions.get(i));

@ -145,11 +145,11 @@ public class QuizFrame extends JFrame {
progressLabel.setText(String.format("第 %d/%d 题", progressLabel.setText(String.format("第 %d/%d 题",
currentIndex + 1, questions.size())); currentIndex + 1, questions.size()));
questionLabel.setText("<html><body style='width: 500px'>" + questionLabel.setText("<html><body style='width: 500px'>" +
question.getQuestionText() + "</body></html>"); question.questionText() + "</body></html>");
optionGroup.clearSelection(); optionGroup.clearSelection();
String[] options = question.getOptions(); String[] options = question.options();
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
optionButtons[i].setText(options[i]); optionButtons[i].setText(options[i]);
} }
@ -178,7 +178,7 @@ public class QuizFrame extends JFrame {
} }
Question question = questions.get(currentIndex); Question question = questions.get(currentIndex);
if (selectedIndex == question.getCorrectAnswer()) { if (selectedIndex == question.correctAnswer()) {
correctCount++; correctCount++;
} }

@ -34,10 +34,10 @@ public class RegisterDialog extends JDialog {
Pattern.compile("^[A-Za-z0-9+_.-]+@qq\\.com$"); Pattern.compile("^[A-Za-z0-9+_.-]+@qq\\.com$");
// 邮件配置 - 请根据实际情况修改 // 邮件配置 - 请根据实际情况修改
private static final String SMTP_HOST = "smtp.qq.com"; // QQ邮箱SMTP服务器 private static final String SMTP_HOST = "smtp.qq.com"; // SMTP服务器
private static final String SMTP_PORT = "587"; // 或使用465(SSL) private static final String SMTP_PORT = "587";
private static final String FROM_EMAIL = "3551664030@qq.com"; // 发件人邮箱 private static final String FROM_EMAIL = "3551664030@qq.com"; // 发件人邮箱
private static final String EMAIL_PASSWORD = "wvazqphcxhuqdbfa"; // 授权码(非登录密码) private static final String EMAIL_PASSWORD = "wvazqphcxhuqdbfa"; // 授权码
private final JTextField emailField; private final JTextField emailField;
private final JTextField codeField; private final JTextField codeField;
@ -180,9 +180,6 @@ public class RegisterDialog extends JDialog {
props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "true"); props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true"); // 启用TLS props.put("mail.smtp.starttls.enable", "true"); // 启用TLS
// 如果使用SSL端口465取消下面两行注释
// props.put("mail.smtp.ssl.enable", "true");
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// 创建会话 // 创建会话
Session session = Session.getInstance(props, new Authenticator() { Session session = Session.getInstance(props, new Authenticator() {

@ -1,24 +0,0 @@
ui\ChangePasswordDialog.class
ui\UserSettingsDialog.class
ui\QuizFrame.class
ui\RegisterDialog$1.class
model\QuestionMaker.class
model\UserManager.class
ui\GradeSelectionFrame$1.class
ui\ResultFrame.class
model\UserManager$1.class
model\Grade.class
ui\GradeSelectionFrame.class
model\PrimaryMaker.class
model\UserManager$UserInfo.class
ui\PasswordSetupDialog.class
model\HighMaker.class
model\QuestionMaker$1.class
ui\RegisterDialog.class
model\Student.class
ui\LoginFrame.class
ui\MathLearningApp.class
model\Question.class
model\MiddleMaker.class
model\QuestionMaker$2.class
ui\RegisterDialog$2.class

@ -1,17 +0,0 @@
E:\VSCode\Partner-Project\Math\src\model\MiddleMaker.java
E:\VSCode\Partner-Project\Math\src\model\UserManager.java
E:\VSCode\Partner-Project\Math\src\ui\RegisterDialog.java
E:\VSCode\Partner-Project\Math\src\ui\ResultFrame.java
E:\VSCode\Partner-Project\Math\src\model\Question.java
E:\VSCode\Partner-Project\Math\src\model\Student.java
E:\VSCode\Partner-Project\Math\src\model\QuestionMaker.java
E:\VSCode\Partner-Project\Math\src\model\PrimaryMaker.java
E:\VSCode\Partner-Project\Math\src\ui\PasswordSetupDialog.java
E:\VSCode\Partner-Project\Math\src\ui\ChangePasswordDialog.java
E:\VSCode\Partner-Project\Math\src\ui\MathLearningApp.java
E:\VSCode\Partner-Project\Math\src\ui\GradeSelectionFrame.java
E:\VSCode\Partner-Project\Math\src\ui\LoginFrame.java
E:\VSCode\Partner-Project\Math\src\ui\QuizFrame.java
E:\VSCode\Partner-Project\Math\src\model\Grade.java
E:\VSCode\Partner-Project\Math\src\model\HighMaker.java
E:\VSCode\Partner-Project\Math\src\ui\UserSettingsDialog.java

@ -1,14 +1,8 @@
{ {
"3551664030@qq.com": { "1617940512@qq.com": {
"email": "3551664030@qq.com", "email": "1617940512@qq.com",
"password": "123456Ab", "password": "Qwerty1",
"grade": "primary", "grade": "primary",
"username": "violet" "username": "zhang"
},
"2904255373@qq.com": {
"email": "2904255373@qq.com",
"password": "123456Ab",
"grade": "primary",
"username": "Violet"
} }
} }
Loading…
Cancel
Save