2016012045+小学四则运算练习项目报告

 2016012045+ 小学四则运算练习项目报告

代码仓库地址:https://git.coding.net/Enther/newoperation.git

一、需求分析

二、功能设计

三、设计实现

四、代码展示

五、测试运行

六、psp

七、不足与改进

一.需求分析。

此项目主要是为面向小学生的小学数学四则运算练习所用,所以归纳出以下几点要求:

(1)     对于每个数而言不能过大,且其中不涉及小数与负数,即仅考虑100内整数形式的运算。

(2)     题目中最少包含两个运算符且保证不一样,且涉及加减乘除。

(3)     程序中需要输入参数n作为随机产生题目数量。

二.功能设计。

此项目目前仅完成了基本功能,部分实现括号,即

  1. 对参数合法性的判断。
  2. 自动生成n个运算时且其中运算符不同。
  3. 保存在指定目录下。

三.设计实现。

整体思路:本来试图运用建两个栈将数字与操作符分开的方式,利用场调度算法和逆波兰算法实现中缀便后缀计算后缀的情况。但在调试过程中出现bug。目前还需要进行改进。

此次作业仅仅实现三个运算数的情况,通过将不同的两个运算符列举,进而实现需要功能。但在真正操作中发现,这种方法的缺点在于需要考虑很多不同情况之下可能的结果,虽然现在在实现基本功能上现在还没有体现出来,但在增加操作数后,需要考虑的问题会成倍增加

设计函数:answer函数用来调用参数;

Calculate函数用来计算和列举方法;

 

四,代码展示。
复制代码
 1 public static void main (String[] args)  throws IOException{
 2         
 3         Scanner data=new Scanner(System.in);
 4          
 5         System.out.println("欢迎使用!");
 6         System.out.print("请输入所需四则运算个数(0~1000):");
 7         int Data = data.nextInt();
 8         try{
 9            
10            if (Data <= 0) {
11             System.out.println("请重新输入1至1000内整数!");
12             Data = 0;
13         }
14         else if(Data > 1000) {
15             System.out.println("请重新输入1至1000内整数!");
16             Data = 0;
17         }
18             }catch (Exception e){ 
19                    System.out.println("请重新输入1至1000内整数");
20                    return; 
21             }
22     
23        //确定生成题目数量
24            
25            
26            
27            for(int i=0;i<Data;i++){
28                 String Hresult= calculate();
29 
30 
31                     
32                    }  
33            try{
34             File file = new File("src/result.txt");
35             System.out.println("2016012045");
36             if(file.createNewFile()){
37                 System.out.println("文件创建成功!");
38             }
39             else{
40                 System.out.println("文件已经存在!");
41             }
42         }
43         catch(IOException ioe) {
44             ioe.printStackTrace();
45         }
46 
47            }//创建文件,并且向其中写入学号和所有算式     
48            
49     
50   

 

 
 
   
复制代码
  1 public static String calculate(){      
  2     String[] ope = {"+","-","*","÷"};
  3     int x = (int) (Math.random() *100);// 产生100以内的随机数
  4     int y = (int) (Math.random() *100);
  5     int a=(int) (Math.random()*4);
  6     String q = x + ope[a] + y;
  7     int sum,p,p1,p11,p2,p22,p3,p4;{
  8     switch(a){
  9     case 0:
 10          q = x + ope[a] + y;
 11          String[] ope1 = {"-","*","÷"};
 12           int b=(int) (Math.random()*3);
 13          int z =(int) (Math.random() *100);
 14          if (b==0){
 15                p=x+y;
 16                 if(p>=z){
 17                     q=q+ope1[b]+z;
 18                     sum=p-z;
 19                     q=q+"="+sum;
 20                     answer(q);
 21                    return q;
 22                 }else{
 23                    calculate();
 24                   
 25          }
 26          }//当第一个运算符为加号时,第二个运算符为减号时
 27                 else if(b==1){
 28                     p1=y*z;
 29                     sum=x+p1;
 30                    q=q+ope1[b]+z;
 31                     q=q+"="+sum;
 32                     answer(q);
 33                     return q;
 34                   }//当第一个运算符为加号时,第二个运算符为乘号时
 35                 else if(b==2){
 36                    z = (int) (Math.random() * 10) + 1;
 37                   y= (int) (Math.random() * 2) * z;
 38                    if(z!=0&&y>z&&y%z==0){
 39                    p11=y/z;   
 40                    sum=x+p11;
 41                   q=q+ope[b]+z;
 42                    q=q+"="+sum;     
 43                    answer (q);
 44                    return q;
 45                 }else{
 46                    calculate();
 47                 }//当第一个运算符为加号时,第二个运算符为除号时
 48                 }
 49     case 1:// 如果是“-”,保证a比b大,避免出现负数
 50         if (x < y) {
 51             int temp = x;
 52             x = y;
 53             y = temp;
 54         }
 55           
 56          String[] ope2 = { "+","*", "÷" };
 57           int c=(int) (Math.random()*3);
 58          int z1=(int)(Math.random()*100);
 59           q = x + ope[a] + y;
 60          if (c==0){
 61               sum=x-y;
 62              q=q+ope2[c]+z1;
 63              sum=sum+z1;
 64              q=q+"="+sum;
 65              answer (q);
 66              return q;
 67              
 68          }//当第一个运算符为减号时,第二个运算符为加号时
 69          else  if(c==1){
 70              if(x==0){
 71                  calculate();
 72              }else{
 73             p2=y*z1;
 74              if(x>=p2){
 75            q=q+ ope2[c]+z1;
 76            sum=x-p2;
 77            q=q+"="+sum;
 78            answer(q);
 79              return q;
 80          }
 81              else if(p2> x){
 82                 if(x>y){
 83                     q="("+q+")"+ope2[c]+z1;
 84                  sum=(x-y)*z1;
 85                  q=q+"="+sum;
 86                  answer(q);
 87                  return q;
 88                  }else{
 89                      calculate(); 
 90                  } 
 91 
 92          }//当第一个运算符为减号时,第二个运算符为乘号时
 93            else{
 94                z1 = (int) (Math.random() * 10) + 1;
 95                y= (int) (Math.random() * 2) * z1;
 96               if(z1!=0&&y>z1&&y%z1==0){
 97               p22=y/z1;
 98               if(x==0){
 99                   calculate();
100               }
101               else if(x>p22){
102               sum=x-p22;
103               q = q + ope[c] + z1;
104               q=q+"="+sum;     
105               answer (q);
106               return q;
107               }
108            }else{
109                calculate();
110            }
111            }//当第一个运算符为减号时,第二个运算符为除号
112              }
113          }
114     case 2:
115         String[] ope3 = { "+","-", "÷" }; 
116           int d=(int) (Math.random()*3);
117          int z2=(int)(Math.random()*100);
118            q = x + ope[a] + y;
119          if(d==0){
120             sum=x*y+z2;
121             q=q+ ope3[d]+z2;
122             q=q+"="+sum;
123              answer (q);
124               return q;
125          }
126         //当第一个运算符为乘号时,第二个运算符为加号时
127          else if(d==1){
128         
129              if(x*y>=z2)
130              {
131                 sum=x*y-z2;
132                 q=q+ ope3[d]+z2;
133                 q=q+"="+sum;
134                  answer (q);
135                   return q;
136              }
137              else{
138                  calculate();
139                  }
140          }//当第一个运算符为乘号时,第二个运算符为减号时
141              else if(d==2){
142                  p3=x*y;
143                  z2 = (int) (Math.random() * 10) + 1;
144                  y= (int) (Math.random() * 2) * z2;
145                  if(z2!=0&&y>z2&&y%z2==0){
146                   sum=p3/z2;
147                   q=q+ ope3[d]+z2;
148                   q=q+"="+sum;
149                   answer (q);
150                   return q;
151                  }else{
152                      calculate();
153                  }//当第一个运算符为乘号时,第二个运算符为除号时
154                 
155              }
156     case 3:
157          y = (int) (Math.random() * 10) + 1;
158          x = (int) (Math.random() * 2) * y;
159          q = x + ope[a] + y;
160          String[] ope4= {"+","-","*"};
161          int e=(int) (Math.random()*3);
162          int z3= (int) (Math.random()*100);
163          if(e==0){
164              if(x==0){
165              calculate(); 
166              }else{
167             sum=x/y+z3;
168             q=q+ope4[e]+z3;
169             q=q+"="+sum;
170             answer (q);
171               return q;
172                }//当第一个运算符为除号时,第二个运算符为加号时
173          }
174          else if(e==1){
175              p4=x/y;
176              if(x==0){
177                  calculate(); 
178                  }else{
179              if(p4>=z3){
180                  sum=p4-z3;
181                  q=q+ope4[e]+z3;
182                  q=q+"="+sum;
183                  answer (q);
184                  return q;
185          }
186              else{
187                  calculate();
188              
189                     }//当第一个运算符为除号时,第二个运算符为减号时
190              }
191          }else if(e==2){
192                  q=q+ope4[e]+z3;
193                  if(x==0){
194                      calculate(); 
195                      }else{
196                  if(z3==0){
197                     sum=0;
198                     q=q+"="+sum;
199                     answer(q);
200                     return q;
201                  }else{
202                      sum=x/y*z3;
203                  q=q+"="+sum;
204                  answer(q);
205                   return q;
206                  }
207              }//当第一个运算符为除号时,第二个运算符为乘号时
208            }
209  
210          }
211 
212 
213  

 

五.测试

result.txt 的部分截图

 

控制台输出

命令行输出

 

 

六,PSP展示。  

 

 

PSP2.1

任务内容

计划共完成需要的时间(min)

实际完成需要的时间(min)

Planning

计划

80

100

·        Estimate

·   估计这个任务需要多少时间,并规划大致工作步骤

100

130

Development

开发

1000

2000

·        Analysis

·         需求分析 (包括学习新技术)

80

120

·        Design Spec

·         生成设计文档

30

50

·        Design Review

·         设计复审 (和同事审核设计文档)

20

30

·        Coding Standard

·         代码规范 (为目前的开发制定合适的规范)

15

30

·        Design

·         具体设计

20

30

·        Coding

·         具体编码

800

1600

·        Code Review

·         代码复审

10

60

·        Test

·         测试(自我测试,修改代码,提交修改)

15

60

Reporting

报告

40

100

·         Test Report

·         测试报告

20

30

·         Size Measurement

·         计算工作量

10

10

·         Postmortem & Process Improvement Plan

·         事后总结, 并提出过程改进计划

15

20

 

 

七.不足与改进。

 目前的代码还很简单,甚至在算法方面还欠缺不少。由于对Java语言的不熟悉导致花费较长时间温习。此次本想运用的建栈由于考虑不全导致现在还有许多bug。这次提交的作业运用的方法比较简单,但不适用于太多操作符和运算数的情况。Java需要加强,代码能力也需要继续磨砺。

posted @ 2018-03-25 22:58  昔陌上  阅读(226)  评论(6编辑  收藏  举报