final3 代码规范检查完毕

pull/1/head
杨博文 5 days ago
parent 34c2af894e
commit 10b8903d2d

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="CheckStyle-IDEA-Module" serialisationVersion="2">
<option name="activeLocationsIds" />
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">

@ -2,10 +2,28 @@ package mathpuzzle;
import mathpuzzle.controller.StartController;
/**
*
*
* <p>main
*
*
* @author
* @version 1.0
* @since 2025
*/
public class Main {
/**
*
*
* <p>start
*
*
* @param args 使
*/
public static void main(String[] args) {
StartController startController = new StartController();
startController.start();
}
}
}

@ -13,12 +13,32 @@ import mathpuzzle.service.QuestionGenerator;
import mathpuzzle.service.SeniorHighGenerator;
import mathpuzzle.system.LogSystem;
/**
*
*
* <p>
*
* @author
* @version 1.0
* @since 2025
*/
public class StartController {
/** 日志系统,用于用户登录和管理。 */
private LogSystem logSystem = new LogSystem();
/** 去重器,用于避免生成重复的数学题目。 */
private QuestionDeduplicator deduplicator = new QuestionDeduplicator();
/** 文件处理器,用于保存生成的题目试卷。 */
private FileHandler fileHandler = new FileHandler();
/**
*
*
* <p>
*
*/
public void start() {
logSystem.userHashMapInit();
@ -52,6 +72,15 @@ public class StartController {
}
}
/**
*
*
* <p>"切换为XX"
*
*
* @param user
* @param input
*/
private void handleLevelSwitch(User user, String input) {
if (input.startsWith("切换为")) {
String newLevel = input.substring(3);
@ -65,6 +94,14 @@ public class StartController {
}
}
/**
*
*
* <p>
*
* @param level "小学""初中""高中"
* @return null
*/
private QuestionGenerator createGenerator(String level) {
switch (level) {
case "小学":
@ -78,6 +115,16 @@ public class StartController {
}
}
/**
*
*
* <p>使
*
*
* @param user
* @param count
* @throws IOException
*/
// 处理题目生成,进行去重工作
private void handleQuestionGeneration(User user, int count) throws IOException {
QuestionGenerator generator = createGenerator(user.getLevel());
@ -104,8 +151,8 @@ public class StartController {
String newQuestion = tempQuestions.get(0);
// 移除末尾的 " =" 以便查重更准确(可选,取决于你如何存储历史题)
String questionForDedup = newQuestion.endsWith(" =") ?
newQuestion.substring(0, newQuestion.length() - 2) : newQuestion;
String questionForDedup = newQuestion.endsWith(" =")
? newQuestion.substring(0, newQuestion.length() - 2) : newQuestion;
if (!deduplicator.isDuplicate(questionForDedup)) {
// 题目不重复,加入最终列表和查重集
@ -130,4 +177,4 @@ public class StartController {
fileHandler.savePaper(user, finalQuestions);
System.out.println("试卷已成功保存!");
}
}
}

@ -1,30 +1,72 @@
package mathpuzzle.entity;
/**
*
*
* <p>
*
*
* @author
* @version 1.0
* @since 2025
*/
public class User {
/** 用户名,不可修改。 */
private final String name;
/** 用户密码,不可修改。 */
private final String password;
/** 用户当前的学习级别,可以修改。 */
private String level;
/**
*
*
* @param name
* @param password
* @param level "小学""初中""高中"
*/
public User(String name, String password, String level) {
this.name = name;
this.password = password;
this.level = level;
}
/**
*
*
* @return
*/
public String getName() {
return name;
}
/**
*
*
* @return
*/
public String getPassword() {
return password;
}
/**
*
*
* @return
*/
public String getLevel() {
return level;
}
/**
*
*
* @param newLevel "小学""初中""高中"
*/
public void setLevel(String newLevel) {
level = newLevel;
}
}
}

@ -7,12 +7,35 @@ import java.util.List;
import java.util.Map;
import java.util.Stack;
/**
*
*
* <p>使
*
*
* @author
* @version 1.0
* @since 2025
*/
public class CaculatePrimary {
/** 数字栈,用于存储操作数。 */
private final Stack<Double> numStack = new Stack<>();
/** 操作符栈,用于存储运算符和括号。 */
private final Stack<String> opStack = new Stack<>();
/** 运算符优先级映射表,数字越大优先级越高。 */
private final Map<String, Integer> precedence = new HashMap<>();
/**
*
*
* <p>使
*
* @param expression
* @return
*/
public double caculate(List<String> expression) {
precedence.put("+", 1);
precedence.put("-", 1);
@ -42,6 +65,12 @@ public class CaculatePrimary {
return numStack.pop();
}
/**
*
*
* <p>
*
*/
public void doCaculte() {
String op = opStack.pop();
double num2 = numStack.pop();
@ -63,5 +92,4 @@ public class CaculatePrimary {
break;
}
}
}
}

@ -1,6 +1,6 @@
package mathpuzzle.service;
import mathpuzzle.entity.User;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
@ -10,12 +10,29 @@ import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import mathpuzzle.entity.User;
/**
*
*
*
* <p>
*
*
* @author
* @version 1.0
* @since 2025
*/
public class FileHandler {
/**
*
*
* <p>
*
*
* @param user
* @throws IOException
*/
public void ensureUserDirectory(User user) throws IOException {
String dirPath = "./" + user.getName();
Path path = Paths.get(dirPath);
@ -24,6 +41,16 @@ public class FileHandler {
}
}
/**
*
*
* <p>
*
*
* @param user
* @param questions
* @throws IOException
*/
public void savePaper(User user, List<String> questions) throws IOException {
ensureUserDirectory(user);
// 生成文件名:年-月-日-时-分-秒.txt
@ -39,4 +66,4 @@ public class FileHandler {
}
}
}
}
}

@ -7,12 +7,30 @@ import java.util.List;
import java.util.Random;
/**
*
*
*
* <p>
*
*
* @author
* @version 1.0
* @since 2025
*/
public class JuniorHighGenerator implements QuestionGenerator {
/**
* "平方""开根号"
*/
private static final String[] ADVANCED_OPS = {"平方", "开根号"};
/**
*
*/
private static final String[] OPERATORS = {"+", "-", "*", "/"};
/**
*
*/
private final Random random = new Random();
@Override
@ -25,6 +43,14 @@ public class JuniorHighGenerator implements QuestionGenerator {
return questions;
}
/**
*
*
* <p>
*
*
* @return
*/
private String generateSingleQuestion() {
List<String> parts = new ArrayList<>();
int operandCount = random.nextInt(5) + 1;
@ -46,9 +72,7 @@ public class JuniorHighGenerator implements QuestionGenerator {
// 随机数看取出来的是不是开根号运算符
if ("开根号".equals(ADVANCED_OPS[random.nextInt(ADVANCED_OPS.length)])) {
parts = generateRoot(parts, i);
}
// 如果不是开根号就是平方运算
else {
} else { // 如果不是开根号就是平方运算
parts = generateSquare(parts, i);
}
hasAdvancedOp = true;
@ -63,7 +87,15 @@ public class JuniorHighGenerator implements QuestionGenerator {
return String.join(" ", parts) + " =";
}
// 产生基本操作
/**
*
*
* <p>
*
* @param operandCount
* @param parts
* @return
*/
public List<String> generateBase(int operandCount, List<String> parts) {
for (int i = 0; i < operandCount; i++) {
int num = random.nextInt(100) + 1;
@ -75,7 +107,15 @@ public class JuniorHighGenerator implements QuestionGenerator {
return parts;
}
// 强制加入一个高级运算符
/**
*
*
* <p>使
*
*
* @param parts
* @return
*/
public List<String> forceAddAdvancedOp(List<String> parts) {
String advancedOp = ADVANCED_OPS[random.nextInt(ADVANCED_OPS.length)];
if ("平方".equals(advancedOp)) {
@ -87,6 +127,16 @@ public class JuniorHighGenerator implements QuestionGenerator {
return parts;
}
/**
*
*
* <p>
*
*
* @param parts
* @param i
* @return
*/
public List<String> generateRoot(List<String> parts, int i) {
if (random.nextBoolean()) {
parts.set(i, "开根号(" + parts.get(i) + ")");
@ -108,6 +158,15 @@ public class JuniorHighGenerator implements QuestionGenerator {
return parts;
}
/**
*
*
* <p>
*
* @param parts
* @param i
* @return
*/
public List<String> generateSquare(List<String> parts, int i) {
parts.set(i, "(" + parts.get(i));
// 为避免随机数上限出现0此处要单独判断一下左括号正好括住倒数第二个操作数的情况
@ -124,4 +183,4 @@ public class JuniorHighGenerator implements QuestionGenerator {
}
return parts;
}
}
}

@ -5,11 +5,21 @@ import java.util.List;
import java.util.Random;
/**
* +, -, *, / ()
*
*
* <p>
*
*
* @author
* @version 1.0
* @since 2025
*/
public class PrimarySchoolGenerator implements QuestionGenerator {
/** 运算符数组,包含四则运算符号。 */
private static final String[] OPERATORS = {"+", "-", "*", "/"};
/** 随机数生成器,用于生成随机题目。 */
private final Random random = new Random();
@Override
@ -22,6 +32,14 @@ public class PrimarySchoolGenerator implements QuestionGenerator {
return questions;
}
/**
* .
*
* <p>2-5
*
*
* @return
*/
private String generateSingleQuestion() {
CaculatePrimary caculatePrimary = new CaculatePrimary();
int operandCount = random.nextInt(4) + 2; // 2-5个操作数
@ -61,6 +79,14 @@ public class PrimarySchoolGenerator implements QuestionGenerator {
}
}
/**
*
*
* <p>
*
* @param str
* @return truefalse
*/
public static boolean isNumeric(String str) {
if (str == null || str.isEmpty()) {
return false;
@ -73,6 +99,15 @@ public class PrimarySchoolGenerator implements QuestionGenerator {
}
}
/**
*
*
* <p>
*
* @param operandCount
* @param parts
* @return
*/
public List<String> generateBase(int operandCount, List<String> parts) {
for (int i = 0; i < operandCount; i++) {
int num = random.nextInt(100) + 1;
@ -83,6 +118,4 @@ public class PrimarySchoolGenerator implements QuestionGenerator {
}
return parts;
}
}

@ -4,17 +4,35 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import mathpuzzle.entity.User;
import java.util.HashSet;
import java.util.Set;
import mathpuzzle.entity.User;
/**
*
*
*
* <p>
*
*
* @author
* @version 1.0
* @since 2025
*/
public class QuestionDeduplicator {
/**
*
*/
private final Set<String> existingQuestions = new HashSet<>();
/**
*
*
* <p>.txt
*
*
* @param user
*/
public void loadExistingQuestions(User user) {
existingQuestions.clear();
String userDir = "./" + user.getName();
@ -25,9 +43,9 @@ public class QuestionDeduplicator {
}
File[] files = dir.listFiles((d, name) -> name.endsWith(".txt"));
if (files == null) {
return;
}
if (files == null) {
return;
}
for (File file : files) {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
@ -47,11 +65,27 @@ public class QuestionDeduplicator {
}
}
/**
*
*
* <p>
*
* @param question
* @return truefalse
*/
public boolean isDuplicate(String question) {
return existingQuestions.contains(question);
}
/**
*
*
* <p>
*
*
* @param question
*/
public void addQuestion(String question) {
existingQuestions.add(question);
}
}
}

@ -2,7 +2,27 @@ package mathpuzzle.service;
import java.util.List;
/**
*
*
* <p>
*
*
*
* @author
* @version 1.0
* @since 2025
*/
public interface QuestionGenerator {
/**
*
*
* <p>
*
*
* @param count
* @return
*/
List<String> generateQuestions(int count);
}
}

@ -7,12 +7,24 @@ import java.util.List;
import java.util.Random;
/**
* sin, cos, tan
*
*
* <p>sincostan
*
*
* @author
* @version 1.0
* @since 2025
*/
public class SeniorHighGenerator implements QuestionGenerator {
/** 三角函数运算符数组,包含"sin"、"cos"和"tan"。 */
private static final String[] TRIG_FUNCS = {"sin", "cos", "tan"};
/** 基本运算符数组,包含四则运算符号。 */
private static final String[] OPERATORS = {"+", "-", "*", "/"};
/** 随机数生成器,用于生成随机题目。 */
private final Random random = new Random();
@Override
@ -25,6 +37,14 @@ public class SeniorHighGenerator implements QuestionGenerator {
return questions;
}
/**
*
*
* <p>
*
*
* @return
*/
private String generateSingleQuestion() {
List<String> parts = new ArrayList<>();
int operandCount = random.nextInt(5) + 1;
@ -41,7 +61,7 @@ public class SeniorHighGenerator implements QuestionGenerator {
if (i == parts.size() - 1) {
advancedOp = TRIG_FUNCS[random.nextInt(TRIG_FUNCS.length)];
parts.set(i, advancedOp + parts.get(i));
} else if (isNumeric(parts.get(i)) && random.nextBoolean()) {// 随机数看是否为操作数且随即进入生成程序
} else if (isNumeric(parts.get(i)) && random.nextBoolean()) { // 随机数看是否为操作数且随即进入生成程序
// 进入随机生成tan\sin\cos的程序
parts = generateTrig(parts, i);
break;
@ -51,6 +71,15 @@ public class SeniorHighGenerator implements QuestionGenerator {
return String.join(" ", parts) + " =";
}
/**
*
*
* <p>
*
* @param operandCount
* @param parts
* @return
*/
// 产生基本操作
public List<String> generateBase(int operandCount, List<String> parts) {
for (int i = 0; i < operandCount; i++) {
@ -63,6 +92,16 @@ public class SeniorHighGenerator implements QuestionGenerator {
return parts;
}
/**
*
*
* <p>
*
*
* @param parts
* @param i
* @return
*/
// 产生三角函数运算符
public List<String> generateTrig(List<String> parts, int i) {
String trigOp = TRIG_FUNCS[random.nextInt(TRIG_FUNCS.length)];
@ -85,4 +124,4 @@ public class SeniorHighGenerator implements QuestionGenerator {
}
return parts;
}
}
}
Loading…
Cancel
Save