目录
1.作业的基本信息
作业属于课程 | 软件工程四班 |
---|
作业的要求 | 结对编程:小学四则运算 |
作业的目标 | 实现一个自动生成小学四则运算题目的程序 |
2.作业地址
3121005261 gitcode地址
3.PSP表格
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|
Planning | 计划 | 30 | 60 |
Estimate | 估计这个任务需要多少时间 | 120 | 120 |
Development | 开发 | 90 | 90 |
Analysis | 需求分析 (包括学习新技术) | 90 | 150 |
Design Spec | 生成设计文档 | 60 | 70 |
Coding Standard | 设计复审 | 40 | 45 |
Design | 代码规范 (为目前的开发制定合适的规范) | 20 | 20 |
Design Review | 具体设计 | 50 | 50 |
Coding | 具体编码 | 250 | 330 |
Code Review | 代码复审 | 40 | 60 |
Test | 测试(自我测试,修改代码,提交修改) | 40 | 40 |
Reporting | 报告 | 120 | 120 |
Test Repor | 测试报告 | 60 | 60 |
Size Measurement | 计算工作量 | 30 | 30 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 30 | 60 |
Total | 合计 | 1250 | 1440 |
4.效能分析
4.1 JProfiler分析
5.类与函数说明
5.1 关键算法逆波兰算法的流程图与代码实现
类 | 方法 |
---|
Main(主函数调试) | main() |
Creat(随机数生成式子) | single |
| create |
file (文件处理) | file_inArry |
| file_inString |
| file_out() |
Reverse(逆波兰算法求答案) | cal (逆波兰算法:中缀算式分栈变后缀,最后靠优先级计算直到变为一个栈顶元素) |
Transform(小数转换真分式) | gcd(求最大公约数) |
| transform(转换为真分式) |
5.2关键算法逆波兰算法的流程图与代码实现
逆波兰(后缀)算法简介与流程图:
引用:逆波兰算法实现
6.代码说明
package calculation;
public class Create {
String[] fuHao;
int m;
int n;
String[] strArray;
public Create(String[] fuHao, int m, int n, String[] strArray) {
super();
this.fuHao = fuHao;
this.m = m;
this.n = n;
this.strArray = strArray;
}
public String single(int m,String[] fuHao)
{
String str;
int first_number;
int second_number;
int order = (int)(Math.random()*5);
first_number = (int)(Math.random()*m+1);
second_number = (int)(Math.random()*m+1);
if(Math.random()*3>1&&(fuHao[order]==" + "||fuHao[order]==" - ")){
str ="(" + first_number + fuHao[order] + second_number + ")";
}
else {
str = first_number + fuHao[order] + second_number;
}
return str;
}
public void create(int m, int n, String[] fuHao, String[] strArray) {
String str = "";
int i=0;
outer:
while(true) {
str = "";
int first_number;
int second_number;
int lastNumber;
lastNumber = (int)(Math.random()*m+1);
for(int j = 0; j < 2; j++) {
int order = (int)(Math.random()*5);
str = str + single(m, fuHao) + fuHao[order];
}
str = str + lastNumber + " = ";
if(Reverse.cal(str).charAt(0)=='-')
{
continue outer;
}
strArray[i] = str;
System.out.println("第"+(i+1)+"题:"+str);
if(i==9)
{
break outer;
}
i++;
}
}
public static void main(String[] args) {
String[] fuhao = {"+","-","*","/","÷"} ;
int n=10;
int m=50;
String[] strArry = new String[n];
Create creat = new Create(fuhao, m, n, strArry);
creat.create(m, n, fuhao, strArry);
}
}
package calculation;
import java.util.List;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Stack;
public class Reverse {
public static String cal(String str) {
List<String> list = new ArrayList<>();
str.replace(" ", "");
str.replace(" = ", "");
char[] arr = str.toCharArray();
StringBuffer tmpStr = new StringBuffer();
for (char c : arr) {
if (c>='0' && c<='9') {
tmpStr.append(c);
}else if(c=='.') {
tmpStr.append(c);
}
else if (c=='+' || c=='-' || c=='*' || c=='/' || c=='(' || c==')'||c=='÷') {
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
tmpStr.setLength(0);
}
list.add(c + "");
}
else if (c==' ') {
continue;
}
}
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
}
List<String> strList = new ArrayList<>();
Stack<String> stack = new Stack<>();
String tmp;
for (String s : list) {
if (s.equals("(")) {
stack.push(s);
}
else if (s.equals(")")) {
while (!(tmp = stack.pop()).equals("(")) {
strList.add(tmp);
}
}
else if (s.equals("*") || s.equals("/")||s.equals("÷")) {
while(!stack.isEmpty()) {
tmp = stack.peek();
if (tmp.equals("*") || tmp.equals("/")||tmp.equals("÷")) {
stack.pop();
strList.add(tmp);
}
else {
break;
}
}
stack.push(s);
}
else if (s.equals("+") || s.equals("-")) {
while(!stack.isEmpty()) {
tmp = stack.peek();
if (!tmp.equals("(")) {
stack.pop();
strList.add(tmp);
}
else {
break;
}
}
stack.push(s);
}
else {
strList.add(s);
}
}
while (!stack.isEmpty()) {
strList.add(stack.pop());
}
Stack<BigDecimal> newStack = new Stack<>();
for (String s : strList) {
if (s.equals("*") || s.equals("/") || s.equals("+") || s.equals("-")||s.equals("÷")) {
BigDecimal b1 = newStack.pop();
BigDecimal b2 = newStack.pop();
switch (s) {
case "+":
newStack.push(b2.add(b1));
break;
case "-":
newStack.push(b2.subtract(b1));
break;
case "*":
newStack.push(b2.multiply(b1));
break;
case "/":
newStack.push(b2.divide(b1, 4, BigDecimal.ROUND_HALF_UP));
break;
case "÷":
newStack.push(b2.divide(b1, 4, BigDecimal.ROUND_HALF_UP));
break;
}
}
else {
newStack.push(new BigDecimal(s));
}
}
return newStack.peek().toString();
}
}
package calculation;
public class Transform {
String result;
public Transform(String s) {
super();
this.result=transform(s);
}
public static int gcd(int a,int b){
return b==0 ? a:gcd(b,a%b) ;
}
public static String transform(String s)
{
String result="";
int decimal;
int a, b,c;
int g;
String[] array = new String[2];
array = s.split("\\.");
a = Integer.parseInt(array[0]);
decimal = Integer.parseInt(array[1]);
int length = array[1].length();
b = decimal;
c = (int)Math.pow(10, length);
g = gcd(b,c);
result =a+" ' "+b/g+"|"+c/g;
return result;
}
}
package calculation;
import java.io.IOException;
import java.util.function.DoubleConsumer;
public class Main {
public static void main(String[] args) throws IOException {
String[] fuhao = {" + "," - "," * "," / "," ÷ "} ;
int n=10;
int m=50;
int score=0;
String[] str1 = new String[n];
String[] str2 = new String[n+1];
Create creat = new Create(fuhao, m, n, str1);
creat.create(m, n, fuhao, str1);
Reverse answer = new Reverse();
int i=0;
for(String s : str1)
{
str2[i]=answer.cal(s);
if(str2[i].indexOf(".")!=-1)
{
str2[i]=Transform.transform(str2[i]);
}
i++;
}
file qf = new file("D:\\eclipse-workspace\\calculation\\txt_file\\question.txt");
file af = new file("D:\\eclipse-workspace\\calculation\\txt_file\\answer.txt");
file you = new file("D:\\eclipse-workspace\\calculation\\txt_file\\you.txt");
String[] str3 =new String[n];
str3=you.content.split("\n");
for(int j=0;i<n;i++)
{
if(str2[j]==str3[j])
{
score++;
}
}
str2[n]=String.valueOf(score)+"分";
qf.file_inArry(str1);
af.file_inArry(str2);
}
}
7. 测试运行
运算题展示
输入展示
答案展示
项目小结
在这个结对项目中,我完成了一个基于Java的自动生成小学四则运算题目的命令行程序。通过这个项目,我学习到了很多新的技能和知识,也经历了许多成功和失败的时刻。
在项目完成之前,我首先遇到了一些技术难题,例如如何查重题目并去重、如何处理一些特殊情况等等。但是我通过不断学习,最终克服了这些问题,并成功实现了项目的目标。同时,在项目完成后,我也回顾了整个项目的过程,并总结了一些得失和教训。
首先,我认为这个项目的最大成功在于我的合作和沟通。在整个项目过程中,我保持了密切的联系,时刻互相了解对方的工作进度和遇到的问题,及时进行了交流和协作,从而确保了项目的顺利完成。其次,我也注意到了一些需要改进的地方,例如项目计划和时间管理方面还有待提高,代码质量方面还有需要改进的地方等等。