Advertisement
seroperson

Химиястафф

Feb 1st, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import static java.lang.Character.*;
  6.  
  7. class Main {
  8.  
  9.         public static class Transform {
  10.  
  11.                 private final static List<String> unacceptable = new ArrayList<String>() {
  12.                         {
  13.                                 add("+");
  14.                                 add("=");
  15.                                 add("=\\");
  16.                                 add("-->");
  17.                         }
  18.                 };
  19.                 private String toTransform;
  20.                 private String transformed;
  21.  
  22.                 public Transform(String value) {
  23.                         toTransform = value;
  24.                 }
  25.  
  26.                 public void transform() {
  27.                         for(int index = 0; index < unacceptable.size(); )
  28.                                 if(unacceptable.get(index++).equals(toTransform)) {
  29.                                         transformed = toTransform;
  30.                                         return;
  31.                                 }
  32.                         int currentIndex = 0;
  33.  
  34.                         int size = toTransform.length();
  35.                         char[] chars = new char[size];
  36.                         toTransform.getChars(0, size, chars, 0);
  37.                         StringBuilder builder = new StringBuilder();
  38.  
  39.                         if(isDigit(chars[0]))
  40.                                 builder.append(chars[currentIndex++]);
  41.  
  42.                         while(currentIndex != size) {
  43.                                 if(isUpperCase(chars[currentIndex])) {
  44.                                         builder.append(chars[currentIndex]);
  45.                                         while(++currentIndex != size && isLowerCase(chars[currentIndex])) builder.append(chars[currentIndex]);
  46.                                 }
  47.                                 if(currentIndex != size) {
  48.                                         char current = chars[currentIndex];
  49.                                         if(current == '-' || current == '–' || current == '+') {
  50.                                                 builder.append("<sup>");
  51.                                                 builder.append(chars[currentIndex++]);
  52.                                                 builder.append("</sup>");
  53.                                         }
  54.                                         else if(current == '(' || current == ')') builder.append(chars[currentIndex++]);
  55.                                 }
  56.                                 if(currentIndex != size) {
  57.                                         if(isDigit(chars[currentIndex])) {
  58.                                                 builder.append("<sup>");
  59.                                                 builder.append(chars[currentIndex]);
  60.                                                 while(++currentIndex != size && isDigit(chars[currentIndex])) builder.append(chars[currentIndex]);
  61.                                                 builder.append("</sup>");
  62.                                         }
  63.                                 }
  64.                         }
  65.  
  66.                         transformed = builder.toString();
  67.                 }
  68.  
  69.                 public String getTransformed() {
  70.                         return transformed;
  71.                 }
  72.  
  73.         }
  74.  
  75.         public static void main(String... args) throws Throwable {
  76.                 Scanner scanner = new Scanner(new File(args[0]));
  77.                 while(scanner.hasNextLine()) {
  78.                         String currentLine = scanner.nextLine();
  79.                         String[] splitted = currentLine.split(" ");
  80.                         for(int index = 0; index < splitted.length; ) {
  81.                                 Transform transformer = new Transform(splitted[index++]);
  82.                                 transformer.transform();
  83.                                 System.out.print(transformer.getTransformed());
  84.                                 System.out.print(' ');
  85.                         }
  86.                         System.out.println();
  87.                 }
  88.         }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement