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
Answer for 5,6,7 any one know plz
katrin2010 [14]

Answer:

first question option is d

second is false

third is mark

Explanation:

5-The following are generic font

  • serif
  • sans-serif
  • cursive
  • fantasy
  • monospace

6 - We can change image size using height and width. so it is false.

7 - we can highlight text using <mark> Marked text </mark>

5 0
3 years ago
Joaquin is considering a career as an animator and wants to know more about the daily work of an average animator. Which strateg
Advocard [28]
Lacking a response to my question, I'll assume there's no "correct' answer here. 

Joaquin could contact the university's graphic arts department, or if he's interested in the programming side of it, the computer science department, and ask if they have any information on the topic. If he's already majoring to become an animator, then he should talk to one of his professors to see if they know someone in the business that would speak with Joaquin. 

The faster and better way would be to go to reddit, or some site at which animators congregate (most probably found through Google), read the forums, and create if he's not finding a suitable answer.
3 0
3 years ago
The changing of values for an object through a system is represented by the _____. (Points : 6) communication diagram
Fed [463]

Answer:

The correct option is communication diagram

Explanation:

The communication diagram represents the change of values for an item by a system.

A communication diagram is an expansion of the diagram of objects showing the objects together with the texts traveling from one to another. Besides the connections between objects, the communication diagram demonstrates the messages that the objects send to one another.

The correct option is a) communication diagram

8 0
2 years ago
Which activity represents a violation of the licensing agreement?
Lena [83]
A) <span>unauthorized duplication of software </span>
6 0
2 years ago
Read 2 more answers
In this scenario, two friends are eating dinner at a restaurant. The bill comes in the amount of 47.28 dollars. The friends deci
pickupchik [31]

Answer:

The java program for the given scenario is as follows.

import java.util.*;

import java.lang.*;

public class Main

{

   //variables for bill and tip declared and initialized

   static double bill=47.28, tip=0.15;

   //variables for total bill and share declared

   static double total, share1;

public static void main(String[] args) {

    double total_tip= (bill*tip);

    //total bill computed

    total = bill + total_tip;

    //share of each friend computed

    share1 = total/2;

    System.out.printf("Each person needs to pay: $%.2f", share1);  

}

}

Explanation:

1. The variables to hold the bill and tip percent are declared as double and initialized with the given values.

static double bill=47.28, tip=0.15;

2. The variables to hold the values of total bill amount and total tip are declared as double.

3. All the variables are declared outside main() and at class level, hence declared as static.

4. Inside main(), the values of total tip, total bill and share of each person are computed as shown.

double total_tip= (bill*tip);

total = bill + total_tip;

share1 = total/2;

5. The share of each person is displayed to the user. The value is displayed with only two decimal places which is assured by %.2f format modifier. The number of decimal places required can be changed by changing the number, i.e. 2. This format is used with printf() and not with println() method.

System.out.printf("Each person needs to pay: $%.2f", share1);  

6. The program is not designed to take any user input.

7. The program can be tested for any value of bill and tip percent.

8. The whole code is put inside a class since java is a purely object-oriented language.

9. Only variables can be declared outside method, the logic is put inside a method in a purely object-oriented language.

10. As shown, the logic is put inside the main() method and only variables are declared outside the method.

11. Due to simplicity, the program consists of only one class.

12. The output is attached.

5 0
3 years ago
Read 2 more answers
Other questions:
  • Row array gameScores contains all player scores. Construct a row array highScores than contains all player scores greater than m
    15·1 answer
  • Which sentence best describes an activity stream?
    10·2 answers
  • A DSLR camera is made up of two parts. They are
    13·2 answers
  • To help determine which technology to use, Raul creates a table to help him compare the pros and cons of his choices. What part
    15·1 answer
  • What are the problems with security when working on a Web Page?
    13·2 answers
  • What will be the value of i after the C statements at the right have been executed
    6·1 answer
  • Assume that the following variables have been defined in a program: int x = 10; int y = 20; int z = 30; Write a cout statement t
    11·1 answer
  • A class is a _____, which encapsulates _____ and _____. (Points : 2) programming language construct; attributes; behavior
    7·1 answer
  • Once secured a wheelchair may move up to 6 inches in any direction
    6·1 answer
  • You find information that you know to be classified on the internet. What should you do.
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!