Advertisement
Vita_Harvey

InfixParser_B

Mar 9th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package csc143.data_structures;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import java.util.*;
  7.  
  8. /**
  9.  * @author Vita Wiebe
  10.  * @version PA9: Stack/Queue Use, Parser
  11.  * This program takes a mathematical expression, user-input in
  12.  * the standard, infix notation, and produces an equivalent
  13.  * expression written in postfix notation.
  14.  */
  15. public class InfixParser extends UnboundedArrayQueue implements ParserInterface {
  16.  
  17.    // Fields
  18.    
  19.    // String s, the input string from user.
  20.    // Pre-condition: non-empty.
  21.    private String s;
  22.    
  23.    // Our data structures, for handling the input, reordering,
  24.    // and eventual output of the postfix form of input.
  25.    private UnboundedArrayQueue inputQ;
  26.    private UnboundedArrayQueue outputQ;
  27.    private UnboundedArrayStack processStack;
  28.    
  29.    // For comparison when parsing the input string.
  30.    public final String operators = "+-/*%";
  31.    public final String numbers = "0123456789";
  32.    
  33.    /**
  34.     * Our class constructor.
  35.     */
  36.    public InfixParser() {      
  37.    }
  38.    
  39.    /**
  40.     * This method parses the input string into individual tokens.
  41.     * <p>
  42.     * For the minimal version of the assignment, this means breaking  
  43.     * the input string into single-character tokens, discarding all
  44.     * white space.
  45.     * For the standard version of the assignment, this entails
  46.     * breaking the input string into contiguous "runs" of alphanumeric
  47.     * characters (identifiers and integers) and punctuation (operators).
  48.     * <p>
  49.     * Negative numbers are not handled by this tokenizer.
  50.     *
  51.     * @param input The input string.
  52.     * @return A queue object containing the individual tokens.
  53.     */
  54.    public UnboundedArrayQueue<String> tokenizeInput(String input) {
  55.      
  56.       // Take input string, split into separate tokens, put into UnboundedQueue
  57.       this.s = input;
  58.       String[] sChar = s.split("");
  59.       inputQ = new UnboundedArrayQueue();
  60.      
  61.       // Iterate thru sChar, combine each token into one token if
  62.       // consecutive elements are numerals  (done to allow for numbers
  63.       // outside of 0-9 range).
  64.       for(int i = 0; i < sChar.length; i++) {
  65.          String tempString = "";
  66.          if(numbers.contains(sChar[i])){
  67.             tempString += sChar[i];  
  68.          } else {
  69.             tempString = sChar[i];
  70.          }
  71.          inputQ.add(sChar[i]);    
  72.       }      
  73.    }  
  74.            
  75.    /**
  76.     * This method converts a stream of tokens representing a
  77.     * well-formed arithmetic expression from infix notation to
  78.     * postfix notation.
  79.     * <p>
  80.     * For the minimal and standard versions of the assignment,
  81.     * this algorithm does not have handle the left-to-right
  82.     * associativity of the additive and multiplicative operators.
  83.     * This is avoided by taking advantange of the associative
  84.     * property of addition and multiplication.
  85.     * The challenge version of the assignment includes the left-
  86.     * to-right associativity, and therefore can also support the
  87.     * subtraction and division operators.
  88.     * <p>
  89.     * This method does not have to detect malformed input streams.
  90.     *
  91.     * @param input The input stream of tokens, the expression in
  92.     * infix notation.
  93.     * @return The output stream of tokens, the expression in postfix
  94.     * notation.
  95.     */
  96.    public UnboundedQueue<String> convertInfixToPostfix(UnboundedQueue<String> input) {
  97.  
  98.       // Iterate over input string, s.
  99.       // If not whitespace or operator.      
  100.       for(int i = 0; i < s.length(); ) {
  101.          char c = s.charAt(i);                
  102.          if (c == ' ') {
  103.             i++;            
  104.          } else if (operators.contains(Character.toString(c))) {          
  105.             i++;
  106.          } else {
  107.             // will i need to convert to String here too to add to
  108.             // string queue?
  109.             inputQ.add(c);
  110.             i++;
  111.          }    
  112.       }
  113.      
  114.       // Iterate over inputQ again to find operators.
  115.       // Add these to the processStack next.
  116.       for(int i = 0; i < s.length(); i++) {
  117.          char c = s.charAt(i);
  118.          if(operators.contains(Character.toString(c))) {
  119.             inputQ.add(c);
  120.          }        
  121.       }
  122.    }
  123.    
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement