Question: I am having issues getting the Infix expressions. I need to get the infix, convert to postfix (which I am confident i can do) and

I am having issues getting the Infix expressions. I need to get the infix, convert to postfix (which I am confident i can do) and go from there. I just need help on where to go from here.

import java.util.regex.Pattern;

import java.util.regex.Matcher;

public class ArithExpression {

public TokenList postfixTokens;

public TokenList infixTokens;

/**

* Sets up a legal standard Arithmetic expression.

* The only parentheses accepted are "(" and ")".

* @param word An arithmetic expression in standard infix order.

* An invalid expression is not expressly checked for, but will not be

* successfully evaluated, when the evaluate method is called.

* @throws InvalidExpressionException if the expression cannot be properly parsed,

* or converted to a postfix expression.

*/

public ArithExpression(String word) {

if (Tools.isBalancedBy("()",word)) {

tokenizeInfix(word);

infixToPostfix();

} else {

throw new InvalidExpressionException("Parentheses unbalanced");

}

}

/*

* A private helper method that tokenizes a string by separating out

* any arithmetic operators or parens from the rest of the string.

* It does no error checking.

* The method makes use of Java Pattern matching and Regular expressions to

* isolate the operators and parentheses.

* The operands are assumed to be the substrings delimited by the operators and parentheses.

* The result is captured in the infixToken list, where each token is

* an operator, a paren or a operand.

* @param express The string that is assumed to be an arithmetic expression.

*/

private void tokenizeInfix(String express) {

infixTokens = new TokenList(express.length());

// regular expression that looks for any operators or parentheses.

Pattern opParenPattern = Pattern.compile("[-+*/^()]");

Matcher opMatcher = opParenPattern.matcher(express);

String matchedBit, nonMatchedBit;

int lastNonMatchIndex = 0;

String lastMatch = "";

// find all occurrences of a matched substring

while (opMatcher.find()) {

matchedBit = opMatcher.group();

// get the substring between matches

nonMatchedBit = express.substring(lastNonMatchIndex, opMatcher.start());

nonMatchedBit = nonMatchedBit.trim(); //removes outside whitespace

// The very first '-' or a '-' that follows another operator is considered a negative sign

if (matchedBit.charAt(0) == '-') {

if (opMatcher.start() == 0 ||

!lastMatch.equals(")") && nonMatchedBit.equals("")) {

continue; // ignore this match

}

}

// nonMatchedBit can be empty when an operator follows a ')'

if (nonMatchedBit.length() != 0) {

infixTokens.append(nonMatchedBit);

}

lastNonMatchIndex = opMatcher.end();

infixTokens.append(matchedBit);

lastMatch = matchedBit;

}

// parse the final substring after the last operator or paren:

if (lastNonMatchIndex < express.length()) {

nonMatchedBit = express.substring(lastNonMatchIndex,express.length());

nonMatchedBit = nonMatchedBit.trim();

infixTokens.append(nonMatchedBit);

}

}

public static boolean isOperator(String op) {

switch(op) {

case "+":

case "-":

case "/":

case "*":

case "^":

return true;

default:

return false;

}

}

public void infixToPostfix() {

//******COMPLETE *****

//initialize empty string for the result

//String result = new String("");

//initialize an empty stack

//Stack stack = new Stack<>();

}

public String getInfixExpression() {

//******COMPLETE *****

return null;

}

public String getPostfixExpression() {

//******COMPLETE *****

return null;

}

public double evaluate() {

//******COMPLETE *****

return -1;

}

public static void main(String[] args) {

//******COMPLETE *****

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!