2016012036+小學四則運算練習軟件項目報告
需求分析
首先明確項目需要,一個可以定做小學四則運算的程序。所以需要我們能夠隨機生成不同的四則運算,範圍應在100以內,包含“+,-,*,÷”四種操作。要求:不存在小數,題目盡量不重復。
功能設計
基本功能應當包括,讀入用戶想要定制的算式數,以及對應的運算符數量,然後生成對應數目和難度的四則運算式子。並由程序自己計算,給出相應的答案。
擴展功能可以用:在算式中包含括號,計算存在真分數的式子。
設計實現
由計算機生成隨機的第一個數字、運算符、後續數字、完成計算、顯示結果等等。
在生成後續數字時,可以限定生成條件以保證運算結果不會出現分數和負數的情況。
代碼詳解
Lib類中
1static String createFormula() 2 { 3 int digital, sign = 0; 4 char symbol = 0; 5 String formula = ""; 6 7 digital = (int)(Math.random()*20); 8 formula = formula + digital; 9 10 for(int i=0; i<5; i++) 11 { 12 if(sign >= 2)13 sign = (int)(Math.random()*2); 14 else sign = (int)(Math.random()*4); 15 16 if(sign == 0) { 17 symbol = ‘+‘; 18 digital = (int)(Math.random()*20+1); 19 } 20 else if(sign == 1) { 21 symbol = ‘-‘;22 digital = (int)(Math.random()*digital+1); //??è¥é?ò???digital 23 } 24 else if(sign == 2) { 25 symbol = ‘*‘; 26 digital = (int)(Math.random()*(100/(digital+1))+1); 27 28 } 29 else if(sign == 3) { 30 symbol = ‘÷‘; 31 for(int j=2; j<=digital; j++) 32 if(digital%j == 0) { 33 digital = j; 34 break; 35 } 36 } 37 formula = formula + symbol + digital; 38 } 39 return formula; 40 }
使用createFormula()創建算式,:
1、隨機生成算式第一個數(20以內)
2、依次確定(生成)五個運算符以及第二個數字
(1)sign用於避免出現連續的“乘”、“除”運算(原理:sign隨機確定運算符,sign的上一個值>=2時,後面生成的運算符只有“+”、“-”)
(2)為避免出現負數,當sign為“-”時,後一個數的生成範圍為“比上一個數小”
(3)當sign為“×”時,為避免出現大雨100的結果,第二個數的生成範圍為[0,100/上一個數]
(4)當sign為“÷”時,為保證不出現分數,第二個數為 [2~第一個數] 這個範圍內找“第一個數的因數”生成
3、實現字符串拼接
static int answerFormula(String formula) { char[] charArray = formula.toCharArray(); int[] stackForInt = new int[10]; char[] stackForChar = new char[10]; int topOfInt = -1, topOfChar = -1, num = 0; for(int i=0; i<charArray.length; i++){ //System.out.println(i+" :"+topOfInt+" "+topOfChar); if(Character.isDigit(charArray[i])) { num = num*10 + charArray[i]-‘0‘; if(i == charArray.length - 1) stackForInt[++topOfInt] = num; } else { stackForInt[++topOfInt] = num; num = 0; if(topOfChar == -1) stackForChar[++topOfChar] = charArray[i]; else if(charArray[i]==‘*‘ || charArray[i]==‘÷‘) { if(stackForChar[topOfChar] == ‘*‘ || stackForChar[topOfChar] == ‘÷‘) { int num1 = stackForInt[topOfInt--]; int num2 = stackForInt[topOfInt--]; char symbol = stackForChar[topOfChar--]; if(symbol == ‘*‘) stackForInt[++topOfInt] = num1 * num2; else if(symbol == ‘÷‘) stackForInt[++topOfInt] = num2 / num1; stackForChar[++topOfChar] = charArray[i]; } else if(stackForChar[topOfChar] == ‘+‘ || stackForChar[topOfChar] == ‘-‘) { stackForChar[++topOfChar] = charArray[i]; } } else if(charArray[i]==‘+‘ || charArray[i]==‘-‘) { if(topOfChar == -1) stackForChar[++topOfChar] = charArray[i]; else { while(topOfChar != -1) { int num1 = stackForInt[topOfInt--]; int num2 = stackForInt[topOfInt--]; char symbol = stackForChar[topOfChar--]; if(symbol == ‘+‘) stackForInt[++topOfInt] = num1 + num2; else if(symbol == ‘-‘) stackForInt[++topOfInt] = num2 - num1; else if(symbol == ‘*‘) stackForInt[++topOfInt] = num1 * num2; else if(symbol == ‘÷‘) stackForInt[++topOfInt] = num2 / num1; } } stackForChar[++topOfChar] = charArray[i]; } } } while(topOfChar!=-1) { int num1 = stackForInt[topOfInt--]; int num2 = stackForInt[topOfInt--]; char symbol = stackForChar[topOfChar--]; if(symbol == ‘+‘) stackForInt[++topOfInt] = num1 + num2; else if(symbol == ‘-‘) stackForInt[++topOfInt] = num2 - num1; else if(symbol == ‘*‘) stackForInt[++topOfInt] = num1 * num2; else if(symbol == ‘÷‘) stackForInt[++topOfInt] = num2 / num1; } return stackForInt[0]; }
答案部分:使用中序運算式轉成後序運算式以計算表達式
1、stackForInt:模擬int棧
2、stackForChar:模擬字符棧
3、topOfChar / topOfInt:模擬棧頂
將string類型的算式轉化成數組
用中序運算讀取字符數字並計算得出答案
static void printFormula(String[] formulaList, int n)throws IOException { FileOutputStream fs = new FileOutputStream(new File("result.txt")); PrintStream p = new PrintStream(fs); p.println("2016012036"); for(int i=0; i<n; i++) p.println(formulaList[i]); p.close(); }
在result中顯示結果(優先顯示學號信息)
在Main中
public static void main(String args[]) { Scanner input = new Scanner(System.in); int n; n = input.nextInt(); String[] formulaList = new String[50]; for(int i=0; i<n; i++) { formulaList[i] = Lib.createFormula(); //System.out.println(formula); int answer = Lib.answerFormula(formulaList[i]); formulaList[i] = formulaList[i] + ‘=‘ + answer; } try { Lib.printFormula(formulaList, n); } catch(IOException ioe) { ioe.printStackTrace(); } }
主函數循環n次,生成n個式子並完成計算,catch異常。
結果展示
基本實現了基礎功能
用“模塊化”將代碼分塊實現,避免了很多不必要的麻煩,整個項目實現起來也更加流暢,思路更加清晰,對效率的提高有很大幫助。
PSP展示
這次作業戰線拉得特別長,從分塊方式等開始慢慢構思,很多思考過程都是在平時的生活中完成的,而且在實現過程中因為不熟練,每天的進度非常低,所以並沒有使用PSP……而且之前也敗在沒有真正習慣事先定好時間計劃,所以實在沒辦法進行PSP展示。
2016012036+小學四則運算練習軟件項目報告