个人项目实战-四则混合运算
coding.net地址 :https://git.dev.tencent.com/qyj814/fours.git
一.实验要求
基本任务:
使用JAVA编程语言,独立完成一个3到5个运算符的四则运算练习的软件。
软件基本功能要求如下:
- 程序可接收一个输入参数n,然后随机产生n道加减乘除(分别使用符号+-*÷来表示)练习题,每个数字在 0 和 100 之间,运算符在3个到5个之间。
- 每个练习题至少要包含2种运算符。同时,由于小学生没有分数与负数的概念,你所出的练习题在运算过程中不得出现负数与非整数,比如不能出 3÷5+2=2.6,2-5+10=7等算式。
- 练习题生成好后,将你的学号与生成的n道练习题及其对应的正确答案输出到文件“result.txt”中,不要输出额外信息,文件目录与程序目录一致。
- 当程序接收的参数为4时,以下为一个输出文件示例。
2018010203
13+17-1=29
11*15-5=160
3+10+4-16=1
15÷5+3-2=4
软件附加功能要求如下:(请有余力的同学完成)
- 支持有括号的运算式,包括出题与求解正确答案。注意,算式中存在的括号数必须大于2对,且不得超过运算符的个数。
- 扩展程序功能支持真分数的出题与运算(只需要涵盖加减法即可),例如:1/6 + 1/8 + 2/3= 23/24。注意在实现本功能时,需支持运算时分数的自动化简,比如 1/2+1/6=2/3,而非4/6,且计算过程中与结果都须为真分数。
二.操作步骤
首先,分析题意,做基本的需求分析,总体设计等;其次,根据详细设计编写代码并进行测试;最后,上传文件到coding.net上。
三.代码分析
主要文件
1.创建整数核心代码
public String[] createProblem(){ Random random = new Random(); int operatorCount =3 + random.nextInt(3); //随机操作符的个数(3-5个) int operand[] = new int[operatorCount + 1]; //操作数个数 int[] operatorIndex = index(operatorCount, 4, random); for(int i = 0; i < operatorCount + 1; i++){ operand[i] = random.nextInt(101);//随机生成(0-100)之间的数 } String formula = stitchingFormula(operatorCount, operand, operatorIndex); //计算结果 Calculator calculator = new Calculator(); int res = calculator.algorithm(formula); String formulaRes[] = new String[2]; if (res > 0){ formulaRes[0] = formula; formulaRes[1] = String.valueOf(res); }else { return createProblem(); } return formulaRes; }
2.创建分数核心代码
public String[] createProblem(){ Random random = new Random(); int operatorCount = 1 + random.nextInt(3); //操作符的个数(1-3)个 CreateInteger create = new CreateInteger(); int[] operatorIndex = create.index(operatorCount,2, random); //操作符的下标 //生成第一个操作数 int[] coprimeNumber1 = createCoprimeNumbers(random); int x = coprimeNumber1[0]; int y = coprimeNumber1[1]; String s = x + "/" + y; for(int i=0; i < operatorCount; i++){ //生成剩下的操作数 int[] coprimeNumber = createCoprimeNumbers(random); int numx = coprimeNumber[0]; int numy = coprimeNumber[1]; String currentOpreator = OPERATOR[operatorIndex[i]]; if(currentOpreator.equals("+")){ //加法 int count = 0; if(x * numy + y * numx > y * numy) { numx = y - x - 1; numy = y; if(numx <= 0) numx = 1; numy = 2 * y; } x = x * numy + y * numx; y = y * numy; int greatFactor = greatFactor(numx,numy); numx /= greatFactor; //最终结果化简 numy /= greatFactor; } else { //减法 if(x * numy - y * numx < 0){ numx = x -1; numy = y; if(numx <= 0) numx = 1; numy = 2*y; } x = x * numy - y * numx; y = y * numy; int greatFactor = greatFactor(numx,numy); numx /= greatFactor; //最终结果化简 numy /= greatFactor; } String num = numx + "/" + numy; s += currentOpreator + num; } int greatFactor = greatFactor(x,y); x /= greatFactor; //最终结果化简 y /= greatFactor; String res = shamToProperFraction(x, y); s += "="; String formulaRes[] = {s, res}; return formulaRes; }
3.计算核心代码
private int calculate(int a, int b, String stmp) { //计算a stmp b的值 int res = 0; //存结果 char s = stmp.charAt(0); switch (s) { case '+': { res = a + b; break; } case '-': { res = a - b; //产生负数就不合格 break; } case '*': { res = a * b; break; } case '÷': { if(b==0) return -1; else if(a%b!=0) //产生小数就不合格 return -2; else res = a / b; break; } } return res; }
4.生成问题并输出到文件核心代码
public void generateProblem(int num) throws IOException { //项目根目录生成文件 File file = new File("./result.txt"); if(file.exists()) { file.delete(); file.createNewFile(); } FileOutputStream fileOutput = new FileOutputStream(file); PrintStream filePrintStream = new PrintStream(fileOutput); Random random = new Random(); CreateFraction createFraction = new CreateFraction(); CreateInteger createInteger = new CreateInteger(); String[] problem = new String[2]; filePrintStream.println("2017012454"); for(int i = 1; i <= num; i++){ int choose = random.nextInt(2); //选择生成分数还是整数 if (choose == 0){ problem = createInteger.createProblem(); }else { problem = createFraction.createProblem(); // problem = createInteger.createProblem(); } outputFile(problem, filePrintStream); // System.out.println(i); } fileOutput.close(); filePrintStream.close(); System.out.println("文件创建成功 "); }
四.PSP分析
PSP2.1 | 任务内容 | 计划共完成需要的时间(h) | 实际完成需要的时间(h) |
Planning | 计划 | 1 | 2 |
Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 0 | 0 |
Development | 开发 | 60 | 73 |
Analysis | 需求分析 (包括学习新技术) | 0 | 0 |
Design Spec | 生成设计文档 | 0 | 0 |
Design Review | 设计复审 (和同事审核设计文档) | 0 | 0 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 0 | 0 |
Design | 具体设计 | 0 | 0 |
Coding | 具体编码 | 0 | 0 |
Code Review | 代码复审 | 0 | 0 |
Test | 测试(自我测试,修改代码,提交修改) | 0 | 0 |
Reporting | 报告 | 1 | 2 |
Test Report | 测试报告 | 0 | 0 |
Size Measurement | 计算工作量 | 0 | 0 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 0 | 0 |
五.心得体会
这个项目我用了两个周末,外加两周内零零散散的时间,一共用了七十多个小时,即使这样,我的代码还是有很多不足,比如说:生成整数问题时例子太少(由于本人实在写不下去了,都快写吐了)。要说我这两周写代码以来最大的体会,就是感觉自己知识学的还是不到位,遇到不懂的问题就得上网去查,一查又浪费了很多时间;写出来的代码也不是准确的,debug又花好多时间;总而言之,做完这个项目让我觉得我要好好学习了。