⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expframe.java

📁 本软件是使用java 开发的
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      case 6:
        codePanel.highlight(8);
        String top = OPTR.peek().toString();
        int label = precede(top, temporary);
        if (label == -1) {
          codePart = 7;
        }
        else if (label == 0) {
          codePart = 8;
        }
        else if (label == 1) {
          codePart = 9;
        }
        else if (label == 2) {
          codePart = 10;
        }
        break;
      case 7:
        codePanel.highlight(9);
        codePart = 71;
        break;
      case 71:
        codePanel.highlight(10);
        OPTR.push(temporary);
        append("操作符 " + temporary + " 入栈");
        optrCount++;
        pushMoving(temporary, 1);
        temporary = exp[tag];
        tag++;
        codePart = 3;
        break;
      case 8:
        codePanel.highlight(11);
        codePart = 81;
        break;
      case 81:
        codePanel.highlight(12);
        String t = OPTR.pop().toString();
        append("操作符 " + t + " 出栈");
        optrCount--;
        popMoving(t, 1);
        temporary = exp[tag];
        tag++;
        codePart = 3;
        break;
      case 9:
        codePanel.highlight(13);
        codePart = 91;
        break;
      case 91:
        codePanel.highlight(14);
        String m1 = OPEN.pop().toString();
        append("操作数 " + m1 + " 出栈");
        openCount--;
        popMoving(m1, 2);
        b = Float.valueOf(m1).floatValue();
        jTextField6.setText("  " + String.valueOf(b));
        codePart = 92;
        break;
      case 92:
        codePanel.highlight(15);
        String m2 = OPEN.pop().toString();
        append("操作数 " + m2 + " 出栈");
        openCount--;
        popMoving(m2, 2);
        a = Float.valueOf(m2).floatValue();
        jTextField8.setText("  " + String.valueOf(a));
        codePart = 93;
        break;
      case 93:
        codePanel.highlight(16);
        theta = OPTR.pop().toString();
        jTextField10.setText("  " + theta);
        append("操作符 " + theta + " 出栈");
        optrCount--;
        popMoving(theta, 1);
        codePart = 94;
        break;
      case 94:
        codePanel.highlight(17);
        result = Operate(a, theta, b);
        jLabel3.setText("  " + String.valueOf(result));
        append("操作数 " + String.valueOf(result) + " 入栈");
        openCount++;
        pushMoving(String.valueOf(result), 2);
        OPEN.push(String.valueOf(result));
        codePart = 3;
        break;
      case 10:

        //当表达式的运算数不合法时弹出一个出错对话框
        JOptionPane.showConfirmDialog(this,
                                      "   输入的表达式不合法\n   请从新输入正确的表达式\n",
                                      "出错信息", JOptionPane.DEFAULT_OPTION);
        doneFlag = true;
        break;
      case 11:
        codePanel.highlight(18);
        codePart = 12;
        break;
      case 12:
        codePanel.highlight(19);
        if (! (OPEN.empty())) {
          result = Float.valueOf(OPEN.peek().toString()).floatValue();
        }
        else {
          JOptionPane.showConfirmDialog(this,
                                        "   输入的表达式不合法\n   请从新输入正确的表达式\n",
                                        "出错信息", JOptionPane.DEFAULT_OPTION);

        }
        doneFlag = true;
        break;
    }
  }

  //函数initAnim()用于完成显示栈操作动画演示标签的初始化
  public void initAnim() {
    optrJLabel = new JLabel[40];
    openJLabel = new JLabel[40];
    for (int i = 0; i < optrJLabel.length; i++) {
      optrJLabel[i] = new JLabel();
      optrJLabel[i].setSize(94, 20);
      optrJLabel[i].setOpaque(true);
      optrJLabel[i].setLocation(basePoint.x + 3, basePoint.y);
      optrJLabel[i].setBackground(new Color(105, 190, 171));
      optrJLabel[i].setVisible(false);
      jPanel5.add(optrJLabel[i]);
    }
    for (int j = 0; j < openJLabel.length; j++) {
      openJLabel[j] = new JLabel();
      openJLabel[j].setSize(94, 20);
      openJLabel[j].setOpaque(true);
      openJLabel[j].setLocation(basePoint.x + 123, basePoint.y);
      openJLabel[j].setBackground(new Color(105, 190, 171));
      openJLabel[j].setVisible(false);
      jPanel5.add(openJLabel[j]);
    }
  }

  //函数In(char c)用于判断c是否为运算符,是返回false,否则返回true
  public boolean In(String c) {
    for (int i = 0; i < oper; i++) {
      if (c.equals(operator[i]))
        return false;
    }
    return true;
  }

  //pushMoving()用于显示操作符和操作树进栈的动画演示
  public void pushMoving(String name, int flag1) {
    runButton.setEnabled(false);
    stepButton.setEnabled(false);
    if (flag1 == 1) {
      this.optrJLabel[optrCount - 1].setText("            " + name);
      for (int i = 0; i < 25 - optrCount * 2; i++) {
        optrJLabel[optrCount -
            1].setLocation(basePoint.x + 3,
                           basePoint.y + i * 10 - optrCount * 1);
        optrJLabel[optrCount - 1].setVisible(true);
        try {
          int step1 = controlsPanel.delay / 30;
          Thread.sleep(step1);
        }
        catch (InterruptedException e) {}
      }
      optrTop.setOpaque(true);
      optrTop.setVisible(true);
      optrTop.setLocation(basePoint.x + 3,
                          basePoint.y + (12 - optrCount - 1) * 20 -
                          (optrCount + 1) * 1);
    }
    else if (flag1 == 2) {
      this.openJLabel[openCount - 1].setText("            " + name);
      for (int i = 0; i < 25 - openCount * 2; i++) {
        openJLabel[openCount -
            1].setLocation(basePoint.x + 123,
                           basePoint.y + i * 10 - openCount * 1);
        openJLabel[openCount - 1].setVisible(true);
        try {
          int step2 = controls = controlsPanel.delay / 30;
          Thread.sleep(step2);
        }
        catch (InterruptedException e) {}
      }
      openTop.setOpaque(true);
      openTop.setVisible(true);
      openTop.setLocation(basePoint.x + 123,
                          basePoint.y + (12 - openCount - 1) * 20 -
                          (openCount + 1) * 1);

    }
    runButton.setEnabled(true);
    stepButton.setEnabled(true);

  }

  //popMoving()用于显示操作符和操作树出栈的动画演示
  public void popMoving(String name, int flag1) {
    runButton.setEnabled(false);
    stepButton.setEnabled(false);
    if (flag1 == 1) {
      this.optrJLabel[optrCount].setText("            " + name);
      for (int i = 0; i < 25 - optrCount * 2; i++) {
        if (i > 0)
          optrTop.setVisible(false);
        optrJLabel[optrCount].setLocation(basePoint.x + 3,
                                          240 - optrCount * 20 - i * 10);
        optrJLabel[optrCount].setVisible(true);

        try {
          int step1 = controls = controlsPanel.delay / 30;
          Thread.sleep(step1);
        }
        catch (InterruptedException e) {}

      }
      optrJLabel[optrCount].setVisible(false);
      if (optrCount != 0) {
        optrTop.setVisible(true);
      }
      optrTop.setOpaque(true);
      optrTop.setLocation(basePoint.x + 3,
                          basePoint.y + (12 - optrCount - 1) * 20 -
                          (optrCount + 1) * 1);
    }
    else if (flag1 == 2) {
      this.openJLabel[openCount].setText("            " + name);
      for (int i = 0; i < 25 - openCount * 2; i++) {
        if (i > 0)
          openTop.setVisible(false);
        openJLabel[openCount].setLocation(basePoint.x + 123,
                                          240 - openCount * 20 - i * 10);
        openJLabel[openCount].setVisible(true);
        try {
          int step1 = controlsPanel.delay / 30;
          Thread.sleep(step1);
        }
        catch (InterruptedException e) {}
      }
      openJLabel[openCount].setVisible(false);
      if (openCount != 0) {
        openTop.setVisible(true);
      }
      openTop.setOpaque(true);
      openTop.setLocation(basePoint.x + 123,
                          basePoint.y + (12 - openCount - 1) * 20 -
                          (openCount + 1) * 1);

    }
    runButton.setEnabled(true);
    stepButton.setEnabled(true);
  }

  public void run() {

    while (Thread.currentThread() == runner) {
      if (runFlag) {
        EvaluateExpression();
        if (doneFlag) {
          if (codePart != 10) {
            jLabel3.setText(String.valueOf(result));
          }
          runFlag = false;
          movFlag = false;
          runButton.setEnabled(false);
          stepButton.setEnabled(false);
          JOptionPane.showConfirmDialog(this,
                                        "    算法演示完成!\n  \n 单击相关按钮重新开始\n",
                                        "演示完成", JOptionPane.DEFAULT_OPTION);

          stop(3);
        }
        try {
          controls = controlsPanel.delay;
          Thread.sleep(controls);
        }
        catch (InterruptedException _ex) {}
      }
    }

    while (Thread.currentThread() == moving) {
      if (movFlag) {
        EvaluateExpression();
        if (doneFlag) {
          if (codePart != 10) {
            jLabel3.setText(String.valueOf(result));
          }
          runFlag = false;
          movFlag = false;
          runButton.setEnabled(false);
          stepButton.setEnabled(false);
          JOptionPane.showConfirmDialog(this,
                                        "    算法演示完成!\n  \n 单击相关按钮重新开始\n",
                                        "演示完成", JOptionPane.DEFAULT_OPTION);

          stop(3);
        }
        stop(2);
        movFlag = false;
      }
    }
  }

  //终止线程
  public void stop(int flag1) {
    if (flag1 == 1) {
      if (runner != null) {
        runner.yield();
        runner = null;
      }
    }
    else if (flag1 == 2) {
      if (moving != null) {
        moving.yield();
        moving = null;
      }
    }
    else {
      if (runner != null) {
        runner.yield();
        runner = null;
      }
      if (moving != null) {
        moving.yield();
        moving = null;
      }
    }
  }

  //函数append()用于向记录栈操作的文本框中添加栈的操作步骤
  public void append(String s) {
    recordwork.append(" " + s + "\n");
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -