Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class minimumMaximum {
  6. public static void main(String[] args) {
  7.  
  8. // Creates a scanner object
  9. Scanner inputExpression = new Scanner(System.in);
  10. // Reads user input
  11. String input = inputExpression.nextLine();
  12. // Splits input into an array of character strings
  13. String[] exp = input.split("");
  14. // checks for errors
  15. errorCheck(exp);
  16. // PRINTS FINAL OUTPUT
  17. System.out.println(calculation(combineNums(exp)));
  18.  
  19. }
  20.  
  21. // Performs primary calculation
  22. public static Integer calculation(String[] inputExp) {
  23. // GETS RID OF PARENS
  24. ArrayList<String> fullExp = new ArrayList<>(Arrays.asList(deleteParens(inputExp)));
  25.  
  26. // BASE CASE 1
  27. if (fullExp.size() == 1) {
  28. return Integer.parseInt(fullExp.get(0));
  29. // BASE CASE 2
  30. } else if (deleteParens(fullExp.toArray(new String[fullExp.size()])).length == 1) {
  31.  
  32. return Integer.parseInt(fullExp.get(1));
  33.  
  34.  
  35. // RECURSIVE CASE
  36. } else {
  37.  
  38.  
  39. // Loops thru the full expression
  40. for (int i = 0; i < (fullExp.size()); i++) {
  41.  
  42. // Performs min (@) calculation
  43.  
  44. if (isInt(fullExp.get(i)) && isInt(fullExp.get(i + 2)) && fullExp.get(i + 1).equals("@")) {
  45. Integer a = Integer.parseInt(fullExp.get(i));
  46. Integer b = Integer.parseInt(fullExp.get(i + 2));
  47. Integer minResult = minOperator(a, b);
  48. fullExp.set(i, Integer.toString(minResult));
  49. fullExp.remove(i+2);
  50. fullExp.remove(i+1);
  51.  
  52. return calculation(deleteParens(fullExp.toArray(new String[fullExp.size()])));
  53.  
  54.  
  55. // Performs max (&) calculation
  56. } else if (isInt(fullExp.get(i)) && isInt(fullExp.get(i + 2)) && fullExp.get(i + 1).equals("&")) {
  57. Integer a = Integer.parseInt(fullExp.get(i));
  58. Integer b = Integer.parseInt(fullExp.get(i + 2));
  59. Integer maxResult = maxOperator(a, b);
  60. fullExp.set(i, Integer.toString(maxResult));
  61. fullExp.remove(i+2);
  62. fullExp.remove(i+1);
  63. return calculation(deleteParens(fullExp.toArray(new String[fullExp.size()])));
  64.  
  65. }
  66.  
  67.  
  68. }
  69.  
  70. // Indicates error if run
  71. return 99999999;
  72.  
  73. }
  74.  
  75. }
  76.  
  77. // Creates the "@" operator which takes in two integers and returns the minimum
  78. public static Integer minOperator(Integer a, Integer b) {
  79. if (a < b) {
  80. // "a" is the min
  81. return a;
  82. } else if (b < a) {
  83. // "b" is the min
  84. return b;
  85. } else {
  86. // "a" and "b" are the same, return either
  87. return a;
  88. }
  89. }
  90.  
  91.  
  92. // Creates the "&" operator which takes in two integers and returns the minimum
  93. public static Integer maxOperator(Integer a, Integer b) {
  94. if (a > b) {
  95. // "a" is the max
  96. return a;
  97. } else if (b > a) {
  98. // "b" is the max
  99. return b;
  100. } else {
  101. // "a" and "b" are the same, return either
  102. return a;
  103. }
  104. }
  105.  
  106. // combine numbers
  107. public static String[] combineNums(String[] arrInput) {
  108. // converting to array list
  109. ArrayList<String> rawInput = new ArrayList<>(Arrays.asList(arrInput));
  110.  
  111. for (int i = 0; i < rawInput.size(); i++) {
  112. if (isInt(rawInput.get(i))) {
  113. String newNum = rawInput.get(i);
  114. int counter = 1;
  115.  
  116. if (i == rawInput.size()-1) {
  117. break;
  118. } else if (i + counter == rawInput.size()-1) {
  119. if (isInt(rawInput.get(i + counter))) {
  120. newNum = newNum.concat(rawInput.get(i + counter));
  121. counter++;
  122. } else {
  123. rawInput.remove(rawInput.size()-2);
  124. break;
  125. }
  126. } else {
  127. while (isInt(rawInput.get(i + counter))) {
  128. newNum = newNum.concat(rawInput.get(i + counter));
  129.  
  130. if (i + counter + 1 == rawInput.size() - 1) {
  131. if (isInt(rawInput.get(i + counter + 1))) {
  132. newNum = newNum.concat(rawInput.get(i + counter + 1));
  133. counter += 2;
  134. break;
  135. }
  136. break;
  137. } else {
  138. counter++;
  139. }
  140. }
  141. }
  142.  
  143. rawInput.set(i, newNum);
  144. rawInput.subList(i+1, i+counter).clear();
  145. }
  146.  
  147. }
  148.  
  149. return rawInput.toArray(new String[rawInput.size()]);
  150. }
  151.  
  152.  
  153. public static boolean isInt(String str) {
  154. if (str.matches("\\d+")) {
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. }
  160.  
  161. public static String[] deleteParens(String[] arr) {
  162. ArrayList<String> arrInput = new ArrayList<>(Arrays.asList(arr));
  163.  
  164. for (int i = 0; i < arrInput.size()-2; i++) {
  165. if (arrInput.get(i).equals("(") && isInt(arrInput.get(i + 1)) && arrInput.get(i + 2).equals(")")) {
  166. arrInput.remove(i+2);
  167. arrInput.remove(i);
  168. }
  169. }
  170. return arrInput.toArray(new String[arrInput.size()]);
  171. }
  172.  
  173. // ERROR CHECKERS
  174. // public static void errorCheck(String input) {
  175. // if (input.length() > 1000) {
  176. // System.out.println("INVALID EXPRESSION");
  177. // System.exit(0);
  178. // } else if (input.length() - input.replace("(", "").length() != input.length() - input.replace(")", "").length()) {
  179. // System.out.println("INVALID EXPRESSION");
  180. // System.exit(0);
  181. // }
  182. // }
  183.  
  184. public static void errorCheck(String[] inputL) {
  185. for (String c : inputL){
  186. if (!(isInt(c)) && !(c.equals("(")) && !(c.equals(")")) && !(c.equals("@")) && !(c.equals("&"))){
  187. System.out.println("INVALID CHARACTERS");
  188. System.exit(0);
  189. }
  190. }
  191.  
  192. if (inputL.length > 1000) {
  193. System.out.println("INVALID EXPRESSION");
  194. System.exit(0);
  195. } else if (Arrays.toString(inputL).length() - Arrays.toString(inputL).replace("(", "").length() != Arrays.toString(inputL).length() - Arrays.toString(inputL).replace(")", "").length()) {
  196. System.out.println("INVALID EXPRESSION");
  197. System.exit(0);
  198. }
  199.  
  200.  
  201. for (int i = 0; i < (inputL.length - 1); i++) {
  202. if (inputL[i].equals(inputL[i + 1])) { // checks for "@@" or "&&" in user's input
  203. if (!(inputL[i].equals("(")) && !(inputL[i].equals(")")) && !(isInt(inputL[i]))) {
  204. System.out.println("INVALID EXPRESSION");
  205. System.exit(0);
  206. }
  207. } else if (inputL[i].equals("@") && inputL[i + 1].equals("&")) { // checks for "@&" in user's input
  208. System.out.println("INVALID EXPRESSION");
  209. System.exit(0);
  210. } else if (inputL[i].equals("&") && inputL[i + 1].equals("@")) { // checks for "&@" in user's input
  211. System.out.println("INVALID EXPRESSION");
  212. System.exit(0);
  213. } else if (inputL[i + 1].equals("&") && (i + 1 == inputL.length - 1)) { // checks if last character is &
  214. System.out.println("INVALID EXPRESSION");
  215. System.exit(0);
  216. } else if (inputL[i + 1].equals("@") && (i + 1 == inputL.length - 1)) { // checks if last character is @
  217. System.out.println("INVALID EXPRESSION");
  218. System.exit(0);
  219. } else if (inputL[i].equals("&") && (i == 0)) { // checks if first character is &
  220. System.out.println("INVALID EXPRESSION");
  221. System.exit(0);
  222. } else if (inputL[i].equals("@") && (i == 0)) { // checks if first character is @
  223. System.out.println("INVALID EXPRESSION");
  224. System.exit(0);
  225. } else if (inputL[i].equals("(") && inputL[i + 1].equals(")")) { // checks for cases of empty brackets "()"
  226. System.out.println("INVALID EXPRESSION");
  227. System.exit(0);
  228. }
  229. }
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement