标签:buffer 规范 代码规范 核心 规划 else 总结 大致 多少
四则运算编程练习
需求分析:根据用户输入,随机生成相应个数的表达式。
核心代码:
step1:生成表达式并存入StringBuffer中
1 private static String[] op = { "+", "-", "*", "/" }; 2 /** 3 * 生成算数表达式 4 * 5 * @return 运算表达式(无结果) 6 */ 7 public static String MakeFormula() { 8 StringBuilder build = new StringBuilder(); 9 // 生成区间范围[1,2],随机生成三位or四位的操作运算 10 int count = (int) (Math.random() * 2) + 1; // generate random count 11 int start = 0; 12 int number1 = (int) (Math.random() * 99) + 1; 13 build.append(number1); 14 while (start <= count) { 15 // 生成区间为[0,3] 16 int operation = (int) (Math.random() * 4); // generate operator(生成操作符) 17 int number2 = (int) (Math.random() * 99) + 1; 18 build.append(op[operation]).append(number2); 19 start++; 20 } 21 return build.toString(); 22 }
step2:根据生成的表达式,计算运算符的优先顺序,并生成结果
1 public static String Solve(String formula) { 2 // stack堆栈 3 Stack<String> tempStack = new Stack<>();// Store number or operator 4 // Character表示基本类型 char 的 Class 实例。 5 Stack<Character> operatorStack = new Stack<>();// Store operator 6 int len = formula.length(); 7 int k = 0; 8 for (int j = -1; j < len - 1; j++) { 9 // charAt: 返回此序列中指定索引处的 char 值。 10 char formulaChar = formula.charAt(j + 1); 11 if (j == len - 2 || formulaChar == ‘+‘ || formulaChar == ‘-‘ || formulaChar == ‘/‘ || formulaChar == ‘*‘) { 12 if (j == len - 2) { 13 // subString:返回一个新的 String ,该子字符串始于指定索引处的字符,一直到此字符串末尾。 14 tempStack.push(formula.substring(k)); 15 } else { 16 if (k < j) { 17 tempStack.push(formula.substring(k, j + 1)); 18 } 19 // 判断是否为空 20 if (operatorStack.empty()) { 21 operatorStack.push(formulaChar); // if operatorStack is empty, store it 22 } else { 23 char stackChar = operatorStack.peek(); 24 if ((stackChar == ‘+‘ || stackChar == ‘-‘) && (formulaChar == ‘*‘ || formulaChar == ‘/‘)) { 25 operatorStack.push(formulaChar); 26 } else { 27 tempStack.push(operatorStack.pop().toString()); 28 operatorStack.push(formulaChar); 29 } 30 } 31 } 32 k = j + 2; 33 } 34 } 35 while (!operatorStack.empty()) { // Append remaining operators 36 tempStack.push(operatorStack.pop().toString()); 37 } 38 Stack<String> calcStack = new Stack<>(); 39 for (String peekChar : tempStack) { // Reverse traversing of stack 40 if (!peekChar.equals("+") && !peekChar.equals("-") && !peekChar.equals("/") && !peekChar.equals("*")) { 41 calcStack.push(peekChar); // Push number to stack 42 } else { 43 int a1 = 0; 44 int b1 = 0; 45 if (!calcStack.empty()) { 46 b1 = Integer.parseInt(calcStack.pop()); 47 } 48 if (!calcStack.empty()) { 49 a1 = Integer.parseInt(calcStack.pop()); 50 } 51 switch (peekChar) { 52 case "+": 53 calcStack.push(String.valueOf(a1 + b1)); 54 break; 55 case "-": 56 calcStack.push(String.valueOf(a1 - b1)); 57 break; 58 case "*": 59 calcStack.push(String.valueOf(a1 * b1)); 60 break; 61 default: 62 calcStack.push(String.valueOf(a1 / b1)); 63 break; 64 } 65 } 66 } 67 return formula + "=" + calcStack.pop(); 68 }
step3:提示用户输入数字,并生成表达式
1 import java.util.Scanner; 2 3 import org.junit.Test; 4 5 import demo.demo1; 6 7 public class TestCase { 8 @Test 9 public void test01() { 10 System.out.println("请输入表达式的个数:"); 11 Scanner scanner = new Scanner(System.in); 12 int num = scanner.nextInt(); 13 int i = 0; 14 while (i < num) { 15 String question = demo1.MakeFormula(); 16 String ret = demo1.Solve(question); 17 System.out.println(ret); 18 i++; 19 } 20 } 21 22 }
测试结果如下:
PSP开发流程:
PSP2.1 | 任务内容 | 计划共完成需要的时间(min) | 实际完成需要的时间(min) |
---|---|---|---|
Planning | 计划 | 20 | 25 |
· Estimate | · 估计这个任务需要多少时间,并规划大致工作步骤 | 20 | 25 |
Development | 开发 | 410 | 560 |
· Analysis | 需求分析 (包括学习新技术) | 15 | 20 |
· Design Spec | · 生成设计文档 | 15 | 15 |
· Design Review | · 设计复审 (和同事审核设计文档) | 5 | 10 |
· Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 10 | 20 |
· Design | 具体设计 | 20 | 25 |
· Coding | 具体编码 | 260 | 350 |
· Code Review | · 代码复审 | 40 | 60 |
· Test | · 测试(自我测试,修改代码,提交修改) | 45 | 60 |
Reporting | 报告 | 35 | 45 |
· Test Report | · 测试报告 | 15 | 20 |
· Size Measurement | 计算工作量 | 10 | 10 |
· Postmortem & Process Improvement Plan | · 事后总结 ,并提出过程改进计划 | 10 | 15 |
标签:buffer 规范 代码规范 核心 规划 else 总结 大致 多少
原文地址:https://www.cnblogs.com/02xytx/p/14643637.html