adjacency.java~95~
来自「拓扑排序实现排课功能 8学期任意多门课程排序」· JAVA~95~ 代码 · 共 779 行 · 第 1/2 页
JAVA~95~
779 行
public void jButton7_actionPerformed(ActionEvent e) {
a = new String[n];
b = new String[n];
if (jTextField1.getText() == "") {
JOptionPane.showMessageDialog(null, "不能为空");
} else {
this.n = Integer.parseInt(jTextField1.getText());
aa = new int[n + 1][n + 1];
ad.makeAdjacency(n, 0, aa); //建立邻接矩阵
ad.Output(aa);
}
}
public static void main(String[] args) { //界面
Face f = new Face();
f.setVisible(true);
f.setSize(533, 700);
}
//添加课程//添加科目
public void jButton6_actionPerformed(ActionEvent e) {
int index = jComboBox3.getSelectedIndex();
if (j < n) {
jComboBox1.addItem(String.valueOf(jComboBox3.getItemAt(index)));
jComboBox2.addItem(String.valueOf(jComboBox3.getItemAt(index)));
a[j] = String.valueOf(jComboBox3.getItemAt(index)); //成功将课程名添加进去
b[j] = a[j]; //赋到b里面去
System.out.print(b[j]);
j++;
} else {
JOptionPane.showMessageDialog(null, "添加" + n + "门课程完毕"); //数量购了
}
}
public void jButton5_actionPerformed(ActionEvent e) { //退出
System.exit(0);
}
int m = 0, j = 0;
JLabel jLabel9 = new JLabel();
TitledBorder titledBorder1 = new TitledBorder("");
JLabel jLabel10 = new JLabel();
TitledBorder titledBorder2 = new TitledBorder("");
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
JScrollPane jScrollPane3 = new JScrollPane();
JScrollPane jScrollPane4 = new JScrollPane();
JScrollPane jScrollPane5 = new JScrollPane();
JScrollPane jScrollPane6 = new JScrollPane();
JScrollPane jScrollPane7 = new JScrollPane();
JScrollPane jScrollPane8 = new JScrollPane();
JTextArea jTextArea5 = new JTextArea();
JTextArea jTextArea6 = new JTextArea();
JTextArea jTextArea7 = new JTextArea();
JTextArea jTextArea8 = new JTextArea();
JLabel jLabel11 = new JLabel();
JLabel jLabel12 = new JLabel();
JLabel jLabel13 = new JLabel();
JLabel jLabel14 = new JLabel();
//设定先行性
Xx x = new Xx();
public void jButton1_actionPerformed(ActionEvent e) {
x.setVisible(true);
x.setSize(500, 500);
if (jCheckBox1.isSelected()) {
int index1 = jComboBox1.getSelectedIndex();
int index2 = jComboBox2.getSelectedIndex();
for (m = 0; m < n; m++) {
for (j = 0; j < n; j++) {
if ((a[m].equals(String.valueOf(jComboBox1.getItemAt(index1)))) &&
(b[j].equals(String.valueOf(jComboBox2.getItemAt(index2))))) {
ad.Add(m + 1, j + 1, 1, aa); //赋值数名a是b的先行课
x.jTextArea1.append("《" + a[m] + "》" + "是" + "《" + b[j] +
"》" +
"的先行课" + "\n"); //浏览
}
}
}
}
ad.Output(aa);
}
//课表显示
public void jButton4_actionPerformed(ActionEvent e) {
jTextArea1.setText("");
jTextArea2.setText("");
jTextArea3.setText("");
jTextArea4.setText("");
jTextArea5.setText("");
jTextArea6.setText("");
jTextArea7.setText("");
jTextArea8.setText("");
Field v[] = new Field[n];//构造v
top.Topo(v, n, aa);//进行拓扑排序结果到v
for (int i = 0; i < n; i++) {//按学期输出课名
if (v[i].term == 1) {
jTextArea1.append(a[v[i].index - 1] + "\n");
}
if (v[i].term == 2) {
jTextArea2.append(a[v[i].index - 1] + "\n");
}
if (v[i].term == 3) {
jTextArea3.append(a[v[i].index - 1] + "\n");
}
if (v[i].term == 4) {
jTextArea4.append(a[v[i].index - 1] + "\n");
}
if (v[i].term == 5) {
jTextArea5.append(a[v[i].index - 1] + "\n");
}
if (v[i].term == 6) {
jTextArea6.append(a[v[i].index - 1] + "\n");
}
if (v[i].term == 7) {
jTextArea7.append(a[v[i].index - 1] + "\n");
}
if (v[i].term == 8) {
jTextArea8.append(a[v[i].index - 1] + "\n");
}
}
}
//查看选课情况
public void jButton3_actionPerformed(ActionEvent e) {
Xx x = new Xx();
x.setVisible(true);
x.setSize(400, 400);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (aa[i][j] == 1) {
x.jTextArea1.append("《" + a[i - 1] + "》" + "是" + "《" + b[j -
1] + "》" +
"的先行课" + "\n");
}
}
}
}
}
class Face_jButton3_actionAdapter implements ActionListener {
private Face adaptee;
Face_jButton3_actionAdapter(Face adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class Face_jButton4_actionAdapter implements ActionListener {
private Face adaptee;
Face_jButton4_actionAdapter(Face adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}
class Face_jButton1_actionAdapter implements ActionListener {
private Face adaptee;
Face_jButton1_actionAdapter(Face adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Face_jButton7_actionAdapter implements ActionListener {
private Face adaptee;
Face_jButton7_actionAdapter(Face adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton7_actionPerformed(e);
}
}
class Face_jButton5_actionAdapter implements ActionListener {
private Face adaptee;
Face_jButton5_actionAdapter(Face adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton5_actionPerformed(e);
}
}
class Face_jButton6_actionAdapter implements ActionListener {
private Face adaptee;
Face_jButton6_actionAdapter(Face adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton6_actionPerformed(e);
}
}
package course;
import java.util.Stack;
/**
* <p>Title: 学期域</p>
*
* <p>Description:保存学期数 </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Field {//定义了雷域,两个属性,分表保存课序和学期数
int index = 1;
int term = 0;
}
package course;
import java.util.Stack;
/**
* <p>Title: 拓扑排序</p>
*
* <p>Description: 实现拓扑排序</p>
*
* <p>Copyright: Copyright (c) 2007 </p>
*
* <p>Company: www.bchine.com</p>
*
* @author 李丕绩
* @version 1.0
*/
public class Topological extends Adjacency {
Stack S = new Stack();
Adjacency ad = new Adjacency();
Field fi[];//Field类型的数组
Field eld = new Field();//用来接由堆栈弹书来的对象
public static int t;
public boolean Topo(Field v[], int n, int aa[][]) {
fi = new Field[n];
ad.InitializePos(n); //初始化数组pos作为中间桥梁的作用
int InDegree[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
InDegree[i] = 0; //初始化入度
}
for (int i = 1; i <= n; i++) {
int u = ad.Begin(i, aa);//i邻接的顶点
while (u != 0) {
InDegree[u]++;//初始化各点入度
u = NextVertex(i, aa);
}
}
for (int i = 1; i <= n; i++) {
fi[i - 1] = new Field();//初始化
if (InDegree[i] == 0) {//如果入度为零
fi[i - 1].index = i;//赋上课序号
fi[i - 1].term = 1;//赋上学期号
S.push(fi[i - 1]);//压栈
}
}
int a = 0;
while (!S.empty()) {//当栈不为空时
eld = (Field) S.pop();//弹出
v[a++] = eld;//存入数组,最后用来输出
int w = eld.index;//可序号
int u = Begin(w, aa);//和w邻接的点
while (u != 0) {//若不为零
InDegree[u]--;
if (InDegree[u] == 0) {
fi[u - 1].index = u;//赋上可序号
fi[u - 1].term = eld.term + 1;//将上点的学期号加一
// System.out.println(u + "入栈");
S.push(fi[u - 1]);//入栈
}
u = NextVertex(w, aa);//判断与w邻接的下一个顶点
}
}
return (a == n);
}
}
package course;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.SystemColor;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.UIManager;
/**
* <p>Title: </p>
*
* <p>Description: 用来显示课先行性的窗体</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Xx extends JFrame {
public Xx() {
try {
jbInit();
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setBackground(UIManager.getColor(
"InternalFrame.inactiveTitleBackground"));
setSize(new Dimension(400, 400));
getContentPane().setLayout(null);
jScrollPane1.setBounds(new Rectangle(7, 57, 385, 315));
jTextArea1.setFont(new java.awt.Font("Dialog", Font.BOLD, 14));
jTextArea1.setBorder(BorderFactory.createLoweredBevelBorder());
jLabel1.setForeground(Color.red);
this.getContentPane().add(jScrollPane1);
this.getContentPane().add(jLabel1);
jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 18));
jLabel1.setText("先导课程显示");
jLabel1.setBounds(new Rectangle(118, 28, 152, 25));
jScrollPane1.getViewport().add(jTextArea1);
}
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
JLabel jLabel1 = new JLabel();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?