个人作业1 四则运算题目生成程序

项目地址:https://gitee.com/wenguixin/javascript_four_algorithms.git

1、题目描述:

  生成定量小学四则运算的题目。

2、需求分析:

  在现今的时代,科技丰富了我们的生活。纸质作业对于老师和学生有着不少的障碍,无法实时反馈和方便练习等。对此,开发一些能简单自动生成的题目的程序是很有发展前景的。

3、功能设计:

  ①生成四则运算题目,并且计算答案。

  ②用于网页实现,较为直观。

  ②还有难度测试没有完善,需要后台操作,没有实现查重。

4、设计实现:

   calculate.js文件主要是用于生成和计算表达式的。

  ·getRPN函数主要是用于生成后缀表达式。

  ·fracyionCul函数主要是用于以分数的形式进行计算。

 index.html用于页面生成。

5、代码说明:

 

后缀表达式的生成:

 1 function getRPN(text){
 2     var operand = [], //用于存放操作数的栈
 3         operator = [], //用于存放操作符的栈
 4         textArr = text.split(''),
 5         newTextArr = [];
 6     calTextArr = []; //用于存放操作数与操作符分割后的数组
 7     for(var i = 0; i < textArr.length; i++){
 8         if(!Number(text[i]) && Number(text[i]) != 0){
 9             newTextArr.push("|",text[i],"|");
10         }
11         else{
12             newTextArr.push(textArr[i]);
13         }
14     }
15     var calTextArr = newTextArr.join('').split("|");
16     calTextArr.unshift("#")
17 
18     for(var i = 0; i < calTextArr.length; i++){
19         //如果是数字则直接入栈
20         if(Number(calTextArr[i]) || Number(calTextArr[i]) == 0){
21             operand.push(calTextArr[i]);
22         }
23         //如果是操作符则再根据不同的情况进行操作
24         else {
25             switch(true){
26                 //如果operator栈顶是“(”或者遍历到的操作符是“(”则直接入栈
27                 case calTextArr[i] == "(" && operator.slice(-1)[0] == "(":
28                     operator.push(calTextArr[i]);
29                     break;
30 
31                 /*如果遍历到的操作符是“)”则把operator中的操作符依次弹出并压入
32                  operand中直至operator栈顶操作符为“(”,然后将“(”也弹出,但不压入
33                  operand栈中
34                  */
35                 case calTextArr[i] == ")":
36                     do{
37                         operand.push(operator.pop());
38                     }while(operator.slice(-1)[0] != "(");
39                     operator.pop();
40                     break;
41 
42                 //如果是其他的操作符,则比较优先级后再进行操作
43                 default:
44                     var compare = compareOperator(calTextArr[i],operator.slice(-1)[0]);
45                     var a = calTextArr[i];
46                     var b = operator.slice(-1)[0]
47                     if(operator.length == 0){
48                         operator.push(calTextArr[i]);
49                     }
50                     else if(compareOperator(calTextArr[i],operator.slice(-1)[0])){
51                         do{
52                             operand.push(operator.pop());
53                             var compareResult = compareOperator(calTextArr[i],operator.slice(-1)[0]);
54                         }while(compareResult);
55                         operator.push(calTextArr[i]);
56                     }
57                     else {
58                         operator.push(calTextArr[i]);
59                     }
60                     break;
61             }
62         }
63     }
64     //遍历结束后,将operator中的元素全部压入operand中
65     operator.forEach(function(){
66         operand.push(operator.pop());
67     });
68     //把用于比较的“#”字符去掉
69     operator.pop();
70     return operand;
71 }

 

计算表达式:

  1 function fractionCul(endStr){
  2     var length = endStr.length,
  3         stack = new Array(),
  4        num1,num2,object1,object2;
  5     for(var i =0;i<length;i++){
  6         var key = endStr[i];
  7         if(globalOperate.indexOf(key) !== -1){//是操作符的情况
  8             switch (key){
  9                 case '+':
 10                     object1 = stack.pop();//出栈
 11                     object2 = stack.pop();
 12                     if(object1 instanceof Fraction){//如果是分数
 13                         if(object2 instanceof  Fraction){//是分数
 14                             stack.push(object2.add(object1));//栈底的减去栈顶的元素,先进后出
 15                         }else{
 16                             num1 = Number(object2);
 17                             stack.push(new Fraction(num1).add(object1));
 18                         }
 19                     }else{
 20                         if(object2 instanceof Fraction){
 21                             num1 = Number(object1);
 22                             stack.push(object2.add(new Fraction(num1)));
 23                         }else{
 24                             num1 = Number(object1);
 25                             num2 = Number(object2);
 26                             stack.push(new Fraction(num2).add(new Fraction(num1)))
 27                         }
 28                     }
 29                     break;
 30                 case '-':
 31                     object1 = stack.pop();
 32                     object2 = stack.pop();
 33                     if(object1 instanceof Fraction){
 34                         if(object2 instanceof  Fraction){
 35                             stack.push(object2.sub(object1));
 36                         }else{
 37                             num1 = Number(object2);
 38                             stack.push(new Fraction(num1).sub(object1));
 39                         }
 40                     }else{
 41                         if(object2 instanceof Fraction){
 42                             num1 = Number(object1);
 43                             stack.push(object2.sub(new Fraction(num1)));
 44                         }else{
 45                             num1 = Number(object1);
 46                             num2 = Number(object2);
 47                             stack.push(new Fraction(num2).sub(new Fraction(num1)))
 48                         }
 49                     }
 50                     break;
 51                 case '*':
 52                     object1 = stack.pop();
 53                     object2 = stack.pop();
 54                     if(object1 instanceof Fraction){
 55                         if(object2 instanceof  Fraction){
 56                             stack.push(object2.mul(object1));
 57                         }else{
 58                             num1 = Number(object2);
 59                             stack.push(new Fraction(num1).mul(object1));
 60                         }
 61                     }else{
 62                         if(object2 instanceof Fraction){
 63                             num1 = Number(object1);
 64                             stack.push(object2.mul(new Fraction(num1)));
 65                         }else{
 66                             num1 = Number(object1);
 67                             num2 = Number(object2);
 68                             stack.push(new Fraction(num2).mul(new Fraction(num1)))
 69                         }
 70                     }
 71                     break;
 72                 case '÷':
 73                     object1 = stack.pop();
 74                     object2 = stack.pop();
 75                     if(object1 instanceof Fraction){
 76                         if(object2 instanceof  Fraction){
 77                             stack.push(object2.div(object1));
 78                         }else{
 79                             num1 = Number(object2);
 80                             stack.push(new Fraction(num1).div(object1));
 81                         }
 82                     }else{
 83                         if(object2 instanceof Fraction){
 84                             num1 = Number(object1);
 85                             stack.push(object2.div(new Fraction(num1)));
 86                         }else{
 87                             num1 = Number(object1);
 88                             num2 = Number(object2);
 89                             stack.push(new Fraction(num2).div(new Fraction(num1)))
 90                         }
 91                     }
 92                     break;
 93                 case '/':
 94                     num1 = Number(stack.pop());
 95                     num2 = Number(stack.pop());
 96                     stack.push(new Fraction(num2,num1));
 97                     break;
 98             }
 99         }else{//操作数直接进栈
100             stack.push(key);
101         }
102     }
103     var fraction = stack.pop();
104     return fraction.getResult();
105 }

六、测试运行:

定向生成10题,而在难度设置也没有做完。

 

 

 

 

7、展示RSP:
PSP2.1Personal Software Process StagesTime
Planning 计划 -
· Estimate · 估计这个任务需要多少时间 20h
Development 开发 -
· Analysis · 需求分析 (包括学习新技术) 3h
· Design Spec · 生成设计文档  
· Design Review · 设计复审 (和同事审核设计文档) 1h
· Coding Standard · 代码规范 (为目前的开发制定合适的规范) 1h
· Design · 具体设计 2h
· Coding · 具体编码 3h
· Test · 测试(自我测试,修改代码,提交修改) 5h
Reporting 报告 -
· Test Report · 测试报告 1h
· Size Measurement · 计算工作量 1h
· Postmortem & Process Improvement Plan · 事后总结, 并提出过程改进计划) 0.5h
- 合计 18h

posted on 2018-03-31 18:19  俳懮  阅读(99)  评论(0编辑  收藏  举报

导航