Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- public class minimumMaximum {
- public static void main(String[] args) {
- // Creates a scanner object
- Scanner inputExpression = new Scanner(System.in);
- // Reads user input
- String input = inputExpression.nextLine();
- // Splits input into an array of character strings
- String[] exp = input.split("");
- // checks for errors
- errorCheck(exp);
- // PRINTS FINAL OUTPUT
- System.out.println(calculation(combineNums(exp)));
- }
- // Performs primary calculation
- public static Integer calculation(String[] inputExp) {
- // GETS RID OF PARENS
- ArrayList<String> fullExp = new ArrayList<>(Arrays.asList(deleteParens(inputExp)));
- // BASE CASE 1
- if (fullExp.size() == 1) {
- return Integer.parseInt(fullExp.get(0));
- // BASE CASE 2
- } else if (deleteParens(fullExp.toArray(new String[fullExp.size()])).length == 1) {
- return Integer.parseInt(fullExp.get(1));
- // RECURSIVE CASE
- } else {
- // Loops thru the full expression
- for (int i = 0; i < (fullExp.size()); i++) {
- // Performs min (@) calculation
- if (isInt(fullExp.get(i)) && isInt(fullExp.get(i + 2)) && fullExp.get(i + 1).equals("@")) {
- Integer a = Integer.parseInt(fullExp.get(i));
- Integer b = Integer.parseInt(fullExp.get(i + 2));
- Integer minResult = minOperator(a, b);
- fullExp.set(i, Integer.toString(minResult));
- fullExp.remove(i+2);
- fullExp.remove(i+1);
- return calculation(deleteParens(fullExp.toArray(new String[fullExp.size()])));
- // Performs max (&) calculation
- } else if (isInt(fullExp.get(i)) && isInt(fullExp.get(i + 2)) && fullExp.get(i + 1).equals("&")) {
- Integer a = Integer.parseInt(fullExp.get(i));
- Integer b = Integer.parseInt(fullExp.get(i + 2));
- Integer maxResult = maxOperator(a, b);
- fullExp.set(i, Integer.toString(maxResult));
- fullExp.remove(i+2);
- fullExp.remove(i+1);
- return calculation(deleteParens(fullExp.toArray(new String[fullExp.size()])));
- }
- }
- // Indicates error if run
- return 99999999;
- }
- }
- // Creates the "@" operator which takes in two integers and returns the minimum
- public static Integer minOperator(Integer a, Integer b) {
- if (a < b) {
- // "a" is the min
- return a;
- } else if (b < a) {
- // "b" is the min
- return b;
- } else {
- // "a" and "b" are the same, return either
- return a;
- }
- }
- // Creates the "&" operator which takes in two integers and returns the minimum
- public static Integer maxOperator(Integer a, Integer b) {
- if (a > b) {
- // "a" is the max
- return a;
- } else if (b > a) {
- // "b" is the max
- return b;
- } else {
- // "a" and "b" are the same, return either
- return a;
- }
- }
- // combine numbers
- public static String[] combineNums(String[] arrInput) {
- // converting to array list
- ArrayList<String> rawInput = new ArrayList<>(Arrays.asList(arrInput));
- for (int i = 0; i < rawInput.size(); i++) {
- if (isInt(rawInput.get(i))) {
- String newNum = rawInput.get(i);
- int counter = 1;
- if (i == rawInput.size()-1) {
- break;
- } else if (i + counter == rawInput.size()-1) {
- if (isInt(rawInput.get(i + counter))) {
- newNum = newNum.concat(rawInput.get(i + counter));
- counter++;
- } else {
- rawInput.remove(rawInput.size()-2);
- break;
- }
- } else {
- while (isInt(rawInput.get(i + counter))) {
- newNum = newNum.concat(rawInput.get(i + counter));
- if (i + counter + 1 == rawInput.size() - 1) {
- if (isInt(rawInput.get(i + counter + 1))) {
- newNum = newNum.concat(rawInput.get(i + counter + 1));
- counter += 2;
- break;
- }
- break;
- } else {
- counter++;
- }
- }
- }
- rawInput.set(i, newNum);
- rawInput.subList(i+1, i+counter).clear();
- }
- }
- return rawInput.toArray(new String[rawInput.size()]);
- }
- public static boolean isInt(String str) {
- if (str.matches("\\d+")) {
- return true;
- } else {
- return false;
- }
- }
- public static String[] deleteParens(String[] arr) {
- ArrayList<String> arrInput = new ArrayList<>(Arrays.asList(arr));
- for (int i = 0; i < arrInput.size()-2; i++) {
- if (arrInput.get(i).equals("(") && isInt(arrInput.get(i + 1)) && arrInput.get(i + 2).equals(")")) {
- arrInput.remove(i+2);
- arrInput.remove(i);
- }
- }
- return arrInput.toArray(new String[arrInput.size()]);
- }
- // ERROR CHECKERS
- // public static void errorCheck(String input) {
- // if (input.length() > 1000) {
- // System.out.println("INVALID EXPRESSION");
- // System.exit(0);
- // } else if (input.length() - input.replace("(", "").length() != input.length() - input.replace(")", "").length()) {
- // System.out.println("INVALID EXPRESSION");
- // System.exit(0);
- // }
- // }
- public static void errorCheck(String[] inputL) {
- for (String c : inputL){
- if (!(isInt(c)) && !(c.equals("(")) && !(c.equals(")")) && !(c.equals("@")) && !(c.equals("&"))){
- System.out.println("INVALID CHARACTERS");
- System.exit(0);
- }
- }
- if (inputL.length > 1000) {
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- } else if (Arrays.toString(inputL).length() - Arrays.toString(inputL).replace("(", "").length() != Arrays.toString(inputL).length() - Arrays.toString(inputL).replace(")", "").length()) {
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- }
- for (int i = 0; i < (inputL.length - 1); i++) {
- if (inputL[i].equals(inputL[i + 1])) { // checks for "@@" or "&&" in user's input
- if (!(inputL[i].equals("(")) && !(inputL[i].equals(")")) && !(isInt(inputL[i]))) {
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- }
- } else if (inputL[i].equals("@") && inputL[i + 1].equals("&")) { // checks for "@&" in user's input
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- } else if (inputL[i].equals("&") && inputL[i + 1].equals("@")) { // checks for "&@" in user's input
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- } else if (inputL[i + 1].equals("&") && (i + 1 == inputL.length - 1)) { // checks if last character is &
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- } else if (inputL[i + 1].equals("@") && (i + 1 == inputL.length - 1)) { // checks if last character is @
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- } else if (inputL[i].equals("&") && (i == 0)) { // checks if first character is &
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- } else if (inputL[i].equals("@") && (i == 0)) { // checks if first character is @
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- } else if (inputL[i].equals("(") && inputL[i + 1].equals(")")) { // checks for cases of empty brackets "()"
- System.out.println("INVALID EXPRESSION");
- System.exit(0);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement