Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MIPS Project 6: Infix Expression Evaluation Linklist Assigned on: November 19 Du

ID: 3776825 • Letter: M

Question

MIPS Project 6: Infix Expression Evaluation Linklist Assigned on: November 19 Due on: November 30th, end of the day 11:59pm NO LATE PENALTY ACCEPTED: am giving everybody as much time as possible PART 1 (40 POINTS]: Infix Expression calculator Write a program that takes in, as a string, an expression in the Infix format and calculates its output. The program asks then the user for the next expression until the user hits the character 'E' which will terminate the program. Follow the stack implementation discussed in class: it is mandatory the explicit usage of the stack in your code. Make use of function calls whenever you see fit-functions must be present in your code. Numbers in expressions are multi-digit non-negative integers (but intermediate and final results can be negative integers); both numbers and the final result can allfit into a word. Operations can be and Examples: Input: (2- (8+9)) (1-4) Output 45 Input: (7- (((6+2) -4 8)) Output 25 Input: (3 ((205-102) +5)) (42-49) Output 46

Explanation / Answer

public class InfixPostfixEvaluator {


private static final String operands = "0123456789";

private static final String operators = "+-/*";

public int evaluationInfix(String infixVariables) {
return evaluateInfix(convertToInFix(infixVariables));
}

public String convertToInFix(String infixVariablesExpr) {
char[] chars = infixVariablesExpr.toCharArray();
Stack<Character> stack = new Stack<Character>();
StringBuilder out = new StringBuilder(infixVariablesExpr.length());

for (char c : chars) {
if (isOperator(c)) {
while (!stack.isEmpty() && stack.peek() != '(') {
if (operatorGreaterOrEqual(stack.peek(), c)) {
out.append(stack.pop());
} else {
break;
}
}
stack.push(c);
} else if (c == '(') {
stack.push(c);
} else if (c == ')') {
while (!stack.isEmpty() && stack.peek() != '(') {
out.append(stack.pop());
}
if (!stack.isEmpty()) {
stack.pop();
}
} else if (isOperand(c)) {
out.append(c);
}
}
while (!stack.empty()) {
out.append(stack.pop());
}
return out.toString();
}

public int evaluateInfix(String evaluateInfix) {
char[] chars = evaluateInfix.toCharArray();
Stack<Integer> stack = new Stack<Integer>();
for (char c : chars) {
if (isOperand(c)) {
stack.push(c - '0'); // convert char to int val
} else if (isOperator(c)) {
int op1 = stack.pop();
int op2 = stack.pop();
int result;
switch (c) {
case '*':
result = op1 * op2;
stack.push(result);
break;
case '/':
result = op2 / op1;
stack.push(result);
break;
case '+':
result = op1 + op2;
stack.push(result);
break;
case '-':
result = op2 - op1;
stack.push(result);
break;
}
}
}
return stack.pop();
}
private int getPrecedence(char operator) {
int ret = 0;
if (operator == '-' || operator == '+') {
ret = 1;
} else if (operator == '*' || operator == '/') {
ret = 2;
}
return ret;
}
private boolean operatorGreaterOrEqual(char op1, char op2) {
return getPrecedence(op1) >= getPrecedence(op2);
}

private boolean isOperator(char val) {
return operators.indexOf(val) >= 0;
}

private boolean isOperand(char val) {
return operands.indexOf(val) >= 0;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote