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
You have dinner with your family and tell them that you have taken bcis. your mother tells you that she is proud of you and that
VikaD [51]
The answer is (D) <span>I'll show you how to set up a simple database with one table called 'recipes.' You'll be able to manage it and run simple queries on it to find specific recipes very quickly and easily. All you need is some rudimentary knowledge of access to get the job done.
</span>

Spreadsheets are not bad for number crunching. However, if you have lots of data, you may benefit from efficient data management tool. Replacing spreadsheets with databases help you manage data centrally, safely and securely. By employing a database, you can avoid making mistakes like miscounts and data entry errors.

Learning Access can be a little bit daunting and intimidating. Through self-dedication, one can conquer and learn to create simple but functional database.


4 0
3 years ago
About ___% of children score within one standard deviation above or below the national average in intelligence.
goldfiish [28.3K]
The question is not influenced by the fact that it's about intelligence -  standard deviation is a way of describing data and it's the same for all kinds of data.

So in general, one  standard deviation away from the mean in both directions encompases around 66% of the cases - so here it would be 66% of children.

Additionally, 95% of all children should lie withing two standard deviations - so two standard deviations below or above the average.
4 0
3 years ago
what type of authentication does the dod require to accesss sensitive data on mobile devices and /or email
nikklg [1K]
Dod required a car to access data gahagsgssgsg
3 0
3 years ago
Read 2 more answers
Which command button contains the following sub options as given in the picture?
DiKsa [7]

Answer:

D) Slide options

Explanation:

hope this helps

7 0
3 years ago
Read 2 more answers
HELP PLEASE
GREYUIT [131]

Answer:

D, storyboard

Explanation:

5 0
3 years ago
Other questions:
  • Your Economics teacher has asked you to create a chart showing how supply and demand affects the price of gasoline. Which applic
    13·2 answers
  • kevin is working on a financial project that involves a lot of statistical information. He needs software that allows him to ent
    8·2 answers
  • So I’m doing a PowerPoint of how social media changed my life any ideas what I should right down and what kind of topics should
    11·2 answers
  • One of the most common uses of spreadsheet programs are communicating with others. O editing images and photos. O tracking and m
    8·2 answers
  • According to the video, what education and experience do employers look for in Reporters and Correspondents? Check all that appl
    10·2 answers
  • 5. Question<br> The control flag that isn't really in use by modern networks is the<br> flag.
    15·1 answer
  • . Write a short program that asks the user to input a string, and then outputs the
    15·1 answer
  • Tom Daniels, an employee of a telecommunications company, is developing software that would enable customers to activate value-a
    7·1 answer
  • All health information available on the internet is valid. <br> a. true <br> b. false
    6·1 answer
  • Expectation on Information Technology Fundamental​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!