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

📄 danjface.java

📁 N皇后问题的实现
💻 JAVA
字号:
package arithmetic;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class DanJFace extends ApplicationWindow {

	protected static Shell shell;

	private List resultList;

	private List fileList;

	protected static String fn = "";

	private static int M, N;

	private final static Double MAX = 10000.0;

	int[] n, b;

	double[][] a;

	/**
	 * Create the application window
	 */
	public Dan() {
		super(null);
		createActions();
		addToolBar(SWT.FLAT | SWT.WRAP);
		addMenuBar();
		addStatusLine();
	}

	/**
	 * Create contents of the application window
	 * 
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.BORDER);

		final Label label = new Label(container, SWT.NONE);
		label.setText("文件内容:");
		label.setBounds(0, 10, 588, 17);

		fileList = new List(container, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		fileList.setBounds(0, 25, 588, 157);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setText("计算结果:");
		label_1.setBounds(0, 188, 584, 17);

		resultList = new List(container, SWT.BORDER);
		resultList.setBounds(0, 205, 588, 87);

		final Button button = new Button(container, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				FileDialog dlg = new FileDialog(shell);
				String fileName = dlg.open();
				try {
					if (fileName != null) {
						// 打开指定的文件
						if (initParameters(fileName) == false) {
							fn = null;
							return;
						}

					}
					if (fileName != null) {
						fn = fileName;
					}
				} catch (Exception e1) {
					MessageDialog.openInformation(null, "", "打开文件失败!");
					fn = null;
					return;
				}

			}

		});
		button.setText("打开文件");
		button.setBounds(56, 307, 70, 30);

		final Button button_1 = new Button(container, SWT.NONE);
		button_1.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				try {
					if (initParameters(fn) == false)
						return;

					boolean flag = false;
					while (a[0][1] > 0 || a[0][2] > 0 || a[0][3] > 0) {
						flag = Simplex(a, n, b);
						if (flag == false)
							break;
					}
					if (flag == false)
						MessageDialog.openInformation(null, "", "unbounded!");
					else {
						resultList.add(String.valueOf(a[0][0]));
					}
				} catch (Exception e1) {
					MessageDialog.openInformation(null, "", "文件内容不合法!");
					return;
				}

			}

			void Pivot(double[][] a, int[] n, int[] b, int l, int e) {

				a[l][0] = a[l][0] / a[l][e];
				for (int j = 1; j < N; j++) {
					if (j != e)
						a[l][j] = a[l][j] / a[l][e];
				}

				a[l][e] = 1 / a[l][e];

				for (int i = 1; i < M; i++) {
					if (i != l) {
						a[i][0] = a[i][0] - a[i][e] * a[l][0];
						for (int j = 1; j < N; j++) {
							if (j != e) {
								a[i][j] = a[i][j] - a[i][e] * a[l][j];
							}
						}

						a[i][e] = -a[i][e] * a[l][e];

					}
				}
				a[0][0] = a[0][0] + a[0][e] * a[l][0];

				for (int j = 1; j < N; j++) {
					if (j != e) {
						a[0][j] = a[0][j] - a[0][e] * a[l][j];
					}
				}

				a[0][e] = -a[0][e] * a[l][e];

				int t = b[l];
				b[l] = n[e];
				n[e] = t;
			}

			boolean Simplex(double[][] a, int[] n, int[] b) {
				int i, j, k, t = 0;
				double sum[] = new double[M], min;
				for (j = 1; j < N; j++) {
					if (a[0][j] > 0) {
						t = j;
						break;
					}
				}
				if (t < N) {

					for (i = 1; i < M; i++) {
						if (a[i][t] > 0)
							sum[i] = a[i][0] / a[i][t];

						else
							sum[i] = MAX;
					}
					min = sum[1];
					k = 1;
					for (i = 2; i < M; i++)
						if (sum[i] < min) {
							min = sum[i];
							k = i;
						}

					if (min == MAX)
						return false;
					else
						Pivot(a, n, b, k, t);
				}
				return true;
			}
		});
		button_1.setBounds(164, 307, 70, 30);
		button_1.setText("计算");

		return container;
	}

	public boolean initParameters(String fileName) throws IOException {
		FileInputStream fis = new FileInputStream(fileName);
		fileList.removeAll();
		resultList.removeAll();
		BufferedReader in = new BufferedReader(new InputStreamReader(fis));
		String s = null;
		// 将指定的文件一行一行地加到列表框中
		if ((s = in.readLine()) != null) {
			fileList.add(s + "\r\n");

			try {
				StringTokenizer tokenizer = new StringTokenizer(s);
				String num = tokenizer.nextToken();
				M = Integer.parseInt(num);
				num = tokenizer.nextToken();
				N = Integer.parseInt(num);
				n = new int[N];
				b = new int[M];
				a = new double[M][N];

				n[0] = b[0] = 0;
				for (int i = 1; i < N; i++) {
					n[i] = i;
				}
				for (int i = 1; i < M; i++) {
					b[i] = N + i - 1;
				}
			} catch (Exception e1) {
				MessageDialog.openInformation(null, "", "文件内容不合法!");
				return false;
			}
		}
		try {
			int row = 0, col = 0;
			while ((s = in.readLine()) != null) {
				fileList.add(s + "\r\n");
				StringTokenizer tokenizer = new StringTokenizer(s);
				while (tokenizer.hasMoreTokens()) {
					String num = tokenizer.nextToken();
					a[row][col] = Double.parseDouble(num);
					col++;
				}
				row++;
				col = 0;
			}
		} catch (Exception e1) {
			MessageDialog.openInformation(null, "", "文件内容不合法!");
			return false;
		}
		return true;
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Create the menu manager
	 * 
	 * @return the menu manager
	 */
	@Override
	protected MenuManager createMenuManager() {
		MenuManager menuManager = new MenuManager("menu");
		return menuManager;
	}

	/**
	 * Create the toolbar manager
	 * 
	 * @return the toolbar manager
	 */
	@Override
	protected ToolBarManager createToolBarManager(int style) {
		ToolBarManager toolBarManager = new ToolBarManager(style);
		return toolBarManager;
	}

	/**
	 * Create the status line manager
	 * 
	 * @return the status line manager
	 */
	@Override
	protected StatusLineManager createStatusLineManager() {
		StatusLineManager statusLineManager = new StatusLineManager();
		statusLineManager.setMessage(null, "");
		return statusLineManager;
	}

	/**
	 * Launch the application
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Dan window = new Dan();
			window.setBlockOnOpen(true);
			window.open();
			Display.getCurrent().dispose();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Configure the shell
	 * 
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("单纯型问题");
		shell = newShell;
	}

	/**
	 * Return the initial size of the window
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(606, 438);
	}

}

⌨️ 快捷键说明

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