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]
2 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]2 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
After Maya gave her friend the password to a protected website, the friend was able to remember it only long enough to type it i
EleoNora [17]

Answer:

The correct answer is Short Term Memory.

Explanation:

The A and D options, Procedural and Implicit memory are two of the long-term memory types and the question states that Maya forgot the password after typing it in, so these options are incorrect. Option C, Flashbulb Memory is a type of memory which stores specific moments and events in a person's lifetime which are remembered like snapshots. So the correct answer is B, Short Term Memory. I hope this answer helps.

5 0
2 years ago
Which of the following is true about advertisements?
katrin [286]

the answer is they often contain biases :)

3 0
2 years ago
Who are the founders of Microsoft?
vekshin1

Answer:

Bill Gates, Paul Allen

Explanation:

The two founders of the mega-tech giant are Bill Gates, and Paul Allen. Hope this helps!

8 0
3 years ago
What do we call the distribution and access of illegal copies of digital books??
Maru [420]
That concept is piracy


3 0
3 years ago
Read 2 more answers
Which is a key component to participating in a school-to-work program? A. Dual enrollment opportunities B. Community service opp
PtichkaEL [24]
The answer is D. Job shadows
5 0
3 years ago
Read 2 more answers
Other questions:
  • Security awareness training can reduce the risk of a data breach by what percentage?
    12·1 answer
  • Exceptions can be handled in all of these ways except:
    6·1 answer
  • why is there a need of properly interpreting teacher's/manufacturer's specifications before operating any food processing equipm
    9·1 answer
  • Nuclear batteries use devices called thermocouples, which convert the ____ of a nuclear reaction into electricity.
    9·1 answer
  • HELP!!! Bad things about Helen Keller. I'm doing a debate and i need to prove that Steve Jobs is better than Helen Keller!!
    14·1 answer
  • What is the smallest amount of information called?
    13·2 answers
  • Role of memory in a computer system
    10·1 answer
  • List six features of the Microsoft ​
    10·1 answer
  • What version of java do i have installed.
    6·1 answer
  • Location of a video or photoshoot is not important when it comes to preplanning the shoot.
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!