本次作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2166
一、题目要求:
像《构建之法》的人物阿超那样,写一个能自动生成小学四则运算题目的命令行 “软件”。
具体要求:任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。例如输入数字是 30, 那就输出 30 道题目和答案。 运算式子必须至少有两个运算符,运算数字是在 100 之内的正整数,答案不能是负数。 如:
23 - 3 * 4 = 11
二、需求分析:
1) 要求能出和真分数 (二分之一, 十二分之五,等)相关的练习题。
2) 并且要求能处理用户的输入,并判断对错,打分统计。 要求能处理用户输入的真分数, 如 1/2, 5/12 等。
初步拟定要实现的功能后,估计一下自己需要花多长时间。编程过程中记录自己实际用了多长时间。
然后和同学们比较一下各自程序的功能、实现方法的异同等等。
写博客纪录自己实现的过程和思路。
三、代码提交
1.定制运算数和运算符
/** 用于生成指定范围的随机数 **/
static private int makeRandom(int min, int max){
return (int)(Math.random()*(max - min) + min + 0.5);
}
首先在Lib类中,我选择了这样一个方法,通过Math.random()方法,生成一个 [min, max] 范围中的数。
2.生成随机的运算式
static public void makeQuestions(String[] questionList, int sum, int ops){
/** 用于存放操作符 和 操作数 **/
char opInQuestion[] = new char[15];
int numInQuestion[] = new int[15];
/** for循环生成sum个question **/
for(int i=0; i<sum; i++){
numInQuestion[0] = makeRandom(1, 100);
/** 每个问题预生成第一个操作数,然后for循环对应生成之后的ops个操作 和对应的操作数 **/
for(int j=0; j<ops; j++){
/** 乘除不连续出现避免出错 **/
if(j!=0 && getw(opInQuestion[j-1])==2)
opInQuestion[j] = op[makeRandom(0, 1)];
else opInQuestion[j] = op[makeRandom(0, 3)];
numInQuestion[j+1] = makeRandom(1, 50);
/** 特判除法构造整除 **/
if(opInQuestion[j] == '÷') {
numInQuestion[j+1] = makeRandom(1, 10);
numInQuestion[j] = numInQuestion[j+1]*makeRandom(2, 10);
}
/** 特判乘法控制范围 **/
else if(opInQuestion[j] == '*') {
numInQuestion[j] = makeRandom(1, 20);
numInQuestion[j+1] = makeRandom(1, 100/numInQuestion[j]);
}
}
/** 将问题拼接为String **/
String question = "" + numInQuestion[0];
for(int j=0; j<ops; j++)
question = question + opInQuestion[j] + numInQuestion[j+1];
//System.out.println(question);
question = question + "=" + calQuestion(question);
System.out.println(question);
questionList[i] = question;
}
}
3.计算表达式的值。
/** 将question转化为后缀表达式并求值 **/
static private int calQuestion(String question){
char[] ch = new char[50];
char[] oc = new char[15];
int[] num = new int[15];
int temp = 0, pn = -1, pc = -1; //temp用于存放中间数值,pn用于在num数组中模拟栈顶, pc用于在ch数组中模拟栈顶
ch = question.toCharArray();
for(int i=0; i<ch.length; i++){
if(Character.isDigit(ch[i])) {
temp = temp*10 + ch[i]-'0';
if(i == ch.length-1)
num[++pn] = temp; //最后一个数入栈
}
else {
num[++pn] = temp; //temp入栈
temp = 0;
while(pc!=-1 && getw(oc[pc]) >= getw(ch[i]) ) {
int num1 = num[pn--];
int num2 = num[pn--];
char ch1 = oc[pc--];
num[++pn] = calc(num2, num1, ch1);
}
//if(pc == -1) oc[++pc] = ch[i];
oc[++pc] = ch[i];
}
}
四、个人软件过程耗时估计与统计表
| PSP2.1 | Personal Software Process Stages | Time Senior Student | Time |
| Planning | 计划 | 8 | 6 |
| · Estimate | 估计这个任务需要多少时间 | 80 | 100 |
| Development | 开发 | 60 | 80 |
| · Analysis | 需求分析 (包括学习新技术) | 10 | 15 |
| · Design Spec | 生成设计文档 | 5 | 6 |
| · Design Review | 设计复审 | 6 | 6 |
| · Coding Standard | 代码规范 | 3 | 3 |
| · Design | 具体设计 | 10 | 15 |
| · Coding | 具体编码 | 30 | 25 |
| · Code Review | 代码复审 | 7 | 9 |
| · Test | 测试(自我测试,修改代码,提交修改) | 13 | 21 |
| Reporting | 报告 | 9 | 6 |
| · | 测试报告 | 3 | 2 |
| · | 计算工作量 | 2 | 2 |
| · | 并提出过程改进计划 | 3 | 3 |