Related process screenshots
Screenshot of infix to suffix and suffix calculation test
Screenshot of topic generation
Screenshot of calculation accuracy
Key code explanation
Convert infix expression to postfix expression and evaluate postfix expression
import java.util.Stack;
import java.util.regex.Pattern;
public class StringToArithmetic {
public StringToArithmetic() {
}
// 将中缀表达式字符串计算得到结果
public static double stringToArithmetic(String string) {
return suffixToArithmetic(infixToSuffix(string));
}
// 将中缀表达式转换为后缀表达式
public static String infixToSuffix(String exp) {
Stack<Character> s = new Stack<Character>();
// 要输出的后缀表达式字符串
String suffix = "";
int length = exp.length(); // 输入的中缀表达式的长度
for (int i = 0; i < length; i++) {
char temp;
char ch = exp.charAt(i);
switch (ch) {
case ' ':
break;
case '(':
s.push(ch);
break;
case '+':
case '-':
while (s.size() != 0) {
temp = s.pop();
if (temp == '(') {
s.push('(');
break;
}
suffix += temp;
}
s.push(ch);
break;
case '×':
case '÷':
while (s.size() != 0) {
temp = s.pop();
if (temp == '+' || temp == '-' || temp == '(') {
s.push(temp);
break;
} else {
suffix += temp;
}
}
s.push(ch);
break;
case ')':
while (!s.isEmpty()) {
temp = s.pop();
if (temp == '(') {
break;
} else {
suffix += temp;
}
}
break;
default:
suffix += ch;
break;
}
}
while (s.size() != 0) {
suffix += s.pop();
}
return suffix;
}
public static double suffixToArithmetic(String exp) {
Pattern pattern = Pattern.compile("\\d+||(\\d+\\.\\d+)");
String[] strings = exp.split("");
Stack<Double> stack = new Stack<Double>();
for (int i = 0; i < strings.length; i++) {
if (strings[i].equals("")) {
continue;
}
if (pattern.matcher(strings[i]).matches()) {
stack.push(Double.parseDouble(strings[i]));
}
else {
double y = stack.pop();
double x = stack.pop();
stack.push(calculate(x, y, strings[i]));
}
}
return stack.pop();
}
private static Double calculate(double x, double y, String string) {
// TODO Auto-generated method stub
if (string.trim().equals("+")) {
return x + y;
}
if (string.trim().equals("-")) {
return x - y;
}
if (string.trim().equals("×")) {
return x * y;
}
if (string.trim().equals("÷")) {
return x / y;
}
return (double) 0;
}
Create topic class
public class createquestions {
public static String create(int level){
String add = "+", sub = "-", multi = "×", div = "÷";
String result="";
int r1=(int)(Math.random()*9+1);
int r2=(int)(Math.random()*9+1);
int r3=(int)(Math.random()*9+1);
//三个整数
int fenzi1=(int)(Math.random()*9+1);
int fenmu1=(int)(Math.random()*9+1);
String fenshu1=fenzi1+"/"+fenmu1;
int fenzi2=(int)(Math.random()*9+1);
int fenmu2=(int)(Math.random()*9+1);
String fenshu2=fenzi2+"/"+fenmu2;
int fenzi3=(int)(Math.random()*9+1);
int fenmu3=(int)(Math.random()*9+1);
String fenshu3=fenzi3+"/"+fenmu3;
//三个分数
int suiji1=(int)(Math.random()*4);
//第一个运算符
int suiji11=(int)(Math.random()*3);
//生成括号
int suiji2=(int)(Math.random()*4);
//第二个运算符
if (level>=1) {
if(suiji11==0&&level==2&&suiji1!=0&&suiji1!=1)
result+="(";
if (level==3)
result+=fenshu1;
else
result+=r1;
if (suiji1 == 0)
result+=add;
if (suiji1 == 1)
result+=sub;
if (suiji1==2)
result+=multi;
if(suiji1==3)
result+=div;
if(suiji11==1&&level==2&&suiji2!=0&&suiji2!=1)
result+="(";
if (level==3)
result+=fenshu2;
else
result+=r2;
if(suiji11==0&&level==2&&suiji1!=0&&suiji1!=1)
result+=")";
}
if (level>=2){
if (suiji2==0)
result+=add;
if (suiji2 == 1)
result+=sub;
if (suiji2==2)
result+=multi;
if(suiji2==3)
result+=div;
if (level==3)
result+=fenshu3;
else
result+=r3;
if(suiji11==1&&suiji2!=0&&suiji2!=1)
result+=")";
}
return result;
}
main class
import java.util.Scanner;
public class calculate {
public static void main(String[] args) {
System.out.println("输入你要生成的题数");
Scanner scan = new Scanner(System.in);
int tishu = scan.nextInt();
System.out.println("输入你要生成题目的等级(输入1或2或3)");
int dengji = scan.nextInt();
//调用一个循环
double answer;
int zhengquelv = 0;
double[] daan = new double[tishu];
switch (dengji) {
case 1:
for (int i = 0; i < tishu; i++) {
String a = createquestions.create(1);
String b = a + "=";
System.out.print(b);
answer = scan.nextDouble();
daan[i] = StringToArithmetic.stringToArithmetic(a);
if (answer == daan[i])
zhengquelv++;
}break;
case 2:
for (int i = 0; i < tishu; i++) {
String a = createquestions.create(2);
String b = a + "=";
System.out.print(b);
answer = scan.nextInt();
daan[i] = StringToArithmetic.stringToArithmetic(a);
if (answer == daan[i])
zhengquelv++;
}break;
case 3:
for (int i = 0; i < tishu; i++) {
String a = createquestions.create(3);
String b = a + "=";
System.out.print(b);
answer = scan.nextInt();
daan[i] = StringToArithmetic.stringToArithmetic(a);
if (answer == daan[i])
zhengquelv++;
}break;
}
if(dengji<1||dengji>3)
System.out.println("题目等级输入错误");
if (dengji >= 1&&dengji <= 3) {
System.out.println("答案为");
for (
double daanwei : daan)
System.out.print(daanwei + " ");
System.out.println("正确率为" + zhengquelv + "/" + tishu);
}
}
}
Difficulties encountered and solutions
There are some small problems about the correct rate of judging the
question
Write the part of the project that you are responsible for
Writing of infix-to-suffix and suffix calculation classes
Individual contribution division (divide the contribution of each member of the group by percentage, and the sum of the contributions of each member is 100%)
20172315 Hu Zhitao 30%
20172312 Peng Lin 30%
20172318 Lu Dayue 40%