//Audrey Shi //ACSL Contest #4 //Senior Division //Montgomery Blair High School #3008 import java.util.Scanner; import java.util.ArrayList; public class PIP { private static String input; private static int parenCount; private static int paren1; private static int paren1End; private static int paren2; private static int paren2End; private static int[] operatorPos = new int[3]; private static int[] operatorType = new int[3]; private static ArrayList positionDone; public static void main(String[] args) { System.out.println("Welcome to Audrey's ACSL PIP program."); System.out.println(); getInput(); System.out.println(); System.out.println("Thank you for using Audrey's ACSL PIP program!"); } public static void getInput() { Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { System.out.print(i + 1); System.out.print(": "); // Initiating input = ""; parenCount = 0; paren1 = 0; paren1End = 0; paren2 = 0; paren2End = 0; for (int j = 0; j < 3; j++) { operatorPos[j] = 0; operatorType[j] = 0; } positionDone = new ArrayList(); input = sc.nextLine().trim(); findPos(input); // Paren 2 mult, add/sub if (parenCount == 2) { // System.out.println("Paren 2 mult"); doOperator(1, true, 2); // System.out.println("Paren 2 add/sub"); doOperator(2, true, 2); } // Paren 1 mult, add/sub if (parenCount == 2 || parenCount == 1) { // System.out.println("Paren 1 mult"); doOperator(1, true, 1); // System.out.println("Paren 1 add/sub"); doOperator(2, true, 1); } // No paren mult, add/sub // System.out.println("No paren mult"); doOperator(1, false, 1); // System.out.println("No paren add/sub"); doOperator(2, false, 1); removeParen(); System.out.println(input); } } public static void findPos(String str) { // Find positions of parentheses paren1 = input.indexOf("("); paren2 = input.indexOf("(", paren1+1); // paren2 = input.substring(paren1+1).indexOf("("); // if (paren2 != -1) // paren2 = paren2 + input.substring(0, paren1+1).length(); // System.out.println("paren1: " + paren1); // System.out.println("paren2: " + paren2); if (paren2 != -1) { paren2End = findParen(paren2); parenCount = 2; } if (paren1 != -1) { paren1End = findParen(paren1); if (paren2 == -1) parenCount = 1; } // Find positions of operators // Type 1 is multiplication, Type 2 is addition or subtraction for (int i = 0; i < input.length(); i++) { if (input.substring(i, i+1).equals("*")) { traverse: for (int j = 0; j < 3; j++) { if (operatorPos[j] == 0) { operatorPos[j] = i; operatorType[j] = 1; break traverse; } } } if (input.substring(i, i+1).equals("+") || input.substring(i, i+1).equals("-")) { traverse: for (int j = 0; j < 3; j++) { if (operatorPos[j] == 0) { operatorPos[j] = i; operatorType[j] = 2; break traverse; } } } } /* System.out.println("OperatorPos, OperatorType"); for (int i = 0; i < 3; i++) { System.out.println(operatorPos[i] + ", " + operatorType[i]); } System.out.println("Paren1 and end: " + paren1 + ", " + paren1End); System.out.println("Paren2 and end: " + paren2 + ", " + paren2End); System.out.println("Paren count: " + parenCount); System.out.println(); */ } public static void doOperator(int type, boolean paren, int parenType) { if (paren == true) { // 2nd order if (parenType == 2) { for (int i = 0; i < 3; i++) { if (operatorType[i] == type) { if (operatorPos[i] > paren2 && operatorPos[i] < paren2End) { convertPre(operatorPos[i], true); operatorPos[i] = 0; operatorType[i] = 0; } } } } // 1st order if (parenType == 1) { for (int i = 0; i < 3; i++) { if (operatorType[i] == type) { if (operatorPos[i] > paren1 && operatorPos[i] < paren1End) { convertPre(operatorPos[i], true); operatorPos[i] = 0; operatorType[i] = 0; } } } } } else { // 0th order for (int i = 0; i < 3; i++) { if (operatorType[i] == type) { convertPre(operatorPos[i], false); operatorPos[i] = 0; operatorType[i] = 0; } } } } public static void convertPre(int operatorPos, boolean paren) { String operator = input.substring(operatorPos, operatorPos+1); int startMarker = operatorPos - 1; if (positionDone.contains(startMarker)) startMarker = positionDone.get(positionDone.indexOf(startMarker) - 1); int endMarker = operatorPos + 1; if (positionDone.contains(endMarker)) endMarker = positionDone.get(positionDone.indexOf(endMarker) + 1); /* System.out.println("OperatorPos: " + operatorPos); System.out.println("StartMarker: " + startMarker); */ // if (startMarker == 0) // input = operator + input.substring(startMarker, operatorPos) + input.substring(operatorPos+1); // else input = input.substring(0, startMarker) + operator + input.substring(startMarker, operatorPos) + input.substring(operatorPos+1); if (paren == true) { if (input.substring(startMarker-1, startMarker).equals("(") && input.substring(endMarker+1, endMarker+2).equals(")")) { // if (input.substring(startMarker-1, startMarker).equals("(") && (input.substring(endMarker+1).equals(")") || input.substring(endMarker+1, endMarker+2).equals(")"))) { startMarker = startMarker - 1; endMarker = endMarker + 1; } } for (int i = 0; i < positionDone.size(); i+=2) { if (positionDone.get(i) >= startMarker && positionDone.get(i+1) <= endMarker) { positionDone.set(i, -1); positionDone.set(i+1, -1); } } positionDone.add(startMarker); positionDone.add(endMarker); /* System.out.println("Input is " + input); System.out.println("PositionDone: "); for (int i = 0; i < positionDone.size(); i++) System.out.print(positionDone.get(i) + ", "); System.out.println(); */ } public static int findParen(int marker) { boolean nested = false; int parenMarker = 0; traverse: for (int i = marker + 1; i < input.length(); i++) { if (input.substring(i, i+1).equals("(")) nested = true; if (input.substring(i, i+1).equals(")")) { if (nested == true) nested = false; else if (nested == false) { parenMarker = i; break traverse; } } } return parenMarker; } public static void removeParen() { while (input.indexOf("(") != -1 || input.indexOf(")") != -1) { input = input.substring(0, input.indexOf("(")) + input.substring(input.indexOf("(")+1); input = input.substring(0, input.indexOf(")")) + input.substring(input.indexOf(")")+1); } }