Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.data_structures;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.util.*;
- /**
- * @author Vita Wiebe
- * @version PA9: Stack/Queue Use, Parser
- * This program takes a mathematical expression, user-input in
- * the standard, infix notation, and produces an equivalent
- * expression written in postfix notation.
- */
- public class Parser extends UnboundedArrayQueue implements ParserInterface {
- // Fields
- // String s, the input string from user.
- // Pre-condition: non-empty.
- private String s;
- // Our data structures, for handling the input, reordering,
- // and eventual output of the postfix form of input.
- private UnboundedArrayQueue inputQ;
- private UnboundedArrayQueue outputQ;
- private UnboundedArrayStack transformStack;
- // For comparison when parsing the input string.
- public final String operators = "+-/*%";
- /**
- * Our class constructor.
- */
- public Parser() {
- }
- /**
- * This method parses the input string into individual tokens.
- * <p>
- * For the minimal version of the assignment, this means breaking
- * the input string into single-character tokens, discarding all
- * white space.
- * For the standard version of the assignment, this entails
- * breaking the input string into contiguous "runs" of alphanumeric
- * characters (identifiers and integers) and punctuation (operators).
- * <p>
- * Negative numbers are not handled by this tokenizer.
- *
- * @param input The input string.
- * @return A queue object containing the individual tokens.
- */
- public UnboundedArrayQueue<String> tokenizeInput(String input) {
- s = input;
- String sChar;
- inputQ = new UnboundedArrayQueue();
- // Iterate over input string, s.
- // If not whitespace or operator.
- for(int i = 0; i < s.length(); ) {
- char c = s.charAt(i);
- if (c == ' ') {
- i++;
- } else if (operators.contains(Character.toString(c))) {
- i++;
- } else {
- // will i need to convert to String here too to add to
- // string queue?
- inputQ.add(c);
- i++;
- }
- }
- // Iterate over input string again to find operators.
- // Add these to the inputQ next.
- for(int i = 0; i < s.length(); i++) {
- char c = s.charAt(i);
- if(operators.contains(Character.toString(c))) {
- inputQ.add(c);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement