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.Iterator;
- /**
- * @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 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 outputQ;
- // 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 tokenizeInput(String input) throws
- IllegalArgumentException, NullPointerException {
- // Take input string, split into separate tokens, put into UnboundedQueue
- this.s = input;
- String[] sChar = s.split("");
- UnboundedArrayQueue 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])) {
- while(i < sChar.length && numbers.contains(sChar[i])){
- tempString += sChar[i];
- ++i;
- }
- --i;
- inputQ.add(tempString);
- }else if(operators.contains(sChar[i])) {
- inputQ.add(sChar[i]);
- }
- }
- return inputQ;
- }
- /**
- * 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) {
- if(!input.hasContents()){
- return new UnboundedArrayQueue();
- }
- UnboundedArrayStack<String> processStack = new UnboundedArrayStack(input.size());
- String ops = "";
- //String combinedNumbers = "";
- try {
- // Check exception: array error:
- // If input is empty, output should be empty
- // Iterate over inputQ. Add numbers to processStack.
- while(input.hasContents()) {
- String token = input.remove();
- // Concatenate subsequent numerals together,
- // as they are probably multiple-digit numbers.
- while(numbers.contains(input.remove())){
- token += token;
- }
- if (!operators.contains(token)) {
- processStack.push(token);
- // Keep track of operators for the end.
- } else {
- ops += token;
- }
- }
- } catch(NullPointerException n) {
- System.err.println(n.getMessage());
- }
- while(processStack.hasContents()){}
- // add to OutputQ
- // 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))) {
- processStack.push(Character.toString(c));
- }
- }
- return null;
- }
- /**
- * This method formats the output stream of tokens into a
- * single string for output. The individual tokens in the
- * output stream are separated by exactly one space between
- * adjacent tokens. There is no opening or closing space in
- * the output String.
- *
- * @param output The output stream of tokens.
- * @return The formatted output string.
- */
- public String formatOutput(UnboundedQueue<String> output){
- return null;
- }
- // Main application method from which the program is run.
- public static void main(String[] args) {
- InfixParser poop = new InfixParser();
- UnboundedArrayQueue out = poop.tokenizeInput("42 + 7");
- System.out.println(out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement