Esempio n. 1
0
  private static boolean validParentheses(String s) {
    Stack<Character> stack = new Stack<Character>();

    for (int i = 0; i < s.length(); i++) {
      char cur = s.charAt(i);

      if (cur == '(' || cur == '[' || cur == '{') { // Left parentheses, push into stack.
        stack.push(cur);
      } else { // Right parentheses, compare with top element of stack.
        if (stack.isEmpty())
          return false; // If stack is empty and current character is right parentheses, it is
        // invalid.
        if (samePair(stack.peek(), cur)) {
          stack.pop();
        } else {
          return false;
        }
      }
    }

    if (stack.isEmpty()) return true;
    else return false;
  }
Esempio n. 2
0
  public static void main(String[] args) throws IOException {

    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    BufferedReader readIn = new BufferedReader(new InputStreamReader(System.in));

    int lineCount = 0;
    while (in.readLine() != null) {
      lineCount++;
    }

    // array of lines from file read-in
    String[] unsorted = new String[lineCount];
    int i = 0;

    // string we are cutting into substrings
    String sep = "";

    String roomName = "";
    int r = 0;
    String desc = "";
    String temp = "";

    List adventure = new List();

    while (i < lineCount) {
      sep = unsorted[i];
      temp = sep.substring(0, 1);
      String[][] cmds = new String[30][2];

      if (temp.equals("r")) {
        if (roomName != "") {
          adventure.insertRoom(roomName, desc, cmds);
          r = 0;
        }
        roomName = sep.substring(2, sep.length());
      }

      if (temp.equals("d")) {
        desc = desc.concat(sep.substring(2, sep.length()));
      }

      if (temp.equals("o")) {
        cmds[r][0] = sep.substring(2, sep.length());
      }

      if (temp.equals("t")) {
        cmds[r][1] = sep.substring(2, sep.length());
      }

      i++;
    }

    String room = adventure.firstRoom();

    String option = "";
    char input = 's';
    Stack history = new Stack();
    history.push(room);

    // Input from user
    while (!option.equals("q")) {
      option = readIn.readLine();

      if (option.length() > 1) {
        System.out.println("Invalid choice! Try again");
      } else {
        input = option.charAt(0);
        if (option != "") {
          // checks if its in range of valid options
          if (((int) input) - 97 < 13) {
            room = adventure.choice(room, input);
            history.push(room);
            adventure.options(room);
          } else {
            if ((int) input - 97 == 16) {
              System.out.println("Exit status: 0");
              System.exit(0);
            } else if ((int) input - 97 == 17) {
              history.restart(adventure.firstRoom());
            }
          }
        }
      }
    }
  }
Esempio n. 3
0
  private static int evalRPN(String[] tokens) {

    Stack<String> stack = new Stack<String>();

    for (int i = 0; i < tokens.length; i++) {
      String cur = tokens[i];
      switch (cur.charAt(0)) {
        case '+':
          stack.push(String.valueOf(Integer.parseInt(stack.pop()) + Integer.parseInt(stack.pop())));
          break;
        case '-':
          int num2 = Integer.parseInt(stack.pop());
          int num1 = Integer.parseInt(stack.pop());
          stack.push(String.valueOf(num1 - num2));
          break;
        case '*':
          stack.push(String.valueOf(Integer.parseInt(stack.pop()) * Integer.parseInt(stack.pop())));
          break;
        case '/':
          int divisor = Integer.parseInt(stack.pop());
          int dividend = Integer.parseInt(stack.pop());
          stack.push(String.valueOf((Integer) (dividend / divisor)));
          break;
        default:
          stack.push(cur);
          break;
      }
    }

    return Integer.parseInt(stack.pop());
  }