該程序能自動生成四則運算。java
須要有四種判斷.要根據定義的範圍輸出題目.可生成真分數類計算式編程
錄入結果後能判斷對錯。dom
有單獨類提早知曉計算結果.有單獨類可儲存現有輸入結果.二者能夠比較.可統計輸出與輸入結果相比較的正確率。學習
設計三個難度,對應運算符的多少以及是否加入括號
設計兩種類,一種用來生成題目,一種用來計算題目
計算:現將中綴表達式轉換爲後綴,在進行計算測試
完善了上次的代碼。this
import java.util.Random; public class NewProject extends SetNum{ public String Front(){ String result; switch (getC()){ case 0: result = getA()+Operator[getB()]; break; case 1: result = getA()+Operator[getB()]+getA()+Operator[getB()]; break; default: result = getA()+Operator[getB()]+getA()+Operator[getB()]+getA()+Operator[getB()]; } return result; } public String Middle(){ String result; switch (getC()){ case 0: result = getA()+""; break; case 1: result ="("+getA()+Operator[getB()]+getA()+")"; break; default: result = "("+getA()+Operator[getB()]+"("+getA()+Operator[getB()]+getA()+")"+Operator[getB()]+getA()+")"; } return result; } public String End(){ String result; switch (getC()){ case 0: result = Operator[getB()]+getA(); break; case 1: result = Operator[getB()]+getA()+Operator[getB()]+getA(); break; default: result = Operator[getB()]+getA()+Operator[getB()]+getA()+Operator[getB()]+getA(); } return result; } public String Allpart(){ String result; switch (getC()){ case 0: result = Front()+Middle(); break; case 1: result = Middle()+Front(); default: result = Front()+Middle()+End(); } return result; } public String JudgeAllpart(){ String result =Allpart(); for(int i = 0;i<result.length();i++){ if ((result.charAt(i)=='/')&&(result.charAt(i+1)=='0')){ result = Allpart(); } } return result; } } import java.util.Random; public class SetNum { Random in = new Random(); int a; int b; int c; public int getC() { setC(); return c; } public void setC() { this.c = in.nextInt(3); } String [] Operator = {"+","-","*","/"}; public int getB() { setB(); return b; } public void setB() { this.b = in.nextInt(4); } public void setA() { this.a = in.nextInt(10); } public int getA() { setA(); return a; } }
public void testCount() throws Exception { //TODO: Test goes here... String line; BufferedReader rdr = new BufferedReader(new FileReader("testdata.txt")); while ((line = rdr.readLine()) != null) { if (line.startsWith("#")) { continue; } StringTokenizer st = new StringTokenizer(line); if (!st.hasMoreTokens()) { continue; } String val = st.nextToken(); String expected = val; LinkedList<String> argument_list = new LinkedList(); while (st.hasMoreTokens()) { argument_list.add(st.nextToken()); } Postfix a = new Postfix(); a.transferToPostfix(argument_list); assertEquals(expected, a.transferToPostfix(argument_list));; } }