Advertisement
Vita_Harvey

InfixParser_C

Mar 10th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 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.Iterator;
  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 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 outputQ;
  26.    
  27.    // For comparison when parsing the input string.
  28.    public final String operators = "+-/*%";
  29.    public final String numbers = "0123456789";
  30.    
  31.    /**
  32.     * Our class constructor.
  33.     */
  34.    public InfixParser() {      
  35.    }
  36.    
  37.    /**
  38.     * This method parses the input string into individual tokens.
  39.     * <p>
  40.     * For the minimal version of the assignment, this means breaking  
  41.     * the input string into single-character tokens, discarding all
  42.     * white space.
  43.     * For the standard version of the assignment, this entails
  44.     * breaking the input string into contiguous "runs" of alphanumeric
  45.     * characters (identifiers and integers) and punctuation (operators).
  46.     * <p>
  47.     * Negative numbers are not handled by this tokenizer.
  48.     *
  49.     * @param input The input string.
  50.     * @return A queue object containing the individual tokens.
  51.     */
  52.    public UnboundedArrayQueue tokenizeInput(String input) throws
  53.       IllegalArgumentException, NullPointerException {
  54.      
  55.       // Take input string, split into separate tokens, put into UnboundedQueue
  56.       this.s = input;
  57.       String[] sChar = s.split("");
  58.       UnboundedArrayQueue inputQ = new UnboundedArrayQueue();
  59.      
  60.       // Iterate thru sChar, combine each token into one token if
  61.       // consecutive elements are numerals  (done to allow for numbers
  62.       // outside of 0-9 range).
  63.       for(int i = 0; i < sChar.length; i++) {
  64.          String tempString = "";
  65.          if(numbers.contains(sChar[i])) {
  66.             while(i < sChar.length && numbers.contains(sChar[i])){
  67.                tempString += sChar[i];
  68.                ++i;  
  69.             }
  70.             --i;
  71.             inputQ.add(tempString);
  72.          }else if(operators.contains(sChar[i])) {
  73.             inputQ.add(sChar[i]);
  74.          }              
  75.       }
  76.       return inputQ;        
  77.    }  
  78.            
  79.    /**
  80.     * This method converts a stream of tokens representing a
  81.     * well-formed arithmetic expression from infix notation to
  82.     * postfix notation.
  83.     * <p>
  84.     * For the minimal and standard versions of the assignment,
  85.     * this algorithm does not have handle the left-to-right
  86.     * associativity of the additive and multiplicative operators.
  87.     * This is avoided by taking advantange of the associative
  88.     * property of addition and multiplication.
  89.     * The challenge version of the assignment includes the left-
  90.     * to-right associativity, and therefore can also support the
  91.     * subtraction and division operators.
  92.     * <p>
  93.     * This method does not have to detect malformed input streams.
  94.     *
  95.     * @param input The input stream of tokens, the expression in
  96.     * infix notation.
  97.     * @return The output stream of tokens, the expression in postfix
  98.     * notation.
  99.     */
  100.    public UnboundedQueue<String> convertInfixToPostfix(UnboundedQueue<String> input) {
  101.    
  102.       if(!input.hasContents()){
  103.          return new UnboundedArrayQueue();
  104.       }
  105.      
  106.       UnboundedArrayStack<String> processStack = new UnboundedArrayStack(input.size());
  107.       String ops = "";
  108.       //String combinedNumbers = "";
  109.       try {
  110.          // Check exception: array error:
  111.          // If input is empty, output should be empty
  112.          // Iterate over inputQ. Add numbers to processStack.    
  113.          while(input.hasContents()) {        
  114.             String token = input.remove();
  115.             // Concatenate subsequent numerals together,
  116.             // as they are probably multiple-digit numbers.
  117.             while(numbers.contains(input.remove())){
  118.                token += token;
  119.             }                        
  120.             if (!operators.contains(token)) {
  121.                processStack.push(token);
  122.             // Keep track of operators for the end.              
  123.             } else {
  124.                ops += token;  
  125.             }
  126.          }  
  127.       } catch(NullPointerException n) {
  128.          System.err.println(n.getMessage());
  129.       }
  130.  
  131.       while(processStack.hasContents()){}
  132.       // add to OutputQ
  133.      
  134.       // Iterate over inputQ again to find operators.
  135.       // Add these to the processStack next.
  136.       for(int i = 0; i < s.length(); i++) {
  137.          char c = s.charAt(i);
  138.          if(operators.contains(Character.toString(c))) {
  139.             processStack.push(Character.toString(c));
  140.          }        
  141.       }      
  142.       return null;
  143.    }
  144.    
  145.    /**
  146.     * This method formats the output stream of tokens into a
  147.     * single string for output. The individual tokens in the
  148.     * output stream are separated by exactly one space between
  149.     * adjacent tokens. There is no opening or closing space in
  150.     * the output String.
  151.     *
  152.     * @param output The output stream of tokens.
  153.     * @return The formatted output string.
  154.     */
  155.    public String formatOutput(UnboundedQueue<String> output){
  156.       return null;
  157.    }
  158.    
  159.    // Main application method from which the program is run.
  160.    public static void main(String[] args) {
  161.       InfixParser poop = new InfixParser();
  162.       UnboundedArrayQueue out = poop.tokenizeInput("42 + 7");
  163.       System.out.println(out);
  164.        
  165.    }
  166.    
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement