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
labwork [276]
3 years ago
9

Convert infix to postfix

Computers and Technology
1 answer:
kvv77 [185]3 years ago
4 0

Answer:

static int checkSymbol(char ch)

{

switch (ch)

{

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

}

return -1;

}

static String convertInfixToPostfix(String expression)

{

String calculation = new String("");

Stack<Character> operands = new Stack<>();

Stack<Character> operators = new Stack<>();

 

for (int i = 0; i<expression.length(); ++i)

{

char c = expression.charAt(i);

if (Character.isLetterOrDigit(c))

operands.push(c);

else if (c == '(')

operators.push(c);

 

else if (c == ')')

{

while (!operators.isEmpty() && operators.peek() != '(')

operands.push(operators.pop());

 

if (!operators.isEmpty() && operators.peek() != '(')

return NULL;    

else

operators.pop();

}

else

{

while (!operators.isEmpty() && checkSymbol(c) <= checkSymbol(operators.peek()))

operands.push(operators.pop());

operators.push(c);

}

}

while (!operators.isEmpty())

operands.push(operators.pop());

while (!operands.isEmpty())

calculation+=operands.pop();

calculation=calculation.reverse();

return calculation;

}

Explanation:

  • Create the checkSymbol function to see what symbol is being passed to the stack.
  • Create the convertInfixToPostfix function that keeps track of the operands and the operators stack.
  • Use conditional statements to check whether the character being passed is a letter, digit, symbol or a bracket.
  • While the operators is  not empty, keep pushing the character to the operators stack.
  • At last reverse and return the calculation which has all the results.
You might be interested in
To save the changes to the layout of a table, click the Save button on the _____.
kari74 [83]
Quick access toolbar :)))))))))
7 0
3 years ago
When records are in ____ order, they are arranged one after another on the basis of the value in a particular field.?
Nat2105 [25]
When records are in ASCENDING order ...
4 0
3 years ago
In which format is information stored on a hard drive?
Natalija [7]
The answer is binary
6 0
3 years ago
Please Answer Quickly.<br> Match the item on the left with the reason that it is false on the right.
Varvara68 [4.7K]

Answer:

me desculpe mais eu preciso de pontos para ajudr meu irmão autista

5 0
2 years ago
In the wireless telecommunications industry, different technical standards are found in different parts of the world. A technica
Serhud [2]

Answer:

A varying infrastructure.

Explanation:

The competitive pressure it creates for multinational companies in the industry is one of difference in infrastructure because different technical standards are found in different parts of the world.

For instance, A technical standard known as Global Systems for Mobile (GSM) is common in Europe, and an alternative standard, Code Division Multiple Access (CDMA), is more common in the United States and parts of Asia.

Consequently, equipment designed for GSM will not work on a CDMA network and vice versa as a result of varying infrastructure, so companies would be innovative in order to improve on their products.

3 0
3 years ago
Other questions:
  • Which statement describes the word "iterative"?
    7·2 answers
  • What is cyberbullying?
    14·2 answers
  • What is the most efficient way to include a space after each paragraph
    9·1 answer
  • Rita wants to know the size of each image in a folder. Which view will help her find this information quickly?
    7·1 answer
  • Which of these networks is primarily for posting and viewing photos?
    8·2 answers
  • Help what is a computer made from (computer class question)!!
    9·1 answer
  • Which operating system (OS) is used to run your laptop?
    15·2 answers
  • Binary search takes a list of information and divides the list into two parts, one is divided and one is kept.
    15·1 answer
  • It is a blueprint of a project​
    9·2 answers
  • Set of general format used to write program in any programming language?​
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!