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

📄 binsearch.java

📁 本软件是使用java 开发的
💻 JAVA
字号:
package datastructure;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Binsearch
    extends JPanel
    implements Runnable, ActionListener {
  private Thread runner;

  private boolean runFlag = false;
  static JButton newButton;
  static JButton runButton;
  static JButton stepButton;
  static JButton stayButton;

  //private int delay;//用来控制速度
  //控制演示速度
  private GlobalControls controlsPanel = new GlobalControls();
  private int controls;

  public CodeAnimationPanel codePanel;
  Panel panel;
  public Panel eastPanel;
  JTextField textfield00;
  JTextField textfield01;
  JTextField textfield11;
  JTextField textfield12;
  JTextField textfield21;
  JTextField textfield22;
  JTextField textfield31;
  JTextField textfield32;
  JTextField textfield41;
  JTextField textfield42;
  JTextField textfield51;
  JTextField textfield52;
  private int table[] = new int[21];

  private int count;
  private int codePart = 1;
  int low;
  int high;
  int mid;
  int searchCount;
  int drawMode = 1;
  static Label labarr[] = new Label[21];
  Label labprompt[] = new Label[21];
  Label searchlab = new Label("57");
  Label note = new Label();
  BinSearchPerson data[] = new BinSearchPerson[21];
  Panel centerpanel;
  int searchInt;
  int searchIndex;
  private boolean doneFlag = false;
  boolean expFlag = true;
  String notecon = new String();

  public Binsearch() {
    //  init();

  }

  public void init() {
    String as[] = {
        "int Search_Bin(SSTable ST,KeyType key)",
        "{  low=1;high=ST.length;",
        "   while(low<=high)",
        "   {",
        "      mid=(low+high)/2;",
        "      if(EQ(key, ST.elem[mid], key))",
        "           return mid;",
        "      else if (LT(key, ST.elem[mid].key))",
        "           high=mid-1;",
        "      else low=mid+1;",
        "    }",
        "return 0;",
        "}",
    };

    int inittable[] = {
        0, 7, 10, 14, 15, 17, 25, 35, 42, 53, 57, 67, 76, 87, 102};
    searchInt = 57;
    binPersonGroup(inittable, 14);
    // draw(g);
    controlsPanel.delay = 900;
    codePanel = new CodeAnimationPanel(as);
    this.setLayout(new BorderLayout());
    // add(codePanel,BorderLayout.EAST);
    centerpanel = new Panel();
    centerpanel.setLayout(null);

    for (int i = 1; i <= 20; i++) {
      this.labarr[i] = new Label();
      centerpanel.add(labarr[i]);
      labarr[i].setSize(25, 25);
      labarr[i].setLocation(25 * i, 200);
      this.labprompt[i] = new Label();
      centerpanel.add(labprompt[i]);
      labprompt[i].setSize(25, 25);
      labprompt[i].setLocation(25 * i, 200);
    }
    searchlab.setSize(25, 25);
    searchlab.setBackground(Color.red);
    searchlab.setLocation(25, 150);
    note.setSize(200, 20);
    note.setLocation(20, 75);
    this.centerpanel.add(searchlab);
    this.centerpanel.add(note);

    this.add(centerpanel, BorderLayout.CENTER);
    runFlag = false;

    eastPanel = new Panel();
    eastPanel.setLayout(new BorderLayout());

    panel = new Panel();
    panel.setLayout(new GridLayout(6, 2));
    eastPanel.add(codePanel, BorderLayout.NORTH);
    eastPanel.add(panel, BorderLayout.SOUTH);
    this.add(eastPanel, BorderLayout.EAST);
    textfield00 = new JTextField("变量名");
    textfield00.setEditable(false);
    textfield01 = new JTextField("变量值");
    textfield01.setEditable(false);
    textfield11 = new JTextField("low");
    textfield11.setEditable(false);
    textfield12 = new JTextField(" ");
    textfield12.setEditable(false);
    textfield21 = new JTextField("mid");
    textfield21.setEditable(false);
    textfield22 = new JTextField("");

    textfield22.setEditable(false);
    textfield31 = new JTextField("high");
    textfield31.setEditable(false);
    textfield32 = new JTextField();
    textfield32.setEditable(false);
    textfield41 = new JTextField("key");
    textfield41.setEditable(false);
    textfield42 = new JTextField();
    textfield42.setEditable(false);
    textfield51 = new JTextField("查找的次数");
    textfield51.setEditable(false);
    textfield52 = new JTextField();
    textfield52.setEditable(false);
    panel.add(textfield00);
    panel.add(textfield01);
    panel.add(textfield11);
    panel.add(textfield12);
    panel.add(textfield21);
    panel.add(textfield22);
    panel.add(textfield31);
    panel.add(textfield32);
    panel.add(textfield41);
    panel.add(textfield42);
    panel.add(textfield51);
    panel.add(textfield52);
    GlobalControls.delay = 900;

    newButton = new JButton("新 建");
    newButton.addActionListener(this);
    runButton = new JButton("运 行");
    runButton.addActionListener(this);
    stepButton = new JButton("单 步");
    stepButton.addActionListener(this);
    stayButton = new JButton("暂 停");
    stayButton.addActionListener(this);
    repaint();
    this.setBackground(Color.lightGray);
  }

  public void start() {
    if (runner == null) {
      runner = new Thread(this);
      runner.start();
    }
  }

  public void stop() {
    if (runner != null) {
      runner = null;

    }
  }

  public void paint(Graphics g) {
    System.out.println("hello");
    draw(g);

  }

  public void update(Graphics g) {
    paint(g);
  }

  public void actionPerformed(ActionEvent actionevent) {

    if (actionevent.getSource() == newButton) {
      stop();
      mid = 0;
      high = 0;
      low = 0;
      searchCount = 0;
      searchInt = 0;
      BinsearchFrame.newData.setLocation(200, 200);
      BinsearchFrame.newData.setVisible(true);

      if (BinsearchFrame.newData.getCheckFlag()) {
        int arr[] = BinsearchFrame.newData.getTable();
        int count = BinsearchFrame.newData.getCount();
        binPersonGroup(arr, count);
        stepButton.setEnabled(true);
        runButton.setEnabled(true);
        stayButton.setEnabled(true);
        searchInt = BinsearchFrame.newData.getsearchInt();
        searchlab.setText(String.valueOf(searchInt));
        notecon = "按单步或运行按钮开始";
      }

      this.searchlab.setLocation(25, 150);
      codePart = 1;
      this.codePanel.highlight(1);
      doneFlag = false;
      repaint();
    }

    if (actionevent.getSource() == stepButton) {
      runFlag = false;
      searchStep();
      if (doneFlag) {

        newButton.setEnabled(true);
        stepButton.setEnabled(false);
        stayButton.setEnabled(false);
        runButton.setEnabled(false);
        mid = 0;
        high = 0;
        low = 0;
        searchCount = 0;
        searchInt = 0;
        JOptionPane.showConfirmDialog(this,
                                      "    算法演示完成!\n  \n 单击相关按钮重新开始\n",
                                      "演示完成", JOptionPane.DEFAULT_OPTION);

      }
      repaint();
    }
    if (actionevent.getSource() == runButton) {
      try {
        this.start();
      }
      catch (Exception exp) {
        exp.toString();
      }
      runFlag = true;

      //  newButton.setEnabled(false);
    }
    if (actionevent.getSource() == stayButton) {
      runFlag = false;
    }

  }

  public void run() {
    while (Thread.currentThread() == this.runner) {

      if (runFlag) {
        if (doneFlag) { //判断是否运行结束

          JOptionPane.showConfirmDialog(this,
                                        "    算法演示完成!\n  \n 单击相关按钮重新开始\n",
                                        "演示完成", JOptionPane.DEFAULT_OPTION);

          newButton.setEnabled(true);
          stepButton.setEnabled(false);
          stayButton.setEnabled(false);
          runButton.setEnabled(false);
          mid = 0;
          high = 0;
          low = 0;
          searchCount = 0;
          searchInt = 0;
          runFlag = false;
          stop();
        }
        else {
          searchStep();
          repaint();
          controls = controlsPanel.delay;
          try {
            runner.sleep(controls);
          }
          catch (InterruptedException exp) {
            exp.toString();
          }
        }
      }
    }
  }

  public void binPersonGroup(int arr[], int count) {
    drawMode = 1;
    table = arr;
    this.count = count;
    try {
      for (int i = 1; i <= count; i++) {
        data[i] = makePerson(table[i]);
      }
    }
    catch (ArrayIndexOutOfBoundsException exp) {
      System.out.println(exp.toString());
      System.out.println("" + count);
    }

  }

  private BinSearchPerson makePerson(int i) {
    Color color;
    int j = 100 + (int) (Math.random() * 154);
    int k = 100 + (int) (Math.random() * 154);
    int l = 100 + (int) (Math.random() * 154);

    color = new Color(j, k, l);

    return new BinSearchPerson(i, color);
  }

  public void draw(Graphics g) {
    switch (drawMode) {
      case 1:
        try {
          for (int k = 1; k <= count; k++) {

            labarr[k].setAlignment(Label.CENTER);

            labarr[k].setText("" + table[k]);
            labarr[k].setBackground(data[k].getColor());
            centerpanel.add(labarr[k]);
            labarr[k].setVisible(true);
            labprompt[k].setVisible(true);
            labprompt[k].setSize(25, 25);
            labprompt[k].setAlignment(Label.CENTER);
            labprompt[k].setLocation(25 * k, 250);
            labprompt[k].setText(String.valueOf(k));
          }
          for (int i = count + 1; i <= 20; i++) {
            labarr[i].setVisible(false);
            labprompt[i].setVisible(false);
          }
        }

        catch (ArrayIndexOutOfBoundsException exc) {
          System.out.println("draw" + count);
        }

        case 2:
          this.searchlab.setLocation(25 * mid, 150);
          note.setText(notecon);
          textfield12.setText(String.valueOf(low));
          textfield22.setText(String.valueOf(mid));
          textfield32.setText(String.valueOf(high));
          textfield42.setText(String.valueOf(searchInt));
          textfield52.setText(String.valueOf(searchCount));
    }
    drawMode = 2;
  }

  public void searchStep() {
    switch (codePart) {
      case 1:
        searchCount = 0;
        codePanel.highlight(1);
        notecon = "开始";
        codePart = 2;
        return;
      case 2:
        codePanel.highlight(2);
        low = 1;
        high = count;
        notecon = "初始化low,high的值";
        codePart = 3;
        return;
      case 3:
        codePanel.highlight(3);
        if (low <= high) {
          searchCount++;
          notecon = "low<=high继续查找";
          codePart = 5;
        }
        else {
          notecon = "没有查找到" + searchInt + "查找结束";
          codePart = 12;
        }
        return;
      case 5:
        codePanel.highlight(5);
        mid = (low + high) / 2;
        notecon = "计算mid的值";
        codePart = 6;
        return;
      case 6:
        codePanel.highlight(6);
        if (searchInt == table[mid]) {
          notecon = "查找到" + searchInt;
          codePart = 7;
        }
        else {
          codePart = 8;
          notecon = "位置为" + mid + "的key值不等于" + searchInt;
        }
        return;
      case 7:
        codePanel.highlight(7);
        searchIndex = mid;
        notecon = "查找到" + searchInt + "  位置为" + searchIndex;
        codePart = 13;
        return;
      case 8:
        codePanel.highlight(8);
        if (searchInt < table[mid]) {
          notecon = "查找元素:" + searchInt + "<" + table[mid];
          codePart = 9;
        }
        else {
          codePart = 10;
          notecon = "查找元素:" + searchInt + ">" + table[mid];
        }
        return;
      case 9:
        codePanel.highlight(9);
        high = mid - 1;
        notecon = "high指针指向mid元素的前一个";
        codePart = 3;
        return;
      case 10:
        codePanel.highlight(10);
        low = mid + 1;
        notecon = "low指针指向mid元素的后一个";
        codePart = 3;
        return;
      case 12:
        codePanel.highlight(12);
        codePart = 13;
        notecon = "查找失败返回0";
        return;
      case 13:
        codePanel.highlight(13);
        // notecon = "按新建按钮重新开始";
        doneFlag = true;
        return;
    }

  }

}

⌨️ 快捷键说明

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