-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 93cb8e1
Showing
6 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class block | ||
{ | ||
int val,block_num; | ||
boolean visited; | ||
block(int n) | ||
{ | ||
this.val=0;this.block_num=n; | ||
this.visited=false;; | ||
} | ||
|
||
void visit() | ||
{ | ||
this.visited=true; | ||
} | ||
|
||
void input(int v) | ||
{ | ||
this.val=v; | ||
} | ||
|
||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import java.util.*; | ||
class karnaugh_inp | ||
{ | ||
boolean pot(int n) | ||
{ | ||
/*If we subtract a power of 2 numbers by 1 | ||
* then all unset bits after the only set bit become set; | ||
* and the set bit becomes unset.*/ | ||
return n!=0 && ((n&(n-1)) == 0); | ||
} | ||
|
||
int getMaxMin(int n,int v) | ||
{ | ||
int i=n; | ||
while(true) | ||
{ | ||
if(pot(i)) return i; | ||
i+=v; | ||
} | ||
} | ||
|
||
block[][] shape(int n) | ||
{ | ||
int col=getMaxMin(n,1); | ||
return new block[((int) Math.pow(2,n))/col][col]; | ||
} | ||
|
||
void k_fill(block arr[],int s,int e,int n,int code, int tb) | ||
{ | ||
if((e-s)%4==0) | ||
{ | ||
if(code ==-1) | ||
{ | ||
k_fill(arr,(s+e)/2,e,n,code,tb); | ||
k_fill(arr,s,(s+e)/2,((s+e)/2),-1*code,tb); | ||
return; | ||
} | ||
k_fill(arr,s,(s+e)/2,n,code,tb); | ||
k_fill(arr,(s+e)/2,e,((s+e)/2),-1*code,tb); | ||
return; | ||
} | ||
if(code==1) | ||
{ | ||
for(int i=s;i<e;i++) | ||
{ | ||
arr[i]=new block(n+tb); | ||
n++; | ||
} | ||
return; | ||
} | ||
for(int i=e-1;i>=s;i--) | ||
{ | ||
arr[i]=new block(n+tb); | ||
n++; | ||
} | ||
} | ||
|
||
void fill(block a[][],int code) | ||
{ | ||
int val=0; | ||
for(int r=0;r<a.length/2;r++) | ||
{ | ||
k_fill(a[r],0,a[0].length,0,1,val); | ||
val+=a[0].length; | ||
} | ||
for(int r=a.length-1;r>=a.length/2;r--) | ||
{ | ||
k_fill(a[r],0,a[0].length,0,1,val); | ||
val+=a[0].length; | ||
} | ||
} | ||
|
||
void print(block a[][],String type) | ||
{ | ||
System.out.println(); | ||
System.out.println("Karnugh Map:"); | ||
System.out.println(); | ||
for(int r=0;r<a.length;r++) | ||
{ | ||
for(int c=0;c<a[0].length;c++) | ||
{ | ||
String p = "10"; | ||
System.out.print(a[r][c].val+" "); | ||
} | ||
System.out.println("\n"); | ||
} | ||
} | ||
|
||
|
||
String[] input() | ||
{ | ||
System.out.println("Enter Cardinal Expression in format(f(X,Y,Z) = S/P(0,1,2....) ) where S for SOP and P for POS : "); | ||
String in = new Scanner(System.in).nextLine(); | ||
|
||
String v = in.split("=")[0].trim(); | ||
String x = in.split("=")[1].trim(); | ||
|
||
v=(v.substring(v.indexOf("(")+1,v.lastIndexOf(")"))).replace(",",""); | ||
|
||
return new String[] {x.charAt(0)+"",v,x}; | ||
} | ||
|
||
void load(int n,block a[][],String x) | ||
{ | ||
List<Integer> ele = new ArrayList<Integer>(); | ||
int co=1; | ||
|
||
String nums[]=(x.substring(x.indexOf("(")+1,x.lastIndexOf(")"))).split(","); | ||
|
||
for(int i=0;i<nums.length;i++) | ||
{ | ||
ele.add(Integer.parseInt(nums[i])); | ||
} | ||
|
||
for(int r=0;r<a.length;r++) | ||
{ | ||
for(int c=0;c<a[0].length;c++) | ||
{ | ||
int t = a[r][c].block_num; | ||
if(ele.contains(t)) | ||
{ | ||
a[r][c].val=1; | ||
ele.remove(new Integer(t)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void main() | ||
{ | ||
System.out.println("WELCOME TO FREDERICK\'S KARNAUGH MAP SOLVER"); | ||
System.out.println(); | ||
|
||
String r[]=input(); | ||
int n =r[1].length(); | ||
|
||
block a[][] = shape(n); | ||
//System.out.println(a.length+" "+a[0].length); | ||
fill(a,1); | ||
load(n,a,r[2]); | ||
print(a,r[0]); | ||
|
||
karnaugh_process p = new karnaugh_process(); | ||
p.get(a,r[0],n,r[1]); | ||
|
||
System.out.println("Resultant Expression: "); | ||
System.out.println(p.getResult()); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import java.util.*; | ||
class karnaugh_process | ||
{ | ||
String exp=""; | ||
String type=""; | ||
String vars=""; | ||
int n=0; | ||
boolean pot(int n) | ||
{ | ||
return n!=0 && ((n&(n-1)) == 0); } | ||
|
||
void check(block a[][],int sr,int sc,int er,int ec) | ||
{ | ||
|
||
int nr=a.length;int nc=a[0].length; | ||
int sum=0; | ||
for(int r=sr;r<=er;r++) | ||
{ | ||
for(int c=sc;c<=ec;c++) | ||
{ | ||
int pr=(r<nr)?r:r-nr; | ||
int pc=(c<nc)?c:c-nc; | ||
//System.out.println(r+" "+nr+" "+pr+" "+c+" "+nc+" "+pc); | ||
sum+=a[pr][pc].val; | ||
} | ||
} | ||
|
||
if(sum!=((er-sr+1)*(ec-sc+1))|| !pot(sum) || sum==0) return; | ||
|
||
List<Integer> bnum = verify(a,sr,sc,er,ec); | ||
if(bnum==null) | ||
{ | ||
return; | ||
} | ||
|
||
/*for(int i:bnum) | ||
{ | ||
System.out.println(i); | ||
}*/ | ||
|
||
//print(a,sr,sc,er,ec); | ||
this.exp+=simplify(bnum)+((this.type.equals("S"))?"+":"."); | ||
//System.out.println("------------------------"); | ||
} | ||
|
||
String getResult() | ||
{ | ||
return this.exp.substring(0,this.exp.length()-1); | ||
} | ||
|
||
List<Integer> verify(block a[][],int sr,int sc,int er,int ec) | ||
{ | ||
List<Integer> bnum = new ArrayList<Integer>(); | ||
|
||
int nr=a.length;int nc=a[0].length;boolean ce=true; | ||
for(int r=sr;r<=er;r++) | ||
{ | ||
for(int c=sc;c<=ec;c++) | ||
{ | ||
int pr=(r<nr)?r:r-nr; | ||
int pc=(c<nc)?c:c-nc; | ||
if(a[pr][pc].visited==false) | ||
{ | ||
ce=false; | ||
a[pr][pc].visit(); | ||
} | ||
bnum.add(a[pr][pc].block_num); | ||
} | ||
} | ||
return (!ce)?bnum:null; | ||
} | ||
|
||
void print(block a[][],int sr,int sc,int er,int ec) | ||
{ | ||
int nr=a.length;int nc=a[0].length; | ||
for(int r=sr;r<=er;r++) | ||
{ | ||
for(int c=sc;c<=ec;c++) | ||
{ | ||
int pr=(r<nr)?r:r-nr; | ||
int pc=(c<nc)?c:c-nc; | ||
System.out.print(a[pr][pc].val+" "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
||
|
||
void get(block a[][],String type,int n,String vars) | ||
{ | ||
this.type=type; this.n=n;this.vars=vars; | ||
int nr=a.length;int nc=a[0].length; | ||
|
||
for(int sr=nr-1;sr>=0;sr--) | ||
{ | ||
for(int sc=nc-1;sc>=0;sc--) | ||
{ | ||
for(int r=nr-1;r>=0;r--) | ||
{ | ||
for(int c=nc-1;c>=0;c--) | ||
{ | ||
if(sr==sc && sc==0) continue; | ||
check(a,r,c,r+sr,c+sc); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
String getBinary(int n,int l) | ||
{ | ||
String res=""; | ||
while(n>=1) | ||
{ | ||
if(n==1){res="1"+res;break;} | ||
res=(n%2)+res; | ||
n=n/2; | ||
} | ||
while(res.length()!=l) | ||
{ | ||
res="0"+res; | ||
} | ||
return res; | ||
} | ||
|
||
char possible(String[] a,int in) | ||
{ | ||
char c = a[0].charAt(in); | ||
for(int i=1;i<a.length;i++) | ||
{ | ||
if(c!=a[i].charAt(in)) | ||
{ | ||
return ' '; | ||
} | ||
} | ||
return c; | ||
} | ||
|
||
String getRes(String a[]) | ||
{ | ||
String res=""; | ||
/*for(int i=1;i<=this.n;i++) | ||
{ | ||
code+=(char) (64+i)+""; | ||
}*/ | ||
|
||
for(int i=0;i<this.n;i++) | ||
{ | ||
char c = possible(a,i); | ||
if(c==' ') {continue;} | ||
res+=this.vars.charAt(i); | ||
if((c=='0' && this.type.equals("S"))||(c=='1' && this.type.equals("P"))) | ||
{ | ||
res+="'"; | ||
} | ||
res+=(this.type.equals("S"))?".":"+"; | ||
} | ||
if(res.isEmpty()) | ||
{ | ||
return (this.type.equals("S"))?"1":"0"; | ||
} | ||
return res.substring(0,res.length()-1); | ||
} | ||
|
||
String simplify(List<Integer> bnum) | ||
{ | ||
String bins[]=new String[bnum.size()]; | ||
for(int i=0;i<bins.length;i++) | ||
{ | ||
bins[i]=getBinary(bnum.get(i),this.n); | ||
} | ||
return "("+getRes(bins)+")"; | ||
} | ||
} |