Hi,Dear all friends, i have projects for my lessons in C#,I need to use this code too in that one,but the problem is,it's java,could you please help me to convert it ?!
(Is there any program that convert it automatically ??!!)
my biggest problem is in converting this one
Java : ArrayList <term> ...(in C# ??)
Thanks alot
_________________
2 class
1.
__
class Term {
public static final byte DontCare = 2;
public Term(byte[] varVals) {
this.varVals = varVals;
}
public int getNumVars() {
return varVals.length;
}
public String toString() {
String result = "{";
for(int i=0; i<varVals.length; i++) {
if (varVals[i] == DontCare)
result += "X";
else
result += varVals[i];
result += " ";
}
result += "}";
return result;
}
public Term combine(Term term) {
int diffVarNum = -1;
for(int i=0; i<varVals.length; i++) {
if (this.varVals[i] != term.varVals[i]) {
if (diffVarNum == -1) {
diffVarNum = i;
} else {
return null;
}
}
}
if (diffVarNum == -1) {
return null;
}
byte[] resultVars = varVals.clone();
resultVars[diffVarNum] = DontCare;
return new Term(resultVars);
}
public int countValues(byte value) {
int result = 0;
for(int i=0; i<varVals.length; i++) {
if (varVals[i] == value) {
result++;
}
}
return result;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (o == null || !getClass().equals(o.getClass())) {
return false;
} else {
Term rhs = (Term)o;
return Arrays.equals(this.varVals, rhs.varVals);
}
}
public int hashCode() {
return varVals.hashCode();
}
boolean implies(Term term) {
for(int i=0; i<varVals.length; i++) {
if (this.varVals[i] != DontCare &&
this.varVals[i] != term.varVals[i]) {
return false;
}
}
return true;
}
public static Term read(Reader reader) throws IOException {
int c = '\0';
ArrayList<Byte> t = new ArrayList<Byte>();
while (c != '\n' && c != -1) {
c = reader.read();
if (c == '0') {
t.add((byte)0);
} else if (c == '1') {
t.add((byte)1);
}
}
if (t.size() > 0) {
byte[] resultBytes = new byte[t.size()];
for(int i=0; i<t.size(); i++) {
resultBytes[i] = (byte)t.get(i);
}
return new Term(resultBytes);
} else {
return null;
}
}
private byte[] varVals;
}
2.
____
class Formula {
public Formula(List<Term> termList) {
this.termList = termList;
}
public String toString() {
String result = "";
result += termList.size() + " terms, " + termList.get(0).getNumVars() + " variables\n";
for(int i=0; i<termList.size(); i++) {
result += termList.get(i) + "\n";
}
return result;
}
@SuppressWarnings("unchecked")
public void reduceToPrimeImplicants() {
originalTermList = new ArrayList<Term>(termList);
int numVars = termList.get(0).getNumVars();
ArrayList<Term>[][] table = new ArrayList[numVars + 1][numVars + 1];
for(int dontKnows=0; dontKnows <= numVars; dontKnows++) {
for(int ones=0; ones <= numVars; ones++) {
table[dontKnows][ones] = new ArrayList<Term>();
}
}
for(int i=0; i<termList.size(); i++) {
int dontCares = termList.get(i).countValues(Term.DontCare);
int ones = termList.get(i).countValues((byte)1);
table[dontCares][ones].add(termList.get(i));
}
for(int dontKnows=0; dontKnows <= numVars - 1; dontKnows++) {
for(int ones=0; ones <= numVars - 1; ones++) {
ArrayList<Term> left = table[dontKnows][ones];
ArrayList<Term> right = table[dontKnows][ones + 1];
ArrayList<Term> out = table[dontKnows+1][ones];
for(int leftIdx = 0; leftIdx < left.size(); leftIdx++) {
for(int rightIdx = 0; rightIdx < right.size(); rightIdx++) {
Term combined = left.get(leftIdx).combine(right.get(rightIdx));
if (combined != null) {
if (!out.contains(combined)) {
out.add(combined);
}
termList.remove(left.get(leftIdx));
termList.remove(right.get(rightIdx));
if (!termList.contains(combined)) {
termList.add(combined);
}
}
}
}
}
}
}
public void reducePrimeImplicantsToSubset() {
int numPrimeImplicants = termList.size();
int numOriginalTerms = originalTermList.size();
boolean[][] table = new boolean[numPrimeImplicants][numOriginalTerms];
for (int impl=0; impl < numPrimeImplicants; impl++) {
for (int term=0; term < numOriginalTerms; term++) {
table[impl][term] = termList.get(impl).implies(originalTermList.get(term));
}
}
ArrayList<Term> newTermList = new ArrayList<Term>();
boolean done = false;
int impl;
while (!done) {
impl = extractEssentialImplicant(table);
if (impl != -1) {
newTermList.add(termList.get(impl));
} else {
impl = extractLargestImplicant(table);
if (impl != -1) {
newTermList.add(termList.get(impl));
} else {
done = true;
}
}
}
termList = newTermList;
originalTermList = null;
}
public static Formula read(Reader reader) throws IOException {
ArrayList<Term> terms = new ArrayList<Term>();
Term term;
while ((term = Term.read(reader)) != null) {
terms.add(term);
}
return new Formula(terms);
}
private int extractEssentialImplicant(boolean[][] table) {
for (int term=0; term < table[0].length; term++) {
int lastImplFound = -1;
for (int impl=0; impl < table.length; impl++) {
if (table[impl][term]) {
if (lastImplFound == -1) {
lastImplFound = impl;
} else {
lastImplFound = -1;
break;
}
}
}
if (lastImplFound != -1) {
extractImplicant(table, lastImplFound);
return lastImplFound;
}
}
return -1;
}
private void extractImplicant(boolean[][] table, int impl) {
for (int term=0; term < table[0].length; term++) {
if (table[impl][term]) {
for (int impl2=0; impl2 < table.length; impl2++) {
table[impl2][term] = false;
}
}
}
}
private int extractLargestImplicant(boolean[][] table) {
int maxNumTerms = 0;
int maxNumTermsImpl = -1;
for (int impl=0; impl < table.length; impl++) {
int numTerms = 0;
for (int term=0; term < table[0].length; term++) {
if (table[impl][term]) {
numTerms++;
}
}
if (numTerms > maxNumTerms) {
maxNumTerms = numTerms;
maxNumTermsImpl = impl;
}
}
if (maxNumTermsImpl != -1) {
extractImplicant(table, maxNumTermsImpl);
return maxNumTermsImpl;
}
return -1;
}
private List<Term> termList;
private List<Term> originalTermList;
public static void main(String[] args) throws IOException {
Formula f = Formula.read(new BufferedReader(new FileReader(args[0])));
f.reduceToPrimeImplicants();
System.out.println(f);
f.reducePrimeImplicantsToSubset();
System.out.println(f);
}
}