2016012045 +x小学四则运算联系项目报告
代码仓库地址:https://git.coding.net/Enther/operation.git
一.需求分析。
此项目主要是为面向小学生的小学数学四则运算练习所用,所以归纳出以下几点要求:
(1) 对于每个数而言不能过大,且其中不涉及小数与负数,即仅考虑100内整数形式的运算。
(2) 题目中最少包含两个运算符且保证不一样,且涉及加减乘除。
(3) 程序中需要输入参数n作为随机产生题目数量。
二.功能设计。
此项目目前仅完成了基本功能,即
- 对参数合法性的判断。
- 自动生成n个运算时且其中运算符不同。
- 保存在指定目录下。
三.设计实现。
整体思路:本来试图运用建两个栈将数字与操作符分开的方式,利用场调度算法和逆波兰算法实现中缀便后缀计算后缀的情况。但在调试过程中出现bug。目前还需要进行改进。
此次作业仅仅实现三个运算数的情况,通过将不同的两个运算符列举,进而实现需要功能。但在真正操作中发现,这种方法的缺点在于需要考虑很多不同情况之下可能的结果,虽然现在在实现基本功能上现在还没有体现出来,但在增加操作数后,需要考虑的问题会成倍增加
设计函数:answer函数用来调用参数;
Calculate函数用来计算和列举方法;

四,代码展示。
这段代码用于将三个操作数的不同运算状况分类计算:
public static void calculate(){
String[] ope = {"+","-","*","/"};
int x = (int) (Math.random() *100);// 产生100以内的随机数
int y = (int) (Math.random() *100);
int a = (int) (Math.random()*4);
int sum,p1,p11,p2,p3;{
switch(a){
case 0://第一个运算符为加号
String[] ope1 = {"-","*","÷"};
int z = (int) (Math.random() *100);
int b=(int) (Math.random()*3);
String q = x + ope[a] + y;
if (b==0){//先加后减
p1=x+y;
if(p1>=z){
q=q+ope1[b]+z;
sum=x+y-z;
q=q+"="+sum;
answer(q);
return ;
}else{
answer("");
return ;
}
}
else if(b==1){//第一个运算符为加号,第二个运算符为乘号 先乘后加
p1=y*z;
sum=x+p1;
q=q+"="+sum;
answer (q);
return;
}
else{ //第一个运算符为加号,第二个运算符为除号 保证整除
z = (int) (Math.random() * 10) + 1;
y= (int) (Math.random() * 8) *z ;
if(z!=0&&y>z&&y%z==0){
p11=y/z;
sum=x+p11;
q=q+"="+sum;
answer (q);
return;
}else{
answer(q);
}
}
case 1:// 如果是“-”,保证a比b大,避免出现负数
if (x < y) {
int temp = x;
x = y;
y = temp;
}
q = x + ope[a] + y;
String[] ope2 = { "+","*", "÷" };
int c = (int) (Math.random()*3);
int z1=(int)(Math.random()*100);
if (c==0){
sum=x-y;
q=q+ope2[c]+z1;
sum=sum+z1;
answer (q);
return ;
}
else if(c==1){
p2=y*z1;
if(x>=p2){
q=q+ ope2[c]+z1;
sum=x-p2;
answer(q);
return ;
}else{
answer(q);
return ;
}
}
case 2://第一个运算符为乘号
String[] ope3 = { "+","-", "÷" };
int d = (int) (Math.random()*3);
int z2=(int)(Math.random()*100);
q = x + ope[a] + y;
if(d==0){
sum=x*y+z2;
}else if(d==1){
if(x*y>=z2)
{
sum=x*y-z2;
q=q+ ope3[d]+z2;
q=q+"="+sum;
}
else{
answer(q);
return ;
}
}
else{
p3=x*y;
z2 = (int) (Math.random() * 10) + 1;
p3= (int) (Math.random() * 8) *z2 ;
sum=p3/z2;
q=q+ ope3[d]+z2;
q=q+"="+sum;
}
case 3://第一个运算符为除数
y = (int) (Math.random() * 10) + 1;
x = (int) (Math.random() * 8) * y;
String[] ope4= {"+","-","*"};
q = x + ope[a] + y;
int e= (int) (Math.random()*3);
int z3= (int) (Math.random()*100);
if(e==0){
sum=x/y+z3;
q=q+ope4[e]+z3;
q=q+"="+sum;
}
else if(e==1){
if(x/y>=z3){
sum=x/y-z3;
q=q+ope4[e]+z3;
q=q+"="+sum;
}
else{
answer(q);
}
}
else{
sum=x/y*z3;
q=q+ope4[e]+z3;
q=q+"="+sum;
answer(q);
}
}
}
}
result.txt 的截图

五,PSP展示。
|
PSP2.1 |
任务内容 |
计划共完成需要的时间(min) |
实际完成需要的时间(min) |
|
Planning |
计划 |
8 |
10 |
|
· Estimate |
· 估计这个任务需要多少时间,并规划大致工作步骤 |
8 |
6 |
|
Development |
开发 |
1000 |
2000 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
20 |
30 |
|
· Design Spec |
· 生成设计文档 |
10 |
10 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
5 |
5 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
3 |
5 |
|
· Design |
· 具体设计 |
10 |
20 |
|
· Coding |
· 具体编码 |
800 |
1600 |
|
· Code Review |
· 代码复审 |
10 |
40 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
15 |
40 |
|
Reporting |
报告 |
10 |
30 |
|
· Test Report |
· 测试报告 |
5 |
10 |
|
· Size Measurement |
· 计算工作量 |
1 |
1 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
5 |
15
|
六.不足与改进。
目前的代码还很简单,甚至在算法方面还欠缺不少。由于对Java语言的不熟悉导致花费较长时间温习。此次本想运用的建栈由于考虑不全导致现在还有许多bug。这次提交的作业运用的方法比较简单,但不适用于太多操作符和运算数的情况。Java需要加强,代码能力也需要继续磨砺。