Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.util.List;
- import java.util.ArrayList;
- import static java.lang.Character.*;
- class Main {
- public static class Transform {
- private final static List<String> unacceptable = new ArrayList<String>() {
- {
- add("+");
- add("=");
- add("=\\");
- add("-->");
- }
- };
- private String toTransform;
- private String transformed;
- public Transform(String value) {
- toTransform = value;
- }
- public void transform() {
- for(int index = 0; index < unacceptable.size(); )
- if(unacceptable.get(index++).equals(toTransform)) {
- transformed = toTransform;
- return;
- }
- int currentIndex = 0;
- int size = toTransform.length();
- char[] chars = new char[size];
- toTransform.getChars(0, size, chars, 0);
- StringBuilder builder = new StringBuilder();
- if(isDigit(chars[0]))
- builder.append(chars[currentIndex++]);
- while(currentIndex != size) {
- if(isUpperCase(chars[currentIndex])) {
- builder.append(chars[currentIndex]);
- while(++currentIndex != size && isLowerCase(chars[currentIndex])) builder.append(chars[currentIndex]);
- }
- if(currentIndex != size) {
- char current = chars[currentIndex];
- if(current == '-' || current == '–' || current == '+') {
- builder.append("<sup>");
- builder.append(chars[currentIndex++]);
- builder.append("</sup>");
- }
- else if(current == '(' || current == ')') builder.append(chars[currentIndex++]);
- }
- if(currentIndex != size) {
- if(isDigit(chars[currentIndex])) {
- builder.append("<sup>");
- builder.append(chars[currentIndex]);
- while(++currentIndex != size && isDigit(chars[currentIndex])) builder.append(chars[currentIndex]);
- builder.append("</sup>");
- }
- }
- }
- transformed = builder.toString();
- }
- public String getTransformed() {
- return transformed;
- }
- }
- public static void main(String... args) throws Throwable {
- Scanner scanner = new Scanner(new File(args[0]));
- while(scanner.hasNextLine()) {
- String currentLine = scanner.nextLine();
- String[] splitted = currentLine.split(" ");
- for(int index = 0; index < splitted.length; ) {
- Transform transformer = new Transform(splitted[index++]);
- transformer.transform();
- System.out.print(transformer.getTransformed());
- System.out.print(' ');
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement