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
What is the best use of a line chart?
lapo4ka [179]
Line graphs can also be used to compare changes over the same period of time for more than one group. Pie charts are best to use when you are trying to compare parts of a whole. They do not show changes over time. Bar graphs are used to compare things between different groups or to track changes over time.
3 0
3 years ago
Help Me Please. I'm Begging you
Luba_88 [7]

Answer:

d and b can't see the last ones answer so im guessing this is what you needed.

Explanation:

8 0
2 years ago
What are the diffrent types of contract
igomit [66]
<span>Fixed Price Contracts, Cost Reimbursable Contracts, and <span>Time and Material Contracts are the three basic types. Not to mention </span></span>Sale contracts, Employment contracts, business contracts, and leases.
4 0
3 years ago
Create a Boolean function odd_number_digits(n) that returns True when a positive integer has an odd number of digits. (You may a
Snowcat [4.5K]

Answer:

Following are the code to this question:

import java.util.*;//import package for user input

public class Main//defining class main

{

   public static boolean odd_number_digits(int n)//defining boolean method odd_number_digits

   {

       if(n>0 && n%2!=0)//defining if block that check value is positive and odd number

       {

       return true;//return value true

       }

       else//defining else block

       {

           return false;//return false value

       }

   }

   public static void  sum_odd_digits(int n)//defining a method sum_odd_digits

   {

       int sum=0,i;//defining integer variable

       for(i=0;i<=n;i++)//defining for loop

       {

           if(i%2!=0)//defining if block for odd number

           {

               sum=sum+i;//add odd number

           }

       }

      System.out.print(sum);//use print method to print sum value

   }

public static void main(String[] args) //defining main method

   {

       Scanner ox=new Scanner(System.in);//creating Scanner object

       int n= ox.nextInt();//defining integer variable for input value

       System.out.print(odd_number_digits(n)+ "\n");//use print method to call method

       System.out.println("Sum of odd numbers: ");//print message

       sum_odd_digits(n);//calling method

   }

}

Output:

please find the attachment.

Explanation:

In the above code, two methods "odd_number_digits and sum_odd_digits" are defined in which the first method return type is boolean because it will true or false value, and the second method returns the sum of odd numbers.

In the "odd_number_digits" method,  an integer variable passes as an argument and inside the method, if block is used that check value is a positive and odd number then it will return a true value.

In the "sum_odd_digits" method, it accepts an integer parameter "n", and define integer variable "sum" inside the method, which uses the for loop, inside the loop if block is used that counts odd numbers and adds its in sum and print its value.

8 0
2 years ago
In order to use NetWitness Investigator to analyze the same packets that you analyzed with Wireshark, you first had to save the
rusak2 [61]

Answer:

.pcap

Explanation:

He initially have to store the DemoCapturepcap.pcapng file throughout the older.pcap format in which of using NetWitness Examiner to examine a certain transmissions that the person examined with Wireshark.

PCAP should be used for the protocol analyzer, server control, traffic producer, server checker or device for detecting intrusion. It is quite user-friendly and seems to be compliant with such a large range of methods. In reality, for further over a variety of different transmission capturing methods, t is used as the platform.

5 0
2 years ago
Other questions:
  • Describe one activity that belongs to the organizing phase software engineering.
    8·2 answers
  • A very simple device that connects network components and sends packets of data to all other connected devices is called a _____
    5·1 answer
  • What are the six peripherals of a computer system
    14·1 answer
  • Which type of chart is used to chart progress over time?
    10·1 answer
  • Create a class 'ProblemSolution' with following characteristics A public method 'solution' without parameters and return type is
    14·1 answer
  • Jason Has A Science Video Project, As He Creats His Project, He Saves, Then Comes Back A Minute Later. The File Is Now Unreadabl
    8·2 answers
  • What is the output of the code snippet given below?string s = "abcde";int i = 1;while (i &lt; 5){ cout &lt;&lt; s.substr (i, 1);
    11·1 answer
  • Computer technology has changed our lives write of a least five ways to show how this is so​
    15·1 answer
  • give one word • pressure exerted by a stationary liquid _______. • force in a direction perpendicular to a given surface area __
    9·1 answer
  • which term describes the layer of software that resides between the virtual operating system and the physical hardware it runs o
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!