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]
2 years ago
9

Convert infix to postfix

Computers and Technology
1 answer:
kvv77 [185]2 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
Obtain the 10’s complement of the following six-digit decimal numbers:<br><br> 123900<br><br> 980657
zysi [14]

Answer:

876100

019343

Explanation:

10s complement of a decimal number is obtained by the following process:

- Obtain 9s complement ( Subtract each digit by 9)

- Add 1 to the result

1) 123900

9s complement => (9-1)(9-2)(9-3)(9-9)(9-0)(9-0)

= 876099

Adding 1 , 10s complement of 123900 = 876100

2) 980657

9s complement = (9-9)(9-8)(9-0)(9-6)(9-5)(9-7)

= 019342

Adding 1 , 10s complement of 980657 = 019343

7 0
3 years ago
4.3 mini programs AP computer science
allochka39001 [22]

1.

name = input("Enter your name: ")

num1 = int(input("Hello "+name+ ", enter an integer: "))

num2 = int(input(name+", enter another integer: "))

try:

   if num1 % num2 == 0:

       print("The first number is divisible by the second number")

   else:

       print("The first number is not divisible by the second number")

except ZeroDivisionError:

   print("The first number is not divisible by the second number")

try:

   if num2 % num1 == 0:

       print("The second number is divisible by the first number")

   else:

       print("The second number is not divisible by the first number")

except ZeroDivisionError:

   print("The second number is not divisible by the first number")

2.

import random, math

num1 = float(input("Enter a small decimal number: "))

num2 = float(input("Enter a large decimal number: "))

r = round(random.uniform(num1, num2), 2)

print("The volume of a sphere with radius " + str(r) + " is " + str(round(((4 / 3) * math.pi * (r ** 3)), 2)))

I hope this helps!

8 0
3 years ago
You often insert your company's logo into a document you create.one way to make it easier for you to quickly insert it is to sav
eduard
Save the logo as a TEMPLATE.

A template is a pre-developed page layout in soft copy or hard copy used to make pages with the same pattern, style, or design.

In the above scenario, you can prepare a template with you logo in it and save it for future use. In the event that you need to create a new document, you only have to bring up the saved template and edit it with the new information that you need to type.

This way, you will not be hassled to manually add your logo to every new document you create.
5 0
2 years ago
Who could vote in the first democracy?
zmey [24]
B citizens

in the 4th century there were about 100k citizens. only about 40k could participate in the democratic process ( this is only if the question is referring to ancient democracy )
4 0
3 years ago
Which of the following statements best describes how digital photographs can be used?
kipiarov [429]

Answer:

umm wheres the options so we can answer it and ty

Explanation:

3 0
3 years ago
Read 2 more answers
Other questions:
  • Which of the following best describes a proxy firewall? A. It sends traffic through another host. B. It acts as a gateway for re
    15·1 answer
  • Why does a HTML seen to work even when its broken? why does the code sometimes turn pink?
    9·1 answer
  • What type of engineer works on cleaning up oil spills?
    8·2 answers
  • Which of the following information should be included in audit documentation? a. Procedures performed. b. Audit evidence examine
    13·1 answer
  • Before performing a Web Recorder task, which two options should the user ensure are setup correctly?
    6·2 answers
  • When a new word processing software program is released, companies that might use it must consider not only the cost of buying i
    13·1 answer
  • Which of the following tools helps ensure your document will open in older versions of word
    13·1 answer
  • Can you help me with this question please ​
    13·1 answer
  • Write a one page report describing the computer the client used, who else had access to it and other relevant findings. Referenc
    7·1 answer
  • Can u please help me solve this
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!