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
Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicatin
Sergeu [11.5K]

Answer:

The program in C++ is as follows:

#include <iostream>

#include <vector>

using namespace std;

int main(){

   vector<int> intVect;

   int n;

   cin>>n;

int intInp;

for (int i = 0; i < n; i++) {

 cin >> intInp;

 intVect.push_back(intInp); }

for (int i = n-1; i >=0; i--) {  cout << intVect[i] << " "; }

 return 0;

}

Explanation:

This declares the vector

   vector<int> intVect;

This declares n as integer; which represents the number of inputs

   int n;

This gets input for n

   cin>>n;

This declares intInp as integer; it is used to get input to the vector

int intInp;

This iterates from 0 to n-1

for (int i = 0; i < n; i++) {

This gets each input

 cin >> intInp;

This passes the input to the vector

 intVect.push_back(intInp); }

This iterates from n - 1 to 0; i.e. in reverse and printe the vector elements in reverse

for (int i = n-1; i >=0; i--) {  cout << intVect[i] << " "; }

7 0
3 years ago
How can users open a shared worksheet if they do not have Excel installed on a computer? visit Microsoft’s website use Office365
Paladinen [302]

Answer:

use Excel Online through the 365 portal

Explanation:

just did it

4 0
3 years ago
Spoken word and written word are different because what
OLEGan [10]
Written word is expressed in writing while spoken word is expressed orally. 
6 0
3 years ago
What is an internt?​
EastWind [94]

{\texttt{\huge{\purple{ANSWER :-}}}}

Internet is a worldwide system of computer networks which connects more than thousands of computers in which the users at any of the one computer can, if they have permission, get information from any other computer.

Usually named as "net ''.

8 0
3 years ago
Computers are able to think like human beings true or false?
Temka [501]

Answer:

False. Cuz they work the way they are programmed, which can be like a human being, but is not exactly it. They are built this way so they work as what the user thinks. (e.g. being sure to delete a file/recommending what the user might like)

make me brainliestt :))

7 0
3 years ago
Other questions:
  • âwhen the footer is the last element in a page body, the _____ pseudo-element is used to add a placeholder element after the foo
    8·2 answers
  • Suppose a program is running on a distributed-memory multiprocessor. There are 1,000 instructions in the program and 80% of inst
    13·1 answer
  • Which of the following is not one of the five major areas to include in your notes organizer?
    15·2 answers
  • Big data refers to huge collections of data that are difficult to process, analyze, and manage using conventional data tools. It
    13·1 answer
  • When a module is executing what happens when the end of the module is reached?
    10·1 answer
  • True or False: Assuming that all variables are defined, the following segment of code is invalid. if ( x &gt; 0 )
    10·1 answer
  • I have answered 22 questions and my messages are still not working what is it that i'm doing wrong?
    6·1 answer
  • On the Format tab, which group allows you to select a different font for a chart?
    7·2 answers
  • The answer is D!!Readable code includes
    9·2 answers
  • Write a paragraph on 'Save Earth Save Life.'​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!