Arithmometer: A Node.js implementation
GitHub: https://github.com/m8705/Arithmometer
Project elaboration
In this project, We are required to actualize a command line program for automatically generating four arithmetic problems for primary schools.
Now the project is still in progress, we have so far finished exercise judgement, and soon we will update more information.
Team members: Wu Tang Huang, Zhang Guo Jun
Detail description
Program usage
Generate exercise file:
node e.js -n 10 -r 10
Validate exercise:
node e.js -e exercisefile.txt -a answerfile.txt
Parameter and regulation
1. Use -n parameter to control the number of generated questions(1~10000).
2. Use -r parameter to control the range of numeric value (natural number, true fraction and true fraction denominator) in the title(1~100).
3. The calculation process in the generated problem does not produce a negative number, that is, if there is a sub-expression in the arithmetic expression, such as e1 - e2, then e1 < e2
4. If there is a subexpression e1 ÷ e2 in the generated exercise, the result should be true score.
5. There are no more than 3 operators in each problem.
6. The problem generated by the program running at one time can not be repeated, that is, any two problems can not be transformed into the same problem by the finite number of exchange + and * arithmetic expressions.The generated problem is stored in the Exercises.txt file under the current directory of the execution program.
7. At the same time, the answers to all the questions are computed and stored in the Answers.txt file in the current directory of the execution program.
8. The program should support the generation of ten thousand problems.
9. Program support for a given question file and answer file, determine the right and wrong answers and statistics, statistics output to the file Grade.txt
Code preview
1 //console.log(process.argv); 2 3 var arg1 = process.argv[2]; 4 var arg2 = process.argv[3]; 5 var arg3 = process.argv[4]; 6 var arg4 = process.argv[5]; 7 8 if( (arg1 === "-n") && (arg3 === "-r") ){//生成題目 9 10 arg2 = Math.floor(arg2);//取整 11 if( arg2 > 0 && arg2 <= 10000 ){ 12 13 arg4 = Math.floor(arg4);//取整 14 if( arg4 > 0 && arg4 <= 100 ){ 15 16 produce(arg2,arg4); 17 18 } 19 else{ 20 21 console.log("題目生成範圍不正確!允許範圍:1~100"); 22 23 } 24 25 } 26 else{ 27 28 console.log("題目數量不正確!允許範圍:1~10000"); 29 return; 30 31 } 32 33 } 34 else if( (arg1 === "-e") && (arg3 === "-a") ){//檢查題目 35 36 arg2 = arg2 ? arg2 : "exercisefile.txt"; 37 arg4 = arg4 ? arg4 : "answerfile.txt"; 38 39 judge(arg2,arg4); 40 41 } 42 else{ 43 44 console.log("參數錯誤!"); 45 console.log("用例:"); 46 console.log("生成題目:node e.js -n 10 -r 10"); 47 console.log("檢查題目:node e.js -e exercisefile.txt -a answerfile.txt"); 48 49 } 50 51 function convert(str){//將任何數轉成真分數(小數不換) 52 53 //整數 2 = 2‘1/1 54 //真分數 3/8 55 //假分數 2‘2/3 56 //帶分數 1‘1/1 57 58 if( str.indexOf("/") >= 0 ){//真分數或帶分數 59 60 if( str.indexOf("‘") >= 0 ){//帶分數 61 62 first = str.split("‘")[0]; 63 second = str.split("‘")[1]; 64 65 up = second.split("/")[0]; 66 down = second.split("/")[1]; 67 68 if( ( up === down ) || ( down === "1" ) ){ 69 return "ERROR"; 70 } 71 72 str = ( (+first) * (+down) + (+up) ) + "/" + down; 73 74 } 75 else{//真分數 76 ; 77 } 78 79 } 80 else{//整數 81 82 str = str + "/1"; 83 84 } 85 86 console.log(str); 87 } 88 89 90 function produce(n, range){ 91 92 //console.log(n,range); 93 94 //隨機生成符號(1,2,3) -> 隨機生成運算數種類 -> 隨機生成運算數 -> 隨機生成括號 95 96 var symbol = Math.random(); 97 var symbolNum; 98 99 if( symbol <= 1/3 ){//生成一個符號 100 symbolNum = 1; 101 } 102 else if( symbol <= 2/3 ){//生成兩個符號 103 symbolNum = 2; 104 } 105 else{//生成三個符號 106 symbolNum = 3; 107 } 108 109 var symbolChoice = []; 110 var tmp; 111 for(var a = 0; a < symbolNum; a++){ 112 113 tmp = Math.random(); 114 if( tmp <= 1/4 ){ 115 symbolChoice.push("+"); 116 } 117 else if( tmp <= 2/4 ){ 118 symbolChoice.push("-"); 119 } 120 else if( tmp <= 3/4 ){ 121 symbolChoice.push("*"); 122 } 123 else{ 124 symbolChoice.push("/"); 125 } 126 127 } 128 129 console.log(symbolChoice); 130 131 132 var numChoice = []; 133 134 135 136 } 137 138 139 function judge(file1,file2){ 140 141 console.log(file1,file2) 142 143 var fs = require("fs"); 144 145 fs.readFile(file1, function (err1, data1) {// 異步讀取練習題 146 147 if (err1) { 148 return console.error(err1); 149 } 150 151 var lines1 = data1.toString().split("\r\n"); 152 153 fs.readFile(file2, function (err2, data2) {// 異步讀取答案 154 155 if (err2) { 156 return console.error(err2); 157 } 158 159 var lines2 = data2.toString().split("\r\n"); 160 161 if(lines1.length !== lines2.length){ 162 163 console.log("題目數與答案數不匹配!題目數:" + lines1.length + "答案數" + lines2.length ); 164 return; 165 166 } 167 168 var correct = []; 169 var wrong = []; 170 171 var expression; 172 var left,right; 173 174 175 for( var a = 0; a < lines1.length; a++ ){//按行讀取 176 177 expression = lines1[a].split(". ")[1]; 178 left = eval(expression); 179 180 answer = lines2[a].split(". ")[1]; 181 right = eval(answer); 182 183 if( Math.abs(right - left) < 1e-10 || Math.abs(left - right) < 1e-10 ){//正確 184 185 correct.push( (a + 1) + "" ); 186 187 } 188 else{//錯誤 189 190 wrong.push( (a + 1) + "" ); 191 192 } 193 194 195 } 196 197 var result = ""; 198 result += "Correct: " + correct.length + " (" + ("" + correct ) + ")\n"; 199 result += "Wrong: " + wrong.length + " (" + ("" + wrong ) + ")\n"; 200 201 202 fs.writeFile(‘Grade.txt‘, result, function(err) { 203 if (err) { 204 return console.error(err); 205 } 206 console.log("數據寫入成功!"); 207 console.log("--------我是分割線-------------"); 208 }); 209 210 }); 211 212 }); 213 214 215 216 }
PSP2.1 |
Personal Software Process Stages |
預估耗時(分鐘) |
實際耗時(分鐘) |
Planning |
計劃 |
120 | 60 |
· Estimate |
· 估計這個任務需要多少時間 |
15 | 15 |
Development |
開發 |
360 | 300 |
· Analysis |
· 需求分析 (包括學習新技術) |
60 | 60 |
· Design Spec |
· 生成設計文檔 |
30 | 30 |
· Design Review |
· 設計復審 (和同事審核設計文檔) |
30 | 30 |
· Coding Standard |
· 代碼規範 (為目前的開發制定合適的規範) |
15 | 15 |
· Design |
· 具體設計 |
60 | 60 |
· Coding |
· 具體編碼 |
120 | 100 |
· Code Review |
· 代碼復審 |
120 | 160 |
· Test |
· 測試(自我測試,修改代碼,提交修改) |
40 | 40 |
· Reporting |
· 報告 |
90 | 90 |
· Test Report |
· 測試報告 |
60 | 60 |
· Size Measurement |
· 計算工作量 |
30 | 30 |
· Postmortem & Process Improvement Plan |
· 事後總結, 並提出過程改進計劃 |
30 | 30 |
合計 |
1180 | 1080 |
Arithmometer: A Node.js implementation