Java實現四則運算
GitHub:https://github.com/Wamelon/operations
組員:黃銳斌,張正浩
1題目:實現一個自動生成小學四則運算題目的命令行程序。
2說明:
自然數:0, 1, 2, …。
- 真分數:1/2, 1/3, 2/3, 1/4, 1’1/2, …。
- 運算符:+, ?, ×, ÷。
- 括號:(, )。
- 等號:=。
- 分隔符:空格(用於四則運算符和等號前後)。
- 算術表達式:
e = n | e1 + e2 | e1 ? e2 | e1 × e2 | e1 ÷ e2 | (e),
其中e, e1和e2為表達式,n為自然數或真分數。
- 四則運算題目:e = ,其中e為算術表達式。
3需求:
1. 使用 -n 參數控制生成題目的個數,例如
Myapp.exe -n 10
將生成10個題目。
2. 使用 -r 參數控制題目中數值(自然數、真分數和真分數分母)的範圍,例如
Myapp.exe -r 10
將生成10以內(不包括10)的四則運算題目。該參數可以設置為1或其他自然數。該參數必須給定,否則程序報錯並給出幫助信息。
3. 生成的題目中計算過程不能產生負數,也就是說算術表達式中如果存在形如e1 ? e2的子表達式,那麽e1 ≥ e2。
4. 生成的題目中如果存在形如e1 ÷ e2的子表達式,那麽其結果應是真分數。
5. 每道題目中出現的運算符個數不超過3個。
6. 程序一次運行生成的題目不能重復,即任何兩道題目不能通過有限次交換
生成的題目存入執行程序的當前目錄下的Exercises.txt文件,格式如下:
1. 四則運算題目1
2. 四則運算題目2
……
其中真分數在輸入輸出時采用如下格式,真分數五分之三表示為3/5,真分數二又八分之三表示為2’3/8。
7. 在生成題目的同時,計算出所有題目的答案,並存入執行程序的當前目錄下的Answers.txt文件,格式如下:
1. 答案1
2. 答案2
特別的,真分數的運算如下例所示:1/6 + 1/8 = 7/24。
8. 程序應能支持一萬道題目的生成。
9. 程序支持對給定的題目文件和答案文件,判定答案中的對錯並進行數量統計,輸入參數如下:
Myapp.exe -e <exercisefile>.txt -a <answerfile>.txt
統計結果輸出到文件Grade.txt,格式如下:
Correct: 5 (1, 3, 5, 7, 9)
Wrong: 5 (2, 4, 6, 8, 10)
PSP表格
一、需求分析
(1)能夠自動生成簡單運算的式子,可以定義其的數量和大小。
(2)運算符要包括“+,-,×,÷”,其中還要支持括號和分數的計算。
(3)程序要能夠自己生成答案,並保存在文件夾中。
(4)可以計算做對以及做錯的題數和題號
二、功能設計
通過控制臺生成四則運算,進行查重,將式子寫入Exercise.txt中,
計算每道題的結果寫入Answer.txt
在zuoda.txt中作答,通過比較,在Grade.txt中生成成績
三、設計實現
四、關鍵代碼
//主頁
public class HomePage { public static void main(String[] args)throws Exception { System.out.print("請輸入生成的題目數量:"); String input1 = input(); while (!isInt(input1)) { System.out.println("輸入錯誤,請重新輸入!"); input1 = input(); } System.out.print("請輸入題目的數值範圍:"); String input2 = input(); while (!isInt(input2)) { System.out.println("輸入錯誤,請重新輸入!"); input2 = input(); } question ques = new question(); ques.Exam(Integer.parseInt(input1), Integer.parseInt(input2)); System.out.println("出題完畢,請到zuoda.txt作答!"); System.out.println("如需批改請按‘1’,任意鍵退出"); String input3 = input(); if (input3.equals("1")){ CompareAnswer.compare(); System.out.println("批改已完成,請到Grade.txt查看"); } else System.exit(0);
}
//生成式子
public void Exam(int count,int range) throws Exception { int re=0; String[] strr = new String[count+1]; String[] strr2 = new String[count+1]; String[] repeat =new String[count+1]; FileWriting.fileClear(); String subject; for(int i=0;i<count;i++) { CreateFormula create=new CreateFormula(); subject=create.Creating(range); //去掉多余括號 subject=deleteKuoHao(subject); str=Check.chaChong(subject); strr[i]=str; strr2[i]=subject; int xy=1; for(int h=1;h<i;h++) { if(strr[h].equals(str)) { xy=0; repeat[re]=h+","+strr2[h]+" Repeat "+i+","+subject; re++; break; } } if(xy==0) { i--; continue; } else { String result=Calculate.getResult(subject); FileWriting.writter((i+1)+":"+subject); FileWriting.writter1((i+1)+":"+result); FileWriting.writter2((i+1)+":"); System.out.print("第"+(i+1)+"題,"+subject+"\n"); } }
//生成算式
public class CreateFormula {
private static char[] operate = { ‘+‘, ‘-‘, ‘*‘, ‘÷‘ }; public String Creating(int range){ String subject = ""; Random r = new Random(); int operator = r.nextInt(3)+1;//操作符個數 int kuohao=0; boolean flag=true; for(int i=1;i<=operator;i++) { Random rand=new Random(); int temp=rand.nextInt(3)+1; if(temp==1)//決定是否加入左括號 { subject+="("; kuohao++; subject+=this.getNum(range); subject += this.getOperate(); } else if (temp == 2 && flag) {//決定是否加入左括號 flag=false; subject="("+subject; kuohao++; subject+=this.getNum(range); subject += this.getOperate(); }else if(temp==3&& kuohao!=0){//決定是否加入右括號 subject+=this.getNum(range); subject+=")"; kuohao--; subject += this.getOperate(); }else{ subject+=this.getNum(range); subject += this.getOperate(); } } subject+=this.getNum(range); for(int i=kuohao;i>0;i--) { subject=subject+")"; } return subject; }
//計算
public class Calculate { public static String getResult(String s) { String result=Calculate.PRNCal(s); String[] ary = result.split("/"); int a = Integer.parseInt(ary[0].trim()); int b = Integer.parseInt(ary[1].trim()); if (Math.abs(a) < Math.abs(b)) { result = a + "/" + b; } else if (a == b) { result = 1 + ""; } else if (a == -b) { result = -1 + ""; } else { int yu; int da; if (a == 0 || b == 0) { yu = 0; da = 0; } else { yu = a % b; da = a / b; } if (yu == 0) result = da + ""; else result = da + "‘" + Math.abs(yu) + "/" + Math.abs(b); } return result; }
//對比答案
public class CompareAnswer { private static String filePath2 = "answer.txt"; private static String filePath3 = "zuoda.txt"; public static void compare() throws Exception { String right = ""; String error = ""; int correct=0; int wrong=0; InputStreamReader reader = new InputStreamReader(new FileInputStream( filePath2), "gbk"); BufferedReader br = new BufferedReader(reader); String s = null; InputStreamReader reader1 = new InputStreamReader(new FileInputStream( filePath3), "gbk"); BufferedReader br1 = new BufferedReader(reader1); String s1 = null; int i = 0; right = ""; error = ""; while (((s = br.readLine()) != null) && (s1 = br1.readLine()) != null) { i++; if (s.trim().equals(s1.trim())) { correct++; right = right + i + ","; } else { wrong++; error = error + i + ","; } } String str1 = "Correct:" + correct; if (right.length() <= 1) str1 = str1 + "(" + ")"; else str1 = str1 + "(" + right.substring(0, right.length() - 1)+ ")"; String str2 = "Wrong:" + wrong; if (error.length() <= 1) str2 = str2 + "(" + ")"; else str2 = str2 + "(" + error.substring(0, error.length() - 1)+ ")"; FileWriting.fileClear(); FileWriting.writter3(str1); FileWriting.writter3(str2); } }
五、測試運行
六、小結
由於平時較少與人合作項目,此次與隊友合作花費時間也較長,中間也遇到不少問題,不過遇到問題可以相互討論,是一個人無法完成的。
Java實現四則運算