Arithmometer: A Node.js implementation
Posted m870100
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(" "); 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(" "); 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 ) + ") "; 199 result += "Wrong: " + wrong.length + " (" + ("" + wrong ) + ") "; 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的主要内容,如果未能解决你的问题,请参考以下文章