四则运算练习器(基于控制台)

题目链接:http://www.cnblogs.com/HQL0301/p/7502315.htm

源码连接:https://gitee.com/a1234321/four_operations/tree/master

一.需求分析

⦁可以根据输入的题数和数值范围自动生成四则运算表达式。

⦁表达式包括“+ - * /”这四种运算符。

⦁可以进行分数的运算,如果题目中出现假分数,则转换成真分数。

⦁对题目答案进行批改对错,给出正确率。

二.功能设计

(1)基本功能

1.输入参数n,程序生成 n道四则运算题目.

2.用参数来控制生成的四则运算的数值范围.

3.用户可以对相应的题目进行做答.

4.对用户的答案进行判断对错,并给出正确率.

(2)扩展功能

1.查重复,不出现重复的题目(没有实现)

2.对假分数转换成带分数表示

(3)高级功能

1.用户回答正确可以获得相应的积分,错误会扣除相应的积分,积分可以兑换一定的礼品。(后续完善的时候再实现)

三.设计实现

使用java实现,基于控制台。根据所输入的长度,随机生成四则运算题目。

timu包是实现出题

calculate包是题目的做题业务实现

四.代码说明

  1 public class calculate {
  2 private Map> priorityMap = new HashMap>();
  3 private Stack optStack = new Stack();
  4 private Stack numStack = new Stack();
  5 
  6 public double calcualte1(String exp) {
  7 StringTokenizer vn = new StringTokenizer(exp);
  8 while (vn.hasMoreTokens()) {
  9 String token = vn.nextToken();
 10 transversion(token);
 11 }
 12 return numStack.pop();
 13 }
 14 public double calcualte(String exp) {
 15 char[] strExp = exp.toCharArray();
 16 for(char a: strExp){
 17 transversion(""+a+"");
 18 }
 19 return numStack.pop();
 20 }
 21 
 22 private void transversion(String token) {
 23 while (false == "=".equals(optStack.peek()) || false == token.equals("=")) {
 24 if (true == judgenum(token)) {
 25 numStack.push(Double.parseDouble(token));
 26 break;
 27 
 28 } else {
 29 String priority = priority(optStack.peek(), token);
 30 if ("<".equals(priority)) {
 31 optStack.push(token);
 32 break;
 33 } else if ("=".equals(priority)) {
 34 optStack.pop();
 35 break;
 36 } else {
 37 double res = calculate(optStack.pop(), numStack.pop(), numStack.pop());
 38 numStack.push(res);
 39 }
 40 }
 41 }
 42 }
 43 
 44 private double calculate(String opt, double a1, double a2) {
 45 if ("+".equals(opt)) {
 46 return a2 + a1;
 47 } else if ("-".equals(opt)) {
 48 return a2 - a1;
 49 } else if ("*".equals(opt)) {
 50 return a2 * a1;
 51 } else if ("/".equals(opt)) {
 52 return a2 / a1;
 53 } else {
 54 throw new RuntimeException("unsupported operator:" + opt);
 55 }
 56 }
 57 
 58 private boolean judgenum(String token) {
 59 int LEN = token.length();
 60 for (int ix = 0 ; ix < LEN ; ++ix) {
 61 char ch = token.charAt(ix);
 62 if (ch == '.') {
 63 continue;
 64 }
 65 if (false == judgenum(ch)) {
 66 return false;
 67 }
 68 }
 69 return true;
 70 }
 71 
 72 private boolean judgenum(char ch) {
 73 if (ch >= '0' && ch <= '9') {
 74 return true;
 75 }
 76 return false;
 77 }
 78 
 79 public String priority(String op1, String op2) {
 80 return priorityMap.get(op1).get(op2);
 81 }
 82 
 83 public calculate() {
 84 
 85 optStack.push("=");
 86 Map subMap = new HashMap();
 87 subMap.put("+", ">");
 88 subMap.put("-", ">");
 89 subMap.put("*", "<");
 90 subMap.put("/", "<");
 91 subMap.put("(", "<");
 92 subMap.put(")", ">");
 93 subMap.put("=", ">");
 94 priorityMap.put("+", subMap);
 95 // -
 96 subMap = new HashMap();
 97 subMap.put("+", ">");
 98 subMap.put("-", ">");
 99 subMap.put("*", "<");
100 subMap.put("/", "<");
101 subMap.put("(", "<");
102 subMap.put(")", ">");
103 subMap.put("=", ">");
104 priorityMap.put("-", subMap);
105 // *
106 subMap = new HashMap();
107 subMap.put("+", ">");
108 subMap.put("-", ">");
109 subMap.put("*", ">");
110 subMap.put("/", ">");
111 subMap.put("(", "<");
112 subMap.put(")", ">");
113 subMap.put("=", ">");
114 priorityMap.put("*", subMap);
115 // /
116 subMap = new HashMap();
117 subMap.put("+", ">");
118 subMap.put("-", ">");
119 subMap.put("*", ">");
120 subMap.put("/", ">");
121 subMap.put("(", "<");
122 subMap.put(")", ">");
123 subMap.put("=", ">");
124 priorityMap.put("/", subMap);
125 // (
126 subMap = new HashMap();
127 subMap.put("+", "<");
128 subMap.put("-", "<");
129 subMap.put("*", "<");
130 subMap.put("/", "<");
131 subMap.put("(", "<");
132 subMap.put(")", "=");
133 //subMap.put("#", ">");
134 priorityMap.put("(", subMap);
135 // )
136 subMap = new HashMap();
137 subMap.put("+", ">");
138 subMap.put("-", ">");
139 subMap.put("*", ">");
140 subMap.put("/", ">");
141 //subMap.put("(", "<");
142 subMap.put(")", ">");
143 subMap.put("=", ">");
144 priorityMap.put(")", subMap);
145 // #
146 subMap = new HashMap();
147 subMap.put("+", "<");
148 subMap.put("-", "<");
149 subMap.put("*", "<");
150 subMap.put("/", "<");
151 subMap.put("(", "<");
152 //subMap.put(")", ">");
153 subMap.put("=", "=");
154 priorityMap.put("=", subMap);
155 }
156 }

 

 

出题包:

public void generate(int n, int range) {
char[] symbol = { '+', '-', '*', '/', '(' };
int op; // 操作数
try { 
File writename = new File("D:\\test1.txt"); 
BufferedWriter out = new BufferedWriter(new FileWriter(writename)); 
for (int i = 0; i < n; i++) {

String eHead = "" + (i + 1) + ". ";
String e ="";
int exlength = (int) (Math.random() * 4 + 2);
int lbracket = 0;
int rbracket = 0;
int[] whe = new int[exlength]; 
for (int j = 0; j < exlength; j++) {
char s = symbol[(int) (Math.random() * 5)];
if (j == exlength - 1 && s == '(') { 
int count1 = lbracket;
if (lbracket == 0) {
e += "" + (int) (Math.random() * range) + "";
} else {
e += "" + (int) (Math.random() * range) + "";
for (int q = 0; q < count1; q++) {
e += ")";
lbracket--;
}
}
e += "=";
continue;
}
if (lbracket == 0) { 
if (s == '(') {
e = e + "" + s + "" + (int) (Math.random() * range)
+ "" + symbol[(int) (Math.random() * 4)] + "";

whe[rbracket] = (int) (Math.random()
* (exlength - j - 1) + j + 1);

lbracket++;
rbracket++;
} else {
e = e + "" + (int) (Math.random() * range) + "" + s
+ "";
}
} else if (s != '(') {
int count2 = lbracket;
int count_match = 0;
for (int l = 0; l < count2; l++) {
if (whe[(rbracket-count2) + l ] == j) {
if (count_match == 0) {
e = e + "" + (int) (Math.random() * range)
+ ")";
count_match++;
lbracket--;
} else {
e += ')';
lbracket--;
}
}
}
if (count_match == 0) {
e = e + "" + (int) (Math.random() * range) + "" + s
+ "";
} else {
e += "" + s + "";
}
} else {
int count3 = lbracket;
int count = 0;
for (int m = 0; m < count3; m++) {

if (whe[rbracket-count3 + m] == j) {
if (count == 0) {
e = e + "" + (int) (Math.random() * range)
+ ")";
count++;
lbracket--;

} else {
e += ')';
lbracket--;

}

}

}
if (count == 0) {
e = e + "(" + (int) (Math.random() * range) + ""
+ symbol[(int) (Math.random() * 4)] + "";
whe[rbracket] = (int) (Math.random()
* (exlength - j - 1) + j + 1);
lbracket++;
rbracket++;
} else {
e += "" + symbol[(int) (Math.random() * 4)] + "("
+ (int) (Math.random() * range) + ""
+ symbol[(int) (Math.random() * 4)] + "";
whe[rbracket] = (int) (Math.random()
* (exlength - j - 1) + j + 1);
rbracket++;
lbracket++;
}
}

}
char[] strChar = e.toCharArray();
strChar[strChar.length - 1] = '=';
for(int g=0 ;g){
if(strChar[g]=='/'){
if(strChar[g+1]=='0'){
i=i-1;
System.out.print("非法表达式");
}

}
}
System.out.print(eHead);
System.out.println(strChar);
calculate ee = new calculate();
double result = ee.calcualte(e);
out.write(eHead+"答案:"+result+"\r\n"); 
out.flush();

}
out.close(); 
} catch (Exception e1) { 
e1.printStackTrace(); 
} 
}

 

.测试运行

四则运算练习器(基于控制台)_第1张图片

 

文件:

四则运算练习器(基于控制台)_第2张图片

 

你可能感兴趣的:(四则运算练习器(基于控制台))