第四周学习进度条

周一 周二 周三 周四 周五 周六
上午两节课的听课时间 下午2点到4点20 晚上7点到晚上8点35 晚上7点到8点 中午5点到6点
  阅读代码量约300行 编写代码量约了110行 代码量约120行(删除了原来很多的有缺陷的代码) / 代码量约110行
  查阅了JAVA的栈的问题,阅读了代码如下,尝试用栈来解决作业 解决了四则运算的第一个小问题 和搭档探讨解决第二个小问题的思路 阅读《构建之法》 和搭档解决了四则运算的第二个小问题。

查阅的JAVA的栈的资料如下:

public class Main {   
  private Stack<Character> priStack = new Stack<Character>();// 操作符栈   
  private Stack<Integer> numStack = new Stack<Integer>();;// 操作数栈   
    
  /**  
   * 传入需要解析的字符串,返回计算结果(此处因为时间问题,省略合法性验证)  
   * @param str 需要进行技术的表达式  
   * @return 计算结果  
   */  
  public int caculate(String str) {   
    // 1.判断string当中有没有非法字符   
    String temp;// 用来临时存放读取的字符   
    // 2.循环开始解析字符串,当字符串解析完,且符号栈为空时,则计算完成   
    StringBuffer tempNum = new StringBuffer();// 用来临时存放数字字符串(当为多位数时)   
    StringBuffer string = new StringBuffer().append(str);// 用来保存,提高效率   
    
    while (string.length() != 0) {   
      temp = string.substring(0, 1);   
      string.delete(0, 1);   
      // 判断temp,当temp为操作符时   
      if (!isNum(temp)) {   
        // 1.此时的tempNum内即为需要操作的数,取出数,压栈,并且清空tempNum   
        if (!"".equals(tempNum.toString())) {   
          // 当表达式的第一个符号为括号   
          int num = Integer.parseInt(tempNum.toString());   
          numStack.push(num); 
          tempNum.delete(0, tempNum.length());   
        }   
        // 用当前取得的运算符与栈顶运算符比较优先级:若高于,则因为会先运算,放入栈顶;若等于,因为出现在后面,所以会后计算,所以栈顶元素出栈,取出操作数运算;   
        // 若小于,则同理,取出栈顶元素运算,将结果入操作数栈。   
    
        // 判断当前运算符与栈顶元素优先级,取出元素,进行计算(因为优先级可能小于栈顶元素,还小于第二个元素等等,需要用循环判断)   
        while (!compare(temp.charAt(0)) && (!priStack.empty())) {  
          int a = (int) numStack.pop();// 第二个运算数   
          int b = (int) numStack.pop();// 第一个运算数   
          char ope = priStack.pop();   
          int result = 0;// 运算结果   
          switch (ope) {   
          // 如果是加号或者减号,则   
          case '+':   
            result = b + a;   
            // 将操作结果放入操作数栈   
            numStack.push(result);   
            break;   
          case '-':   
            result = b - a;   
            // 将操作结果放入操作数栈   
            numStack.push(result);   
            break;   
          case '*':   
            result = b * a;   
            // 将操作结果放入操作数栈   
            numStack.push(result);   
            break;   
          case '/':   
            result = b / a;// 将操作结果放入操作数栈   
            numStack.push(result);   
            break;   
          }   
    
        }   
        // 判断当前运算符与栈顶元素优先级, 如果高,或者低于平,计算完后,将当前操作符号,放入操作符栈   
        if (temp.charAt(0) != '#') {   
          priStack.push(new Character(temp.charAt(0)));   
          if (temp.charAt(0) == ')') {// 当栈顶为'(',而当前元素为')'时,则是括号内以算完,去掉括号   
            priStack.pop();   
            priStack.pop();   
          }   
        }   
      } else  
        // 当为非操作符时(数字)   
        tempNum = tempNum.append(temp);// 将读到的这一位数接到以读出的数后(当不是个位数的时候)   
    }   
    return numStack.pop();   
  }   
    
  /**  
   * 判断传入的字符是不是0-9的数字  
   *  
   * @param str  
   *      传入的字符串  
   * @return  
   */  
  private boolean isNum(String temp) {   
    return temp.matches("[0-9]");   
  }   
    
  /**  
   * 比较当前操作符与栈顶元素操作符优先级,如果比栈顶元素优先级高,则返回true,否则返回false  
   *  
   * @param str 需要进行比较的字符  
   * @return 比较结果 true代表比栈顶元素优先级高,false代表比栈顶元素优先级低  
   */  
  private boolean compare(char str) {   
    if (priStack.empty()) {   
      // 当为空时,显然 当前优先级最低,返回高   
      return true;   
    }   
    char last = (char) priStack.lastElement();   
    // 如果栈顶为'('显然,优先级最低,')'不可能为栈顶。   
    if (last == '(') {   
      return true;   
    }   
    switch (str) {   
    case '#':   
      return false;// 结束符   
    case '(':   
      // '('优先级最高,显然返回true   
      return true;   
    case ')':   
      // ')'优先级最低,   
      return false;   
    case '*': {   
      // '*/'优先级只比'+-'高   
      if (last == '+' || last == '-')   
        return true;   
      else  
        return false;   
    }   
    case '/': {   
      if (last == '+' || last == '-')   
        return true;   
      else  
        return false;   
    }   
      // '+-'为最低,一直返回false   
    case '+':   
      return false;   
    case '-':   
      return false;   
    }   
    return true;   
  }   
    
  public static void main(String args[]) {   
    Main operate = new Main();   
    int t = operate.caculate("(3+4*(4*10-10/2)#");    
    System.out.println(t);   
  }   
    
}

工作照:

原文地址:https://www.cnblogs.com/3066405538a/p/5323607.html