import java.util.*; /** * Write a description of class Main here. * * @author (your name) * @version (a version number or a date) */ public class Main { private static Scanner sc; /** * Constructor for objects of class Main */ public Main() { } /********************************************************** * */ public static void main(String[] args) { Logic logic = new Logic(); Interface iF = new Interface(); System.out.println("Welcome to the Logic calculator."); System.out.println("Please enter the initial number of propositions."); System.out.println("You can either just type the number or the number and the names of the propositions"); sc = new Scanner(System.in); System.out.print("> "); StringTokenizer full = new StringTokenizer(sc.nextLine()); String number = full.nextToken(); try { if(!full.hasMoreTokens()) logic.start(Integer.parseInt(number)); else { String[] names = new String[Integer.parseInt(number)]; for(int i = 0; full.hasMoreTokens(); i++) names[i] = full.nextToken(); logic.start(Integer.parseInt(number),names); } } catch(Exception e) { System.out.println("Error! " + e + ". Either type in a number and the calculator will"); System.out.println("give the propositions default names or type in the number followed by the names."); System.out.println("(Note: the amount of names cannot exceed the number)"); iF.callMain(); } iF.printMethods(); System.out.print("> "); for(boolean done = false; !done;) { try { String tmp = sc.nextLine(); boolean ident = false; if(!tmp.trim().toLowerCase().contains("done")) { StringTokenizer tmpST = new StringTokenizer(tmp); String identifier = tmpST.nextToken(); String alpha = identifier; String operator = ""; if(tmpST.hasMoreTokens()) operator = tmpST.nextToken(); String beta = ""; if(iF.propSearcher(logic,alpha) || alpha.trim().toLowerCase().equals("~")) { if(tmpST.hasMoreTokens()) beta = tmpST.nextToken(); if(operator.equals("v")) { alpha = logic.disjunction(alpha,beta); } else if(operator.equals("&")) { alpha = logic.conjunction(alpha,beta); } else if(operator.equals("<->")) { alpha = logic.biconditional(alpha,beta); } else if(operator.equals(">")) { alpha = logic.conditional(alpha,beta); } else if(operator.equals("~")) { alpha = logic.negation(alpha); } else if(alpha.equals("~")) { ident = true; alpha = logic.negation(beta); iF.printSpecific(logic, alpha); alpha = logic.negation(operator); iF.printSpecific(logic, alpha); while(tmpST.hasMoreTokens()) { String x = tmpST.nextToken(); alpha = logic.negation(x); iF.printSpecific(logic, alpha); } } else System.out.println("Error! Please enter an operator (& v > <->). Thank You."); if(!ident) iF.printSpecific(logic,alpha); } else if(identifier.equals("PRINT")) { String method = operator; if(method.equals("print")) iF.print(logic); else if(method.equals("length")) System.out.println("The number of rows is " + logic.getLength()); else if(method.equals("keys")) System.out.println("The propositions are " + logic.getKeys()); else if(method.equals("number")) System.out.println("The number of proposisitions is " + logic.getNumberOfPropositions()); else { System.out.println("Error! Please type either 'print', 'printkv', 'length', 'keys', 'number of propositions'"); System.out.println("or 'specific' followed by the specific proposition. Thank You."); } } else if(identifier.equals("EDIT")) { String edit = operator; if(edit.equals("change")) { String change = tmpST.nextToken().trim().toLowerCase(); if(change.equals("name")) { String initial = tmpST.nextToken(); for(String temp = tmpST.nextToken(); !temp.equals("to"); temp = tmpST.nextToken()) initial += "." + temp; String fiNal = tmpST.nextToken(); while(tmpST.hasMoreTokens()) fiNal += "." + tmpST.nextToken(); logic.changeNameP(initial,fiNal); } } } else if(identifier.equals("CHECK")) { String method = operator; if(method.equals("tautology")) { if(logic.isTautology(tmpST.nextToken())) System.out.println("Is a Tautology"); else System.out.println("Is Not a Tautology"); } else if(method.contains("logic")) { if(logic.isLogicallyEquivalent(tmpST.nextToken(), tmpST.nextToken())) System.out.println("Is Logically Equivalent"); else System.out.println("Is Not Logically Equivalent"); } else if(method.equals("valid")) { String[] temp = new String[tmpST.countTokens() - 1]; for(int i = 0; i < temp.length; i++) temp[i] = tmpST.nextToken(); if(logic.isValidArg(temp,tmpST.nextToken())) System.out.println("Is a Valid Argument"); else System.out.println("Is Not a Valid Argument"); } } else if(identifier.equals("FIND")) { if(operator.equals("tautology")) { ArrayList finder = logic.findTautology(); for(int i = 0; i < finder.size(); i++) iF.printSpecific(logic,finder.get(i)); } else if(operator.contains("logic")) { boolean retTmp = false; int print = 1; ArrayList> tmp1 = logic.findLogicallyEquivalent(); for(int i = 0; i < tmp1.size(); i++) { if(tmp1.get(i).size() > 1) { retTmp = true; System.out.println("Logically Equivalent Set #" + print++ + ":"); for(int j = 0; j < tmp1.get(i).size(); j++) iF.printSpecific(logic,tmp1.get(i).get(j)); } } if(!retTmp) System.out.println("No Logically Equivalent Propositions."); } } else if(identifier.trim().toLowerCase().equals("help")) iF.printMethods(); System.out.print("> "); } else done = true; } catch(Exception e) { System.out.println("Error! " + e + ". If you continue to experience problems please contact"); System.out.println("tech support at yitzchokpinkesz@yahoo.com."); System.out.print("> "); } } } }