public static void moveCar(int oldPos, int newPos, StringBuilder line) { if (oldPos < newPos) { line.setCharAt(newPos, '\\'); } else if (oldPos > newPos) { line.setCharAt(newPos, '/'); } else { line.setCharAt(newPos, '|'); } }
public static void main(String[] args) { try { Scanner sc = new Scanner(new File(args[0])); int carPos = -1; boolean first = true; while (sc.hasNextLine()) { StringBuilder line = new StringBuilder(sc.nextLine()); if (first) { carPos = line.lastIndexOf("C"); if (carPos == -1) { carPos = line.lastIndexOf("_"); } line.setCharAt(carPos, '|'); first = false; } else { int cPos = line.lastIndexOf("C"); int gatePos = line.lastIndexOf("_"); if (cPos != -1) { moveCar(carPos, cPos, line); carPos = cPos; } else { moveCar(carPos, gatePos, line); carPos = gatePos; } } System.out.println(line); } sc.close(); } catch (FileNotFoundException e) { System.out.println("File not found!"); } }
public TokenTypeEnum GetNextToken() { StringBuilder strToken = new StringBuilder(); StringBuilder str = new StringBuilder(); str.append(m_strExpr); if (m_nNextPos >= str.length()) { m_currToken = new Token(); m_currToken.Type = TokenTypeEnum.T_EOL; m_currToken.str = new String("EOL"); m_nNextPos = 0; m_PreviousTokenType = TokenTypeEnum.T_EOL; return TokenTypeEnum.T_EOL; } while (true) { while (m_nNextPos < str.length() && str.charAt(m_nNextPos++) == ' ') ; --m_nNextPos; if (m_nNextPos >= str.length()) { m_currToken = new Token(); m_currToken.Type = TokenTypeEnum.T_EOL; m_currToken.str = new String("EOL"); m_nNextPos = 0; m_PreviousTokenType = TokenTypeEnum.T_EOL; return TokenTypeEnum.T_EOL; } else if (isdigit(str.charAt(m_nNextPos))) { while (m_nNextPos < str.length() && isdigit(str.charAt(m_nNextPos))) { strToken.append(str.charAt(m_nNextPos)); m_nNextPos++; } if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '.') { strToken.append(str.charAt(m_nNextPos)); m_nNextPos++; while (m_nNextPos < str.length() && isdigit(str.charAt(m_nNextPos))) { strToken.append(str.charAt(m_nNextPos)); m_nNextPos++; } m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_NUMBER; m_currToken.str = new String(strToken.toString()); m_currToken.Value = Double.valueOf(m_currToken.str).doubleValue(); return TokenTypeEnum.T_NUMBER; } else { m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_NUMBER; m_currToken.str = new String(strToken.toString()); m_currToken.Value = Double.valueOf(m_currToken.str).doubleValue(); return TokenTypeEnum.T_NUMBER; } } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '.') { str.setCharAt(m_nNextPos, '.'); m_nNextPos++; while (m_nNextPos < str.length() && isdigit(str.charAt(m_nNextPos))) { strToken.append(str.charAt(m_nNextPos)); m_nNextPos++; } m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_NUMBER; m_currToken.str = new String(strToken); m_currToken.Value = Double.valueOf(m_currToken.str).doubleValue(); return TokenTypeEnum.T_NUMBER; } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '(') { m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_OPAREN; m_currToken.str = new String("("); ++m_nNextPos; return TokenTypeEnum.T_OPAREN; } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == ')') { m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_CPAREN; m_currToken.str = new String(")"); ++m_nNextPos; return TokenTypeEnum.T_CPAREN; } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '+') { m_PreviousTokenType = m_currToken.Type; m_currToken.str = new String("+"); ++m_nNextPos; m_currToken.Type = TokenTypeEnum.T_PLUS; return TokenTypeEnum.T_PLUS; } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '-') { m_currToken.str = new String("-"); ++m_nNextPos; m_PreviousTokenType = m_currToken.Type; if (m_PreviousTokenType == TokenTypeEnum.T_CPAREN || m_PreviousTokenType == TokenTypeEnum.T_NUMBER) { m_currToken.Type = TokenTypeEnum.T_MINUS; return TokenTypeEnum.T_MINUS; } else { m_currToken.Type = TokenTypeEnum.T_UMINUS; return TokenTypeEnum.T_UMINUS; } } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '*') { m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_MULT; m_currToken.str = new String("*"); ++m_nNextPos; return TokenTypeEnum.T_MULT; } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '/') { m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_DIV; m_currToken.str = new String("/"); ++m_nNextPos; return TokenTypeEnum.T_DIV; } else if (m_nNextPos < str.length() && str.charAt(m_nNextPos) == '^') { m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_EXP; m_currToken.str = new String("^"); ++m_nNextPos; return TokenTypeEnum.T_EXP; } else { m_PreviousTokenType = m_currToken.Type; m_currToken.Type = TokenTypeEnum.T_UNKNOWN; m_currToken.str = new String(str); ++m_nNextPos; return TokenTypeEnum.T_UNKNOWN; } } }