Advertisement
iskren_penev

Dragon#

Apr 12th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package october2015;
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5.  
  6. public class DragonSharp {
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         int lines = Integer.parseInt(scan.nextLine());
  10.         String[] input = new String[lines];
  11.         Pattern patternif = Pattern.compile("[a-z]+ \\(([0-9]+)(\\=\\=|\\>|\\<)+([0-9]+)\\) (.*out) (\\\"(.+)\\\"\\;)");
  12.         Pattern patternelse = Pattern.compile("else (.*out) (\\\"(.+)\\\"\\;)");
  13.  
  14.         int validCounter = 0;
  15.         int trueCount = 0;
  16.  
  17.         for (int i = 0; i < lines; i++) {
  18.             input[i] = scan.nextLine();
  19.             Matcher matchIf = patternif.matcher(input[i]);
  20.             Matcher matchElse = patternelse.matcher(input[i]);
  21.             if (matchIf.find()) {
  22.                 validCounter++;
  23.             } else if (matchElse.find()) {
  24.                 validCounter++;
  25.             } else { break;}
  26.         }
  27.         if (validCounter == lines) {
  28.             for (int i = 0; i < lines; i++) {
  29.                 Matcher matchIf = patternif.matcher(input[i]);
  30.                 Matcher matchElse = patternelse.matcher(input[i]);
  31.                 if (matchIf.find()) {
  32.                     boolean isTrue = false;
  33.                     int num1 = Integer.parseInt(matchIf.group(1));
  34.                     int num2 = Integer.parseInt(matchIf.group(3));
  35.                     String condition = matchIf.group(2);
  36.                     if (condition.equals("==") && num1 == num2) {
  37.                         isTrue = true;
  38.                     } else if (condition.equals(">") && num1 > num2) {
  39.                         isTrue = true;
  40.                     } else if (condition.equals("<") && num1 < num2) {
  41.                         isTrue = true;
  42.                     }
  43.  
  44.                     if (isTrue) {
  45.                         String[] loops = matchIf.group(4).split(" ");
  46.                         if (loops.length == 1) {
  47.                             System.out.println(matchIf.group(6));
  48.                         } else {
  49.                             for (int j = 0; j < Integer.parseInt(loops[1]); j++) {
  50.                                 System.out.println(matchIf.group(6));
  51.                             }
  52.                         }
  53.                         trueCount++;
  54.                     }
  55.  
  56.                 } else if (matchElse.find() && trueCount == i - 1) {
  57.                     String[] loops = matchElse.group(1).split(" ");
  58.                     if (loops.length == 1) {
  59.                         System.out.println(matchElse.group(3));
  60.                     } else {
  61.                         for (int j = 0; j < Integer.parseInt(loops[1]); j++) {
  62.                             System.out.println(matchElse.group(3));
  63.                         }
  64.                     }
  65.                     trueCount++;
  66.                 } else {
  67.                     trueCount++;
  68.                 }
  69.             }
  70.         } else {
  71.             System.out.println("Compile time error @ line " + (validCounter + 1));
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement