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
What color typically indicates the speaker port on a sound card?
allsm [11]
The answer to this question would be: Lime green

The lime green color code is for audio output for speaker or headphones. Color coding is greatly helpful to make sure that the port you are using is correct. Using the color you can determine what kind of input or output the port is for. There is a few others color coding like pink for analog microphone input, or light blue for audio input of analog line.
4 0
4 years ago
Read 2 more answers
Write a recursive program that requests an answer to the question "Are we there yet?" using an input statement and terminates if
inna [77]

Answer:

def recursive_func():

   x = input("Are we there yet?")

   if x.casefold() == 'Yes'.casefold():

       return

   else:

       recursive_func()

recursive_func()

Explanation:

We define the required function as recursive_func().

The first line takes user input. The user input is stored in variable x.

The next line compares the user input to a string yes. The function executes the else block if the condition isn't met, that is a recursive call is executed.

IF condition returns the function. The string in variable X is compared to a string 'Yes'. the casefold() is a string function that ignores the upper/lower cases when comparing two strings. (This is important because a string 'yes' is not the same yes a string 'Yes' or 'YES'. Two equal strings means their cases and length should match).

3 0
4 years ago
When adding clip art to a slide you are limited to the pictures stored on your computer?
77julia77 [94]
The answer is No.  <span>When adding clip art to a slide you are not limited to the pictures stored on your computer.  </span><span>We are not limited to using clip art from our computer. Any clip art can be used, but if it is from another source, it must first be saved to your computer as a </span>file<span>. </span>
8 0
3 years ago
What is a hacker who breaks into other people's computer systems and may just look around or steal and destroy?
inessss [21]
A cyber gang or cyber theif
5 0
3 years ago
Read 2 more answers
List three ways on how it's good to meet online and real life
nikitadnepr [17]

Answer:

Explanation:

How to meet people in real life

1. We can meet people if you go to your friend's party, is a great opportunity to meet friends from your friends.

2. We must go to events, in this case, if you like to read books, go to reading events.

3. We can use public transportation if you see someone reading your favorite book is the best opportunity.

How to meet people online

1. We can meet people in social media, but there are a lot of fake accounts, take care.

2. There are special platforms to meet people or to find love.

3. We could meet people on educational websites or apps, for example, if we want to learn Spanish, we can meet Spanish people.

3 0
3 years ago
Other questions:
  • The Spinning Jenny reduced the number of workers necessary to _______. a.remove cotton seeds from fibers b.pump water from the m
    7·1 answer
  • William found out that someone used his report on American culture without his permission. What is William a victim of?
    13·2 answers
  • HELP ASAP U GET BRAINLIEST
    15·2 answers
  • Indenting the start and finish of segments
    8·2 answers
  • Which of the following is a hand-drawn animation that takes a large amount of time to complete?
    5·1 answer
  • _______________ are distinguished from microcontrollers by their complexity and increased on-chip resources.A. Systems on a chip
    6·2 answers
  • Why do software managers have to be generalists with a range of skills, rather than simply technical specialists?
    14·1 answer
  • The inFS.open(str) function has a string parameter str that specifies the _____ of the file to open.
    10·1 answer
  • PLS HURRRYYYYYY!1!1!1!1!1
    5·2 answers
  • Using a linear search to find a value that is stored in the last element of an array of 20,000 elements, ________ element(s) mus
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!