1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
LekaFEV [45]
3 years ago
13

You can combine the algorithms for converting between infix to postfix and for evaluating postfix to evaluate an infix expressio

n directly. To do so you need two stacks: one to contain operators and the other to contain operands. When an operand is encountered, it is pushed onto the operand stack. When an operator is encountered, it is processed as described in the infix-to-postfix algorithm*. When an operator is popped off the operator stack, it is processed as described in the postfix evaluation algorithm: The top two operands are popped off the operand stack, the operation is performed, and the result is pushed back onto the operand stack. Write a program to evaluate infix expressions directly using this combined algorithm.
Computers and Technology
1 answer:
marshall27 [118]3 years ago
4 0

Answer:

Check the explanation

Explanation:

import java.util.Stack;

/**

* Class to evaluate infix and postfix expressions.

*

public class InfixPostfixEvaluator {

/**

* Operators in reverse order of precedence.

*/

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

private static final String operands = "0123456789";

public int evalInfix(String infix) {

return evaluatePostfix(convert2Postfix(infix));

}

public String convert2Postfix(String infixExpr) {

char[] chars = infixExpr.toCharArray();

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

StringBuilder out = new StringBuilder(infixExpr.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 evaluatePostfix(String postfixExpr) {

char[] chars = postfixExpr.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;

}

}

You might be interested in
Once you have selected the range of cells for your table data, from which tab can you open the Insert Table dialog box?
Anuta_ua [19.1K]

A. Insert Tab. Specifically, The insert tab, then click "Table" which will open the Insert Table Dialog Box. Good luck!

3 0
4 years ago
Read 2 more answers
Is rhis app plagerism free
ollegr [7]
Yes,yes it is.All you do is get help
3 0
3 years ago
School officials want to show the percentage of students who buy lunch from the school cafeteria compared to the percentage of t
Minchanka [31]

I would say it would be (A pie chart)

6 0
3 years ago
Programmers should strive to _____. increase coupling increase cohesion both of the above neither a nor b
denpristay [2]
The answer is increase cohesion.  <span>Programmers should strive to increase cohesion.  C</span>ohesion<span> points to the </span><span>degree to which the elements inside a module belong together.  </span><span>In a highly </span>cohesive<span> system, code readability and reusability is </span>increased<span>, while complexity is kept manageable.</span>
7 0
3 years ago
Which of the following is an example of a Syntax Error?
quester [9]

Answer:

The correct answer is: "The program does not run at all."

Explanation:

The rules to write a program in any language are called syntax. The error in syntax of a language is called syntax error. Syntax errors are usually detected during the compilation of the program. The program cannot be run untill all syntax errors are removed.

So,

The correct answer is: "The program does not run at all."

7 0
3 years ago
Other questions:
  • The DNS server at your headquarters holds a standard primary zone for the abc domain. A branch office connected by a slow WAN li
    14·1 answer
  • You are creating a database for your computer club. Most of the students live in your town, Durham. How can you make Durham appe
    11·1 answer
  • Who said "BAM WHAT?!" On disney channel?
    5·2 answers
  • Who knows a website to learn how to code in java?
    7·1 answer
  • What type of maintenance is required of a computer’s power supply to ensure that it remains in working order?
    15·2 answers
  • Which of these devices collects the most information on network activity?
    11·1 answer
  • 1.
    9·1 answer
  • Write a program that creates a two-dimensional array named height and stores the following data:
    15·2 answers
  • If you are to enhance the ICT used by your teachers, how will ypu do it? Will you use the same ICT or will you modify how it was
    11·1 answer
  • Hello guys please help me <br>list and identify the components of computer ​
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!