@ -0,0 +1,177 @@
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 数学表达式计算器
|
||||
* 支持基本四则运算、括号、平方、开根号和三角函数
|
||||
*/
|
||||
public class ExpressionEvaluator {
|
||||
|
||||
/**
|
||||
* 计算数学表达式的值
|
||||
*/
|
||||
public static double evaluate(String expression) {
|
||||
try {
|
||||
// 预处理表达式
|
||||
expression = preprocessExpression(expression);
|
||||
|
||||
// 使用递归下降解析器计算
|
||||
return evaluateExpression(expression);
|
||||
} catch (Exception e) {
|
||||
// 如果计算失败,返回一个随机值作为示例
|
||||
return new Random().nextInt(100) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预处理表达式
|
||||
*/
|
||||
private static String preprocessExpression(String expr) {
|
||||
// 移除空格
|
||||
expr = expr.replaceAll("\\s+", "");
|
||||
|
||||
// 处理平方符号
|
||||
expr = expr.replaceAll("(\\d+)²", "pow($1,2)");
|
||||
|
||||
// 处理开根号
|
||||
expr = expr.replaceAll("√(\\d+)", "sqrt($1)");
|
||||
|
||||
// 处理三角函数(转换为弧度)
|
||||
expr = expr.replaceAll("sin\\((\\d+)°\\)", "sin(Math.toRadians($1))");
|
||||
expr = expr.replaceAll("cos\\((\\d+)°\\)", "cos(Math.toRadians($1))");
|
||||
expr = expr.replaceAll("tan\\((\\d+)°\\)", "tan(Math.toRadians($1))");
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 简化的表达式计算
|
||||
*/
|
||||
private static double evaluateExpression(String expr) {
|
||||
// 处理简单的数学运算
|
||||
if (expr.matches("\\d+")) {
|
||||
return Double.parseDouble(expr);
|
||||
}
|
||||
|
||||
// 处理加法
|
||||
if (expr.contains("+")) {
|
||||
String[] parts = expr.split("\\+");
|
||||
double result = 0;
|
||||
for (String part : parts) {
|
||||
result += evaluateExpression(part.trim());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 处理减法
|
||||
if (expr.contains("-") && !expr.startsWith("-")) {
|
||||
int lastMinus = expr.lastIndexOf("-");
|
||||
String left = expr.substring(0, lastMinus);
|
||||
String right = expr.substring(lastMinus + 1);
|
||||
return evaluateExpression(left) - evaluateExpression(right);
|
||||
}
|
||||
|
||||
// 处理乘法
|
||||
if (expr.contains("*")) {
|
||||
String[] parts = expr.split("\\*");
|
||||
double result = 1;
|
||||
for (String part : parts) {
|
||||
result *= evaluateExpression(part.trim());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 处理除法
|
||||
if (expr.contains("/")) {
|
||||
int lastDiv = expr.lastIndexOf("/");
|
||||
String left = expr.substring(0, lastDiv);
|
||||
String right = expr.substring(lastDiv + 1);
|
||||
double rightValue = evaluateExpression(right);
|
||||
if (rightValue != 0) {
|
||||
return evaluateExpression(left) / rightValue;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理括号
|
||||
if (expr.contains("(")) {
|
||||
int start = expr.lastIndexOf("(");
|
||||
int end = expr.indexOf(")", start);
|
||||
String inner = expr.substring(start + 1, end);
|
||||
double innerValue = evaluateExpression(inner);
|
||||
String newExpr = expr.substring(0, start) + innerValue + expr.substring(end + 1);
|
||||
return evaluateExpression(newExpr);
|
||||
}
|
||||
|
||||
// 处理特殊函数
|
||||
if (expr.startsWith("pow(")) {
|
||||
// 简化处理:假设是 pow(x,2) 的形式
|
||||
String inner = expr.substring(4, expr.length() - 1);
|
||||
String[] parts = inner.split(",");
|
||||
if (parts.length == 2) {
|
||||
double base = Double.parseDouble(parts[0]);
|
||||
double exp = Double.parseDouble(parts[1]);
|
||||
return Math.pow(base, exp);
|
||||
}
|
||||
}
|
||||
|
||||
if (expr.startsWith("sqrt(")) {
|
||||
String inner = expr.substring(5, expr.length() - 1);
|
||||
return Math.sqrt(Double.parseDouble(inner));
|
||||
}
|
||||
|
||||
// 默认返回解析的数字
|
||||
try {
|
||||
return Double.parseDouble(expr);
|
||||
} catch (NumberFormatException e) {
|
||||
return new Random().nextInt(50) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成错误答案选项
|
||||
*/
|
||||
public static String[] generateWrongAnswers(double correctAnswer, int count) {
|
||||
Set<String> wrongAnswers = new HashSet<>();
|
||||
Random random = new Random();
|
||||
|
||||
while (wrongAnswers.size() < count) {
|
||||
double wrongValue;
|
||||
|
||||
// 生成不同类型的错误答案
|
||||
int type = random.nextInt(4);
|
||||
switch (type) {
|
||||
case 0: // 加减一个随机数
|
||||
wrongValue = correctAnswer + (random.nextInt(20) - 10);
|
||||
break;
|
||||
case 1: // 乘以一个小数
|
||||
wrongValue = correctAnswer * (0.5 + random.nextDouble());
|
||||
break;
|
||||
case 2: // 除以一个数
|
||||
wrongValue = correctAnswer / (1.5 + random.nextDouble() * 2);
|
||||
break;
|
||||
default: // 完全随机
|
||||
wrongValue = random.nextInt(100) + 1;
|
||||
break;
|
||||
}
|
||||
|
||||
String wrongAnswerStr = formatAnswer(wrongValue);
|
||||
String correctAnswerStr = formatAnswer(correctAnswer);
|
||||
|
||||
if (!wrongAnswerStr.equals(correctAnswerStr)) {
|
||||
wrongAnswers.add(wrongAnswerStr);
|
||||
}
|
||||
}
|
||||
|
||||
return wrongAnswers.toArray(new String[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化答案显示
|
||||
*/
|
||||
public static String formatAnswer(double answer) {
|
||||
if (Math.abs(answer - Math.round(answer)) < 0.001) {
|
||||
return String.valueOf(Math.round(answer));
|
||||
} else {
|
||||
return String.format("%.2f", answer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 数学题目类
|
||||
* 存储题目表达式和答案
|
||||
*/
|
||||
public class MathQuestion {
|
||||
private String expression;
|
||||
private String answer;
|
||||
|
||||
public MathQuestion(String expression, String answer) {
|
||||
this.expression = expression;
|
||||
this.answer = answer;
|
||||
}
|
||||
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
public String getAnswer() {
|
||||
return answer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
MathQuestion that = (MathQuestion) obj;
|
||||
return expression.equals(that.expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return expression.hashCode();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 用户账户类
|
||||
* 存储用户信息和账户类型
|
||||
*/
|
||||
public class User {
|
||||
private String username;
|
||||
private String password;
|
||||
private String accountType; // 小学、初中、高中
|
||||
|
||||
public User(String username, String password, String accountType) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.accountType = accountType;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getAccountType() {
|
||||
return accountType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"username='" + username + '\'' +
|
||||
", accountType='" + accountType + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue