Question
Modify the EvaluateExpression program as follows: a. (5 points) Evaluate two infix expressions entered as first two command-line arguments (args[0] and args[1) i. Handle any
Modify the EvaluateExpression program as follows: a. (5 points) Evaluate two infix expressions entered as first two command-line arguments (args[0] and args[1) i. Handle any number of arithmetic expressions entered as command-line arguments ii. Handle two additional operators % and ^ (exponent). Assume % has the same priority as * and /. The ^ has the highest priority. b. (5 points) Evaluate two postfix expressions entered as second two command-line arguments (args[2] and args[3]). c. (Bonus 5 points) Convert first infix expressions entered into args[0] and args[1] into postfix expressions. Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with top two operands in the stack and replace the two operands with the result. Sample Input: "(4 + (4 - 15* 2 ) * 5)" "2 * (5 -3)" "2 3 + 4*" " 3 41 5 - *" Output: Your output should consist of each input expression, followed by its corresponding result. II. Attach (do not turn in any hardcopy) via BlazeView a zipped file named with your last name, for example smith9. The zipped file should contain items (MyExpression9 and any additional classes, MyReport9, and MyOutput9) Make sure that your program is well structured, documented, readable. Rules in converting Infix to Postfix You may apply the conversion algorithm using the following six rules: Scan the input string (infix notation) from left to right. One pass is sufficient. If the next symbol scanned is an operand, it may be immediately appended to the postfix string. If the next symbol is an operator, 1. Pop and append to the postfix string every operator on the stack that a. is above the most recently scanned left parenthesis, and b. has precedence higher than or equal to that of the new operator symbol. 2. Then push the new operator symbol onto the stack. When an opening (left) parenthesis is seen, it must be pushed onto the stack. When a closing (right) parenthesis is seen, all operators down to the most recently scanned left parenthesis must be popped and appended to the postfix string. Furthermore, this pair of parentheses must be discarded. When the infix string is completely scanned, the stack may still contain some operators. All these remaining operators should be popped and appended the postfix string.
//
I want my solution in java programming
also i have half code provided by my professor, so you need to just complete that one.
import java.util.Stack;
public class EvaluateExpression{
public static void main(String[] args) {
// Check number of arguments passed
if (args.length != 1) {
System.out.println(
"Usage: java EvaluateExpression \"expression\"");
System.exit(1);
}
try {
System.out.println(evaluateExpression(args[0]));
}
catch (Exception ex) {
System.out.println("Wrong expression: " + args[0]);
}
}
/** Evaluate an expression */
public static int evaluateExpression(String expression) {
// Create operandStack to store operands
Stack
// Create operatorStack to store operators
Stack
// Insert blanks around (, ), +, -, /, and *
expression = insertBlanks(expression);
// Extract operands and operators
String[] tokens = expression.split(" ");
// Phase 1: Scan tokens
for (String token: tokens) {
if (token.length() == 0) // Blank space
continue; // Back to the while loop to extract the next token
else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
// Process all +, -, *, / in the top of the operator stack
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '+' ||
operatorStack.peek() == '-' ||
operatorStack.peek() == '*' ||
operatorStack.peek() == '/' )) {
processAnOperator(operandStack, operatorStack);
}
// Push the + or - operator into the operator stack
operatorStack.push(token.charAt(0));
}
else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
// Process all *, / in the top of the operator stack
while (!operatorStack.isEmpty() &&
(operatorStack.peek() == '*' ||
operatorStack.peek() == '/' )) {
processAnOperator(operandStack, operatorStack);
}
// Push the * or / operator into the operator stack
operatorStack.push(token.charAt(0));
}
else if (token.trim().charAt(0) == '(') {
operatorStack.push('('); // Push '(' to stack
}
else if (token.trim().charAt(0) == ')') {
// Process all the operators in the stack until seeing '('
while (operatorStack.peek() != '(') {
processAnOperator(operandStack, operatorStack);
}
operatorStack.pop(); // Pop the '(' symbol from the stack
}
else { // An operand scanned
// Push an operand to the stack
operandStack.push(new Integer(token));
}
}
// Phase 2: process all the remaining operators in the stack
while (!operatorStack.isEmpty()) {
processAnOperator(operandStack, operatorStack);
}
// Return the result
return operandStack.pop();
}
/** Process one operator: Take an operator from operatorStack and
* apply it on the operands in the operandStack */
public static void processAnOperator(
Stack
char op = operatorStack.pop();
int op1 = operandStack.pop();
int op2 = operandStack.pop();
if (op == '+')
operandStack.push(op2 + op1);
else if (op == '-')
operandStack.push(op2 - op1);
else if (op == '*')
operandStack.push(op2 * op1);
else if (op == '/')
operandStack.push(op2 / op1);
}
public static String insertBlanks(String s) {
String result = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == ')' ||
s.charAt(i) == '+' || s.charAt(i) == '-' ||
s.charAt(i) == '*' || s.charAt(i) == '/' )
result += " " + s.charAt(i) + " ";
else
result += s.charAt(i);
}
return result;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started