内存动态分配的首先适应、最优适应、最坏适应算法的窗口演示(java版)

           以前写了他的算法描述,这次把他的窗口程序一块写了出来,用 java 写的,感觉还不是那么的完美,但已经能够说明问题了,达到了演示的目的。

import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.*;

public class DynamicAssign {

    public static void main(String[] args) {

        ShowJFrame sjf = new ShowJFrame("内存作业分配算法描述", 150, 150, 400, 300);

    }
}

class Method {

    Area[] area;

    Memory[] memory;
//首次适应算法
    public static void FirstAdapt(Memory[] memory, Area[] area) {

        int k;
        String[] output = new String[memory.length];

        for (int i = 0; i < memory.length; i++) {
            k = area.length;
            for (int j = 0; j < area.length; j++) {
                if ((memory[i].getValue()) <= (area[j].getValue())) {
                    area[j].setValue(area[j].getValue() - memory[i].getValue());

                    output[i] = memory[i] + "------>" + area[j];

                    break;
                } else
                    k--;
                if (k < 1) {

                    output[i] = memory[i] + " 失败 !";

                }

            }

        }
        String string = "";
        for (int i = 0; i < memory.length; i++) {
            string = string + output[i] + "/n";
        }
        javax.swing.JOptionPane.showMessageDialog(null, string, "显示分配作业结果",
                JOptionPane.INFORMATION_MESSAGE);

    }
//最优适应算法
    public static void BestAdapt(Memory[] memory, Area[] area) {
        int i, j;
        String[] output = new String[memory.length];
        for (i = 0; i < memory.length; i++) {
            boolean flag = true;

            int min = 0;
            for (j = 0; j < area.length; j++) {
                if (flag) {
                    if (memory[i].getValue() <= area[j].getValue()) {
                        min = area[j].getValue();
                        flag = false;
                    }
                } else if ((memory[i].getValue() <= area[j].getValue())
                        && (min >= area[j].getValue())) {
                    min = area[j].getValue();
                }
            }
            if (min == 0) {

                output[i] = memory[i] + " 失败 !";

            } else {
                for (j = 0; j < area.length; j++) {
                    if (area[j].getValue() == min) {

                        output[i] = memory[i] + "------>" + area[j];
                        area[j].setValue(min - memory[i].getValue());
                        break;
                    }
                }
            }

        }
        String string = "";
        for (i = 0; i < memory.length; i++) {
            string = string + output[i] + "/n";
        }
        javax.swing.JOptionPane.showMessageDialog(null, string, "显示分配作业结果",
                JOptionPane.INFORMATION_MESSAGE);

    }
//最坏适应算法
    public static void BadAdapt(Memory[] memory, Area[] area) {
        int i, j;
        String[] output = new String[memory.length];
        for (i = 0; i < memory.length; i++) {
            boolean flag = true;

            int max = 0;
            for (j = 0; j < area.length; j++) {
                if (flag) {
                    if (memory[i].getValue() <= area[j].getValue()) {
                        max = area[j].getValue();
                        flag = false;
                    }
                } else if ((memory[i].getValue() <= area[j].getValue())
                        && (max <= area[j].getValue())) {
                    max = area[j].getValue();
                }
            }
            if (max == 0) {

                output[i] = memory[i] + " 失败 !";

            } else {
                for (j = 0; j < area.length; j++) {
                    if (area[j].getValue() == max) {

                        output[i] = memory[i] + "------>" + area[j];
                        area[j].setValue(max - memory[i].getValue());
                        break;
                    }
                }
            }

        }
        String string = "";
        for (i = 0; i < memory.length; i++) {
            string = string + output[i] + "/n";
        }
        javax.swing.JOptionPane.showMessageDialog(null, string, "显示分配作业结果",
                JOptionPane.INFORMATION_MESSAGE);
    }

}

class ShowJFrame extends JFrame implements ActionListener {

    Method method;

    public ShowJFrame(Method method) {
        this.method = method;
    }

    JTextField m1 = new JTextField(4);

    JTextField m2 = new JTextField(4);

    JTextField m3 = new JTextField(4);

    JTextField m4 = new JTextField(4);

    JTextField a1 = new JTextField(4);

    JTextField a2 = new JTextField(4);

    JTextField a3 = new JTextField(4);

    JTextField a4 = new JTextField(4);

    JTextField a5 = new JTextField(4);

    JRadioButton jrb1 = new JRadioButton("首次适应算法");

    JRadioButton jrb2 = new JRadioButton("最优适应算法");

    JRadioButton jrb3 = new JRadioButton("最坏适应算法");

    ShowJFrame(String s, int x, int y, int w, int h) {
        super(s);
        TitledBorder tb1 = new TitledBorder("作业大小");
        TitledBorder tb2 = new TitledBorder("内存区域大小");
        TitledBorder tb3 = new TitledBorder("分配方法");

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();

        JPanel jp5 = new JPanel();

        jp1.setLayout(new GridLayout(4, 2));
        jp2.setLayout(new GridLayout(5, 2));
        jp3.setLayout(new GridLayout(3, 1));

        jp5.setLayout(new GridLayout(2, 1));

        jp1.add(new JLabel("作业0"));
        jp1.add(m1);
        jp1.add(new JLabel("作业1"));
        jp1.add(m2);
        jp1.add(new JLabel("作业2"));
        jp1.add(m3);
        jp1.add(new JLabel("作业3"));
        jp1.add(m4);

        jp2.add(new JLabel("内存区域0"));
        jp2.add(a1);
        jp2.add(new JLabel("内存区域1"));
        jp2.add(a2);
        jp2.add(new JLabel("内存区域2"));
        jp2.add(a3);
        jp2.add(new JLabel("内存区域3"));
        jp2.add(a4);
        jp2.add(new JLabel("内存区域4"));
        jp2.add(a5);

        jp3.add(jrb1);
        jp3.add(jrb2);
        jp3.add(jrb3);

        ButtonGroup group = new ButtonGroup();
        group.add(jrb1);
        group.add(jrb2);
        group.add(jrb3);

        jp5.add(jp1);
        jp5.add(jp2);

        jp1.setBorder(tb1);
        jp2.setBorder(tb2);
        jp3.setBorder(tb3);

        jrb1.addActionListener(this);
        jrb2.addActionListener(this);
        jrb3.addActionListener(this);

        add(jp5, BorderLayout.CENTER);
        add(jp3, BorderLayout.EAST);

        pack();
        setTitle(s);
        setBounds(x, y, w, h);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        Memory[] memory = new Memory[4];
        int n0 = Integer.parseInt(m1.getText().trim());
        memory[0] = new Memory(0, n0);
        int n1 = Integer.parseInt(m2.getText().trim());
        memory[1] = new Memory(1, n1);
        int n2 = Integer.parseInt(m3.getText().trim());
        memory[2] = new Memory(2, n2);
        int n3 = Integer.parseInt(m4.getText().trim());
        memory[3] = new Memory(3, n3);

        Area[] area = new Area[5];
        int b0 = Integer.parseInt(a1.getText().trim());
        area[0] = new Area(0, b0);
        int b1 = Integer.parseInt(a2.getText().trim());
        area[1] = new Area(1, b1);
        int b2 = Integer.parseInt(a3.getText().trim());
        area[2] = new Area(2, b2);
        int b3 = Integer.parseInt(a4.getText().trim());
        area[3] = new Area(3, b3);
        int b4 = Integer.parseInt(a5.getText().trim());
        area[4] = new Area(4, b4);

        if (e.getSource() == jrb1) {

            method.FirstAdapt(memory, area);

        } else if (e.getSource() == jrb2) {

            method.BestAdapt(memory, area);

        } else if (e.getSource() == jrb3) {

            method.BadAdapt(memory, area);

        }
    }

}

class Area {

    private int id;

    private int value;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    Area(int id) {
        this.id = id;
    }

    Area(int id, int value) {
        this.id = id;
        this.value = value;
    }

    public String toString() {
        return "内存区域 " + id;

    }

}

class Memory {

    Area[] area = new Area[5];

    Memory[] memory = new Memory[4];

    private int id;

    private int value;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    Memory(int id) {
        this.id = id;
    }

    Memory(int id, int value) {
        this.id = id;
        this.value = value;
    }

    public String toString() {
        return "作业 " + id;
    }

}


以上是一个完整的程序,只要编译后就能运行,不需要任何的改动。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值