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
Which is the highest level of the hierarchy of needs model?
Georgia [21]

<span>Maslow's hierarchy of needs is often portrayed in the shape of a pyramid with the largest, most fundamental needs at the bottom and the need
forself-actualization and self-transcendence at the top.</span>
5 0
3 years ago
Read 2 more answers
And tags are examples of stand - alone tags.
lina2011 [118]

Answer:

The image tag (<img>) is such a tag; it simply plops a graphic into the flow of the page. Other standalone tags include the line break (<br>), horizontal rule (<hr>), and tags that provide information about a document and don't affect its displayed content, such as the <meta> and <base> tags.

Explanation:

6 0
3 years ago
How can I get more views on my you tube channel "Braeden Eischen" without paying anything
ehidna [41]
I’m just post video and there
5 0
3 years ago
ASAP PLEASE the online research you did to describe how Senet is related to the culture and historical period when it was create
Morgarella [4.7K]

Answer:

I think this belongs in the history category as the computers % technology is a bit inactive.

6 0
2 years ago
Help me guys..<br>thankyou​
Rainbow [258]

Answer:

spoon

excess

level

1/2 cup

whatever dry ingredient u have

Explanation:

7 0
3 years ago
Other questions:
  • What are some examples of options you can control in the Adjust group to affect an image? (Multiple Choice)
    12·2 answers
  • What kinds of online behaviors could be considered cyberbullying?
    7·2 answers
  • A production house needs an operating system that captures, saves, and generates information within specific time. Which type of
    13·2 answers
  • What is a collection of organized information that allows users to perform certain tasks such as searching for specific informat
    9·1 answer
  • Why might it be a good idea to choose a bus topology?
    6·2 answers
  • Research and discuss the LAMP (Linux, Apache, MySQL, and PHP) architecture. What is the role of each layer of this software stac
    14·1 answer
  • Once the term of copyright has expired,a work
    15·1 answer
  • Suppose that a disk drive has 2,000 cylinders numbered 0 to 1999. The drive is currently serving a request at cylinder 142, and
    8·1 answer
  • How do I install another part on campaign call of duty cold war? please help.(best answer will get branliest.)
    15·2 answers
  • Selling emojis that you dont have
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!