• 个人技术流程(四则运算)--王潮玉


    一,分析需求

    使用Java编程语言,完成一个随机生成四则运算练习的程序,可生成答案

    二、设计实现

    建一个Main类,写实现功能的代码

    三、代码部分

    package org;
    
    import java.util.Stack;
    
    public class Main {
    
        private static String[] op = { "+", "-", "*", "/" };// Operation set
        public static void main(String[] args) {
            String question = MakeFormula();
            System.out.println(question);
            String ret = Solve(question);
            System.out.println(ret);
        }
    
        public static String MakeFormula(){
            StringBuilder build = new StringBuilder();
            int count = (int) (Math.random() * 2) + 1; 
    
    // generate random count
            int start = 0;
            int number1 = (int) (Math.random() * 99) + 1;
            build.append(number1);
            while (start <= count){
                int operation = (int) (Math.random() * 3); 
    
    // generate operator
                int number2 = (int) (Math.random() * 99) + 1;
                build.append(op[operation]).append(number2);
                start ++;
            }
            return build.toString();
        }
    
        public static String Solve(String formula){
            Stack<String> tempStack = new Stack<>();//Store number or operator
            Stack<Character> operatorStack = new Stack<>();//Store operator
            int len = formula.length();
            int k = 0;
            for(int j = -1; j < len - 1; j++){
                char formulaChar = formula.charAt(j + 1);
                if(j == len - 2 || formulaChar == '+' || formulaChar == '-' || formulaChar == '/' || formulaChar == '*') {
                    if (j == len - 2) {
                        tempStack.push(formula.substring(k));
                    }
                    else {
                        if(k < j){
                            tempStack.push(formula.substring(k, j + 1));
                        }
                        if(operatorStack.empty()){
                            operatorStack.push(formulaChar); //if operatorStack is empty, store it
                        }else{
                            char stackChar = operatorStack.peek();
                            if ((stackChar == '+' || stackChar == '-')
                                    && (formulaChar == '*' || formulaChar == '/')){
                                operatorStack.push(formulaChar);
                            }else {
                                tempStack.push(operatorStack.pop().toString());
                                operatorStack.push(formulaChar);
                            }
                        }
                    }
                    k = j + 2;
                }
            }
            while (!operatorStack.empty()){ // Append remaining operators
                tempStack.push(operatorStack.pop().toString());
            }
            Stack<String> calcStack = new Stack<>();
            for(String peekChar : tempStack){ // Reverse traversing of stack
                if(!peekChar.equals("+") && !peekChar.equals("-") && !peekChar.equals("/") && !peekChar.equals("*")) {
                    calcStack.push(peekChar); // Push number to stack
                }else{
                    int a1 = 0;
                    int b1 = 0;
                    if(!calcStack.empty()){
                        b1 = Integer.parseInt(calcStack.pop());
                    }
                    if(!calcStack.empty()){
                        a1 = Integer.parseInt(calcStack.pop());
                    }
                    switch (peekChar) {
                        case "+":
                            calcStack.push(String.valueOf(a1 + b1));
                            break;
                        case "-":
                            calcStack.push(String.valueOf(a1 - b1));
                            break;
                        case "*":
                            calcStack.push(String.valueOf(a1 * b1));
                            break;
                        default:
                            calcStack.push(String.valueOf(a1 / b1));
                            break;
                    }
                }
            }
            return formula + "=" + calcStack.pop();
        }
    }

    运行结果:

    四、psp时间表

    PSP阶段

    计划完成时间

    实际完成时间

    计划

    8

    15

    ·明确需求和其他相关元素,估计每个阶段的时间成本

    8

    15

    开发

    88

    85

    ·需求分析

    10

    10

    ·生成设计文档

    6

    8

    ·设计复审

    6

    8

    ·代码规范

    5

    10

    ·具体设计

    15

    30

    ·具体编码

    60

    80

    ·代码复审

    10

    3

    ·测试

    5

    3

    报告

    10

    8

    ·测试报告

    3

    2

    ·设计工作量

    2

    2

    ·事后总结,并提出过程改进计划

    3

    2

    五、总结

    一开始拿到这个题目觉得不是特别难,但是上手的时候发现于想象中的不同,完全按照流程开发比较有逻辑,按部就班,显得不会那么乱,思路清晰。

  • 相关阅读:
    JavaScript解析顺序和变量作用域
    JS-BOM
    原生对象-Array
    JavaScript01
    css3动画
    scc的使用
    CSS3学习总结
    Js数组方法大全
    JavaScript判断变量是否为数组
    浏览器兼容性问题及解决方案
  • 原文地址:https://www.cnblogs.com/tqz521127/p/14644960.html
Copyright © 2020-2023  润新知