Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.lang.Object;
- public class Minimization {
- ArrayList <String> minTerms = new ArrayList();
- ArrayList<String> commonElements = new ArrayList();
- int n , index ;
- char[] variables;
- String[] compVariables;
- public void setVariables(String minTerm) {
- Scanner read = new Scanner(minTerm);
- String word = minTerm ;
- Scanner scan = new Scanner(word);
- int max = 0;
- n=0 ;
- int k = 0 ;
- while (scan.hasNext()) {
- int y = scan.nextInt();
- if (y > max)
- max = y;
- }
- n = (int) Math.ceil(Math.log(max + 1) / Math.log(2));
- while (read.hasNext()) {
- int x = read.nextInt();
- commonElements.add(String.valueOf(x));
- String number = convertBinary(x);
- minTerms.add(number);
- }
- // variables = new char[n];
- // compVariables = new String[n];
- // for (int i = 0; i < n; i++) {
- // variables[i] = (char) ('A' + i);
- // compVariables[i] = variables[i] + "'";
- // }
- }
- // public int countOccurences(String binary) {
- // return (binary.length() - binary.replace("1", "").length());
- // }
- public String convertBinary(int minterm) {
- String result = "" ;
- for (int j = n - 1; j >= 0; j--) {
- if (minterm < Math.pow(2, j))
- result += "0";
- else {
- result += "1";
- minterm = (int) (minterm - Math.pow(2, j));
- }
- }
- return result ;
- }
- public void minimization (){
- boolean flag = true ;
- int Size = minTerms.size();
- while (flag) {
- for (int i =0 ; i < Size ; i++ ) {
- flag = false ;
- for (int j =i+1 ; j < Size; j++ ) {
- if (ComparingTerms(minTerms.get(i),minTerms.get(j))) {
- SetDashes(minTerms.get(i), index);
- String s = new String();
- s += commonElements.get(i);
- s += ",";
- s += commonElements.get(j);
- commonElements.add(s);
- flag = true ;
- }
- }
- }
- }
- for(int l = 0 ; l< Size ; l++) // print minTerms >> just for testing
- System.out.println(minTerms.get(l));
- for(int l = 0 ; l< commonElements.size() ; l++)
- System.out.println(commonElements.get(l));
- }
- public boolean ComparingTerms (String a , String b) {
- index = 0 ;
- int k = 0 ;
- int count = 0 ;
- for (int i = 0 ; i< a.length() ; i++)
- {
- if (a.charAt(i)!= b.charAt(i)){
- count++ ;
- k = i ;
- }
- }
- if (count == 1){
- index = k ;
- return true ;
- }
- return false ;
- }
- public void SetDashes (String a , int x) {
- String s = a ;
- StringBuilder myBuilder = new StringBuilder(s);
- myBuilder.setCharAt(x, '-');
- s = myBuilder.toString();
- minTerms.add(s);
- }
- public static void main(String[] args)
- {
- String min = "1 2 4 5 6 12 13 14";
- Minimization l = new Minimization() ;
- l.setVariables(min);
- l.minimization();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement