Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ayhaga;
- import org.eclipse.swt.widgets.Display;
- import java.io.*;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.SWT;
- //import org.eclipse.wb.swt.SWTResourceManager;
- import com.sun.java.swing.plaf.windows.resources.windows;
- import org.eclipse.swt.widgets.Text;
- import org.eclipse.swt.widgets.List;
- import org.eclipse.swt.widgets.Table;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- import org.eclipse.jface.dialogs.MessageDialog;
- import org.eclipse.jface.viewers.TableViewer;
- import org.eclipse.swt.custom.CCombo;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.layout.GridLayout;
- import org.eclipse.wb.swt.SWTResourceManager;
- public class Hi {
- static ArrayList<Integer> minTerms = new ArrayList<Integer>();
- static ArrayList<Integer> dontCare = new ArrayList<Integer>();
- static int bits, numOfMins, numOfDonts;
- static boolean taken[] = new boolean[10000000];
- static ArrayList<String> minTermInBinary = new ArrayList<String>();
- static ArrayList<String> dontCareInBinary = new ArrayList<String>();
- static ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
- static ArrayList<ArrayList<ArrayList<Integer>>> int_table = new ArrayList<ArrayList<ArrayList<Integer>>>();
- static ArrayList<ArrayList<String>> secondaryTable = new ArrayList<ArrayList<String>>();
- static ArrayList<ArrayList<ArrayList<Integer>>> int_secondarytable = new ArrayList<ArrayList<ArrayList<Integer>>>();
- static boolean toPrimes[][] = new boolean[200][1000];
- static ArrayList<String> primeImplicants = new ArrayList<String>();
- static ArrayList<ArrayList<Integer>> intPrimeImplicants = new ArrayList<ArrayList<Integer>>();
- static int numToBeTaken = -1;
- static int numOfOccurence[] = new int[200000];
- static ArrayList<ArrayList<String>> resMinSOP = new ArrayList<ArrayList<String>>();
- static boolean isMinTerm[] = new boolean[200000];
- //
- static ArrayList<String> Steps = new ArrayList<String>();
- static int nSteps = 0;
- static ArrayList<String> finalResult = new ArrayList<String>();
- protected Shell shlSwtApplication;
- private Text minterms;
- private Button btnNewButton;
- private Button btnExit;
- private Text finalanswer;
- private Button btnNewButton_1;
- private Text dontcares;
- private Button btnShowSteps;
- /**
- * Launch the application.
- *
- * @param args
- */
- public static void main(String[] args) {
- try {
- Hi window = new Hi();
- window.open();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Open the window.
- */
- public void open() {
- Display display = Display.getDefault();
- createContents();
- shlSwtApplication.open();
- shlSwtApplication.layout();
- while (!shlSwtApplication.isDisposed()) {
- if (!display.readAndDispatch()) {
- display.sleep();
- }
- }
- }
- /**
- * Create contents of the window.
- */
- protected void createContents() {
- shlSwtApplication = new Shell();
- shlSwtApplication.setSize(717, 476);
- shlSwtApplication.setText("Quin Solver");
- Label Welcoming = new Label(shlSwtApplication, SWT.NONE);
- Welcoming.setFont(SWTResourceManager.getFont("Traditional Arabic", 31, SWT.NORMAL));
- Welcoming.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
- Welcoming.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
- Welcoming.setForeground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
- Welcoming.setAlignment(SWT.CENTER);
- Welcoming.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
- Welcoming.setFont(SWTResourceManager.getFont("OCR A Extended", 38, SWT.BOLD));
- Welcoming.setBounds(10, 24, 681, 103);
- Welcoming.setText("\u2764\uD83D\uDC38 Quine McClusky \uD83D\uDC38\u2764\r\n\u2764\uD83D\uDC38 Solver \uD83D\uDC38\u2764");
- minterms = new Text(shlSwtApplication, SWT.BORDER);
- minterms.setBounds(20, 174, 419, 25);
- Label lblNewLabel = new Label(shlSwtApplication, SWT.NONE);
- lblNewLabel.setFont(SWTResourceManager.getFont("Segoe UI", 16, SWT.NORMAL));
- lblNewLabel.setBounds(20, 133, 212, 35);
- lblNewLabel.setText("Enter MinTerms Here :");
- Label lblNewLabel_1 = new Label(shlSwtApplication, SWT.NONE);
- lblNewLabel_1.setFont(SWTResourceManager.getFont("Segoe UI", 16, SWT.NORMAL));
- lblNewLabel_1.setBounds(20, 205, 230, 35);
- lblNewLabel_1.setText("Enter Don't Cares Here :");
- btnNewButton = new Button(shlSwtApplication, SWT.NONE);
- btnNewButton.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
- btnNewButton.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- try {
- String min = minterms.getText();
- String dont = dontcares.getText();
- reset();
- DoIt(dont, min);
- finalanswer.setText(finalResult.get(0));
- } catch (Exception e1) {
- MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
- "Input should be between 0 & 2^19 - 1 without duplicates");
- }
- }
- });
- btnNewButton.setBounds(20, 287, 75, 25);
- btnNewButton.setText("Solve !");
- btnExit = new Button(shlSwtApplication, SWT.NONE);
- btnExit.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- shlSwtApplication.close();
- }
- });
- btnExit.setBounds(616, 402, 75, 25);
- btnExit.setText("Exit");
- finalanswer = new Text(shlSwtApplication, SWT.BORDER | SWT.V_SCROLL);
- finalanswer.setBounds(23, 321, 587, 106);
- btnNewButton_1 = new Button(shlSwtApplication, SWT.NONE);
- btnNewButton_1.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- try {
- String min = minterms.getText();
- String dont = dontcares.getText();
- reset();
- DoIt(dont, min);
- finalanswer.setText("");
- String s = new String();
- for (int i = 0; i < finalResult.size(); i++) {
- s = finalResult.get(i);
- finalanswer.append(s + "\n");
- }
- } catch (Exception e1) {
- MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
- "Input should be between 0 & 2^19 - 1 without duplicates");
- }
- }
- });
- btnNewButton_1.setBounds(136, 287, 138, 25);
- btnNewButton_1.setText("Show All Answers");
- dontcares = new Text(shlSwtApplication, SWT.BORDER);
- dontcares.setBounds(20, 245, 419, 25);
- btnShowSteps = new Button(shlSwtApplication, SWT.NONE);
- btnShowSteps.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- try {
- String min = minterms.getText();
- String dont = dontcares.getText();
- reset();
- DoIt(dont, min);
- finalanswer.setText("");
- String s = new String();
- for (int i = 0; i < Steps.size(); i++) {
- s = Steps.get(i);
- finalanswer.append(s + "\n");
- }
- } catch (Exception e1) {
- MessageDialog.openWarning(Display.getDefault().getActiveShell(), "Warning",
- "Input should be between 0 & 2^19 - 1 without duplicates");
- }
- }
- });
- btnShowSteps.setBounds(322, 287, 75, 25);
- btnShowSteps.setText("Show Steps");
- }
- public static void reset() {
- minTerms = new ArrayList<Integer>();
- dontCare = new ArrayList<Integer>();
- taken = new boolean[10000];
- minTermInBinary = new ArrayList<String>();
- dontCareInBinary = new ArrayList<String>();
- table = new ArrayList<ArrayList<String>>();
- int_table = new ArrayList<ArrayList<ArrayList<Integer>>>();
- secondaryTable = new ArrayList<ArrayList<String>>();
- int_secondarytable = new ArrayList<ArrayList<ArrayList<Integer>>>();
- toPrimes = new boolean[200][1000];
- primeImplicants = new ArrayList<String>();
- intPrimeImplicants = new ArrayList<ArrayList<Integer>>();
- numToBeTaken = -1;
- numOfOccurence = new int[200000];
- resMinSOP = new ArrayList<ArrayList<String>>();
- isMinTerm = new boolean[200000];
- //
- Steps = new ArrayList<String>();
- nSteps = 0;
- finalResult = new ArrayList<String>();
- }
- public static void DoIt(String dont, String min) {
- resetIsTaken();
- String str = new String();
- String val = new String();
- int max = 0;
- for (int counter = 0; counter < min.length(); counter++) {
- if (!Character.isDigit(min.charAt(counter)) && min.charAt(counter) != ' ')
- throw new RuntimeException();
- else if (min.charAt(counter) == ' ') {
- str += "*";
- } else
- str += min.charAt(counter);
- }
- str += "*";
- for (int counter = 0; counter < str.length(); counter++) {
- if (str.charAt(counter) != '*')
- val += str.charAt(counter);
- if (str.charAt(counter) != '*' && str.charAt(counter + 1) == '*') {
- int temp = Integer.parseInt(val);
- if (temp > Math.pow(2, 19))
- throw new RuntimeException();
- if (taken[temp]) {
- throw new RuntimeException();
- }
- taken[temp] = true;
- max = Math.max(temp, max);
- minTerms.add(temp);
- val = new String();
- }
- }
- String forBits = Integer.toBinaryString(max);
- bits = forBits.length();
- str = new String();
- val = new String();
- for (int counter = 0; counter < dont.length(); counter++) {
- if (!Character.isDigit(dont.charAt(counter)) && dont.charAt(counter) != ' ')
- throw new RuntimeException();
- else if (dont.charAt(counter) == ' ') {
- str += "*";
- } else
- str += dont.charAt(counter);
- }
- str += "*";
- for (int counter = 0; counter < str.length(); counter++) {
- if (str.charAt(counter) != '*')
- val += str.charAt(counter);
- if (str.charAt(counter) != '*' && str.charAt(counter + 1) == '*') {
- int temp = Integer.parseInt(val);
- if (temp > Math.pow(2, 19))
- throw new RuntimeException();
- if (taken[temp]) {
- throw new RuntimeException();
- }
- taken[temp] = true;
- dontCare.add(temp);
- val = new String();
- }
- }
- numOfMins = minTerms.size();
- numOfDonts = dontCare.size();
- for (int i = 0; i < numOfMins; i++) {
- minTermInBinary.add(Integer.toBinaryString(minTerms.get(i)));
- while (minTermInBinary.get(i).length() < bits)
- minTermInBinary.set(i, "0" + minTermInBinary.get(i));
- }
- for (int i = 0; i < numOfDonts; i++) {
- dontCareInBinary.add(Integer.toBinaryString(dontCare.get(i)));
- while (dontCareInBinary.get(i).length() < bits)
- dontCareInBinary.set(i, "0" + dontCareInBinary.get(i));
- }
- for (int i = 0; i < numOfDonts; i++) {
- minTermInBinary.add(dontCareInBinary.get(i));
- minTerms.add(dontCare.get(i));
- }
- for (int i = 0; i < numOfMins; i++)
- isMinTerm[minTerms.get(i)] = true;
- getTheTable();
- printSteps();
- while (compare()) {
- checkTheUntakenElements();
- table.clear();
- int_table.clear();
- table = new ArrayList<ArrayList<String>>(secondaryTable);
- int_table = new ArrayList<ArrayList<ArrayList<Integer>>>(int_secondarytable);
- printSteps();
- }
- putInPrimeImplicants();
- removeDuplicates();
- printPrimeImplicants();
- calculateNumOfOccurrence();
- ArrayList<String> building = new ArrayList<String>();
- while (true) {
- numToBeTaken = -1;
- directGetofPrimeImp();
- if (numToBeTaken == -1)
- break;
- building.add(addDirectSOP());
- }
- planB(building);
- removeDuplicatesFromTheResult();
- String Hamada = new String();
- for (int i = 0; i < resMinSOP.size(); i++) {
- Hamada += (resMinSOP.get(i) + " ");
- if (i != resMinSOP.size() - 1)
- Hamada += (", ");
- }
- Steps.add(Hamada);
- printResult();
- }
- /**
- * setting the value of taken numbers to false again
- */
- static void resetIsTaken() {
- for (int i = 0; i < 10000; i++)
- taken[i] = false;
- }
- /**
- * setting the first table to be minimized later
- */
- static void getTheTable() {
- for (int numOfOnes = 0; numOfOnes <= bits; numOfOnes++) {
- ArrayList<String> subArray = new ArrayList<String>();
- ArrayList<ArrayList<Integer>> subInt = new ArrayList<ArrayList<Integer>>();
- for (int i = 0; i < numOfMins + numOfDonts; i++) {
- int temp = 0;
- for (int j = 0; j < bits; j++) {
- if (minTermInBinary.get(i).charAt(j) == '1')
- temp++;
- }
- if (temp == numOfOnes) {
- subArray.add(minTermInBinary.get(i));
- ArrayList<Integer> tempo = new ArrayList<Integer>();
- tempo.add(minTerms.get(i));
- subInt.add(tempo);
- }
- }
- if (subArray.size() != 0) {
- table.add(subArray);
- int_table.add(subInt);
- }
- }
- System.out.println(table.size());
- }
- static void printSteps() {
- for (int i = 0; i < table.size(); i++) {
- for (int j = 0; j < table.get(i).size(); j++) {
- Steps.add("-->" + int_table.get(i).get(j) + " " + table.get(i).get(j));
- }
- }
- Steps.add("=====================================");
- }
- static void printPrimeImplicants() {
- for (int i = 0; i < primeImplicants.size(); i++) {
- Steps.add("-->" + intPrimeImplicants.get(i) + " " + primeImplicants.get(i));
- }
- }
- static void resetTakenPrimes() {
- for (int i = 0; i < 200; i++)
- for (int j = 0; j < 1000; j++)
- toPrimes[i][j] = false;
- }
- static boolean compare() {
- resetTakenPrimes();
- boolean ok = false;
- secondaryTable.clear();
- int_secondarytable.clear();
- for (int i = 0; i < table.size() - 1; i++) {
- ArrayList<ArrayList<Integer>> subInt = new ArrayList<ArrayList<Integer>>();
- ArrayList<String> subBinary = new ArrayList<String>();
- for (int j = 0; j < table.get(i).size(); j++) {
- for (int k = 0; k < table.get(i + 1).size(); k++) {
- int numOfDiff = 0, index = 0;
- for (int l = 0; l < bits; l++) {
- if (table.get(i).get(j).charAt(l) != table.get(i + 1).get(k).charAt(l)
- && Character.isDigit(table.get(i).get(j).charAt(l))
- && Character.isDigit(table.get(i + 1).get(k).charAt(l))) {
- numOfDiff++;
- index = l;
- }
- }
- boolean skip = false;
- for (int p = 0; p < bits; p++)
- if (checkDashes(table.get(i).get(j).charAt(p), table.get(i + 1).get(k).charAt(p)))
- skip = true;
- if (numOfDiff == 1 && !skip) {
- toPrimes[i][j] = toPrimes[i + 1][k] = true;
- subBinary.add(table.get(i).get(j).substring(0, index) + "-"
- + table.get(i).get(j).substring(index + 1, bits));
- ArrayList<Integer> temp = new ArrayList<Integer>();
- for (int l = 0; l < int_table.get(i).get(j).size(); l++)
- temp.add(int_table.get(i).get(j).get(l));
- for (int l = 0; l < int_table.get(i + 1).get(k).size(); l++)
- temp.add(int_table.get(i + 1).get(k).get(l));
- subInt.add(temp);
- ok = true;
- }
- }
- }
- if (subBinary.size() > 0) {
- secondaryTable.add(subBinary);
- int_secondarytable.add(subInt);
- }
- }
- return ok;
- }
- static boolean checkDashes(char a, char b) {
- return (a == '-' && b != '-') || (a != '-' && b == '-');
- }
- static void checkTheUntakenElements() {
- for (int i = 0; i < table.size(); i++) {
- for (int j = 0; j < table.get(i).size(); j++) {
- if (!toPrimes[i][j]) {
- primeImplicants.add(table.get(i).get(j));
- intPrimeImplicants.add(int_table.get(i).get(j));
- }
- }
- }
- }
- static void putInPrimeImplicants() {
- for (int i = 0; i < table.size(); i++) {
- for (int j = 0; j < table.get(i).size(); j++) {
- primeImplicants.add(table.get(i).get(j));
- intPrimeImplicants.add(int_table.get(i).get(j));
- }
- }
- }
- static void removeDuplicates() {
- String ifRepeated;
- for (int i = 0; i < primeImplicants.size() - 1; i++) {
- ifRepeated = primeImplicants.get(i);
- for (int j = primeImplicants.size() - 1; j > i; j--) {
- if (ifRepeated.equals(primeImplicants.get(j))) {
- primeImplicants.remove(j);
- intPrimeImplicants.remove(j);
- }
- }
- }
- }
- static void calculateNumOfOccurrence() {
- for (int i = 0; i < 200000; i++)
- numOfOccurence[i] = 0;
- for (int i = 0; i < intPrimeImplicants.size(); i++)
- for (int j = 0; j < intPrimeImplicants.get(i).size(); j++)
- if (isMinTerm[intPrimeImplicants.get(i).get(j)])
- numOfOccurence[intPrimeImplicants.get(i).get(j)]++;
- }
- static boolean directGetofPrimeImp() {
- boolean cont = false;
- for (int i = 0; i < Math.pow(2, bits); i++) {
- if (numOfOccurence[i] == 1 && isMinTerm[i])
- numToBeTaken = i;
- if (numOfOccurence[i] != 0 && isMinTerm[i])
- cont = true;
- }
- return cont;
- }
- static String addDirectSOP() {
- String ret = "";
- for (int i = 0; i < intPrimeImplicants.size(); i++) {
- for (int j = 0; j < intPrimeImplicants.get(i).size(); j++) {
- if (intPrimeImplicants.get(i).get(j) == numToBeTaken) {
- ret = primeImplicants.get(i);
- for (int k = 0; k < intPrimeImplicants.get(i).size(); k++)
- numOfOccurence[intPrimeImplicants.get(i).get(k)] = 0;
- }
- }
- }
- return ret;
- }
- static void planB(ArrayList<String> x) {
- if (!directGetofPrimeImp()) {
- if (resMinSOP.size() == 0 || resMinSOP.get(resMinSOP.size() - 1).size() == x.size()) {
- resMinSOP.add(x);
- } else if (x.size() < resMinSOP.get(resMinSOP.size() - 1).size()) {
- resMinSOP.clear();
- resMinSOP.add(x);
- }
- return;
- }
- ArrayList<String> res = new ArrayList<String>(x);
- for (int i = 0; i < intPrimeImplicants.size(); i++) {
- int temp = 0;
- res = new ArrayList<String>(x);
- for (int j = 0; j < intPrimeImplicants.get(i).size(); j++) {
- if (numOfOccurence[intPrimeImplicants.get(i).get(j)] != 0
- && isMinTerm[intPrimeImplicants.get(i).get(j)])
- temp++;
- }
- if(temp != 0){
- boolean takenIndices[] = new boolean[intPrimeImplicants.get(i).size() + 3];
- for (int k = 0; k < intPrimeImplicants.get(i).size(); k++) {
- if (numOfOccurence[intPrimeImplicants.get(i).get(k)] != 0) {
- numOfOccurence[intPrimeImplicants.get(i).get(k)] = 0;
- takenIndices[k] = true;
- }
- }
- res.add(primeImplicants.get(i));
- planB(res);
- for (int k = 0; k < intPrimeImplicants.get(i).size(); k++) {
- if (takenIndices[k]) {
- numOfOccurence[intPrimeImplicants.get(i).get(k)] = 2;
- }
- }
- }
- }
- }
- static boolean isDashes() {
- for (int i = 0; i < resMinSOP.get(0).get(0).length(); i++) {
- if (resMinSOP.get(0).get(0).charAt(i) != '-')
- return false;
- }
- return true;
- }
- static void removeDuplicatesFromTheResult() {
- for (int i = 0; i < resMinSOP.size(); i++) {
- ArrayList<String> dup = new ArrayList<String>(resMinSOP.get(i));
- ArrayList<String> toBeRemoved;
- Collections.sort(dup);
- for (int j = resMinSOP.size() - 1; j > i; j--) {
- toBeRemoved = new ArrayList<String>(resMinSOP.get(j));
- Collections.sort(toBeRemoved);
- if (dup.equals(toBeRemoved))
- resMinSOP.remove(j);
- }
- }
- }
- static void printResult() {
- if (resMinSOP.get(0).size() == 1 && isDashes()) {
- finalResult.add("1");
- } else {
- for (int i = 0; i < resMinSOP.size(); i++) {
- String Mo2men = new String();
- for (int k = 0; k < resMinSOP.get(i).size(); k++) {
- for (int j = 0; j < bits; j++) {
- if (resMinSOP.get(i).get(k).charAt(j) == '0') {
- Mo2men += ((char) (j + 65));
- Mo2men += ("\'");
- } else if (resMinSOP.get(i).get(k).charAt(j) == '1')
- Mo2men += ((char) (j + 65));
- }
- if (k != resMinSOP.get(i).size() - 1)
- Mo2men += (" + ");
- }
- finalResult.add(Mo2men);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement