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 InfixParser 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 processStack;
- // For comparison when parsing the input string.
- public final String operators = "+-/*%";
- public final String numbers = "0123456789";
- /**
- * Our class constructor.
- */
- public InfixParser() {
- }
- /**
- * 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) {
- // Take input string, split into separate tokens, put into UnboundedQueue
- this.s = input;
- String[] sChar = s.split("");
- inputQ = new UnboundedArrayQueue();
- // Iterate thru sChar, combine each token into one token if
- // consecutive elements are numerals (done to allow for numbers
- // outside of 0-9 range).
- for(int i = 0; i < sChar.length; i++) {
- String tempString = "";
- if(numbers.contains(sChar[i])){
- tempString += sChar[i];
- } else {
- tempString = sChar[i];
- }
- inputQ.add(sChar[i]);
- }
- }
- /**
- * This method converts a stream of tokens representing a
- * well-formed arithmetic expression from infix notation to
- * postfix notation.
- * <p>
- * For the minimal and standard versions of the assignment,
- * this algorithm does not have handle the left-to-right
- * associativity of the additive and multiplicative operators.
- * This is avoided by taking advantange of the associative
- * property of addition and multiplication.
- * The challenge version of the assignment includes the left-
- * to-right associativity, and therefore can also support the
- * subtraction and division operators.
- * <p>
- * This method does not have to detect malformed input streams.
- *
- * @param input The input stream of tokens, the expression in
- * infix notation.
- * @return The output stream of tokens, the expression in postfix
- * notation.
- */
- public UnboundedQueue<String> convertInfixToPostfix(UnboundedQueue<String> input) {
- // 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 inputQ again to find operators.
- // Add these to the processStack 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