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
Write a program named as calcPrice.c that formats product information entered by the user and calculate the total amount of purc
Colt1911 [192]

Answer:

Here is the calcPrice.c program:

#include <stdio.h>   //to use input output functions

int main(void)  {  //start of main method

   int itemNo, month, day, year, quantity;  //declares variables to hold item number, quantity, and date

   float unitPrice; //declare variable to hold price per unit

   printf("Enter item number: ");  // prompts user to enter item number

   scanf("%d", &itemNo);  //reads item number from user and stores it in itemNo variable

   printf("Enter unit price: ");  // prompts user to enter unit price

   scanf("%f", &unitPrice);  //reads input unit price and stores it in unitPrice variable

   

    printf("Enter quantity: ");  //prompts user to enter quantity

   scanf("%d", &quantity);  //reads input quantity and stores it in quantity variable

   printf("Enter purchase date (mm/dd/yyyy): ");  //prompts user to enter purchase date

   scanf("%d/%d/%d", &month, &day, &year);  //reads input date and stores it in month, day and year variables

    float totalAmount = unitPrice * quantity;  //computes the total amount

   printf("\nItem\tUnit Price\tQTY\tPurchase Date\tTotal Amount\n");  //displays the item, unit price, qty, purchase data and total amount with tab space between each

   printf("%d\t$%5.2f\t%d\t%.2d/%.2d/%d\t$%5.2f\n", itemNo, unitPrice,quantity, month, day, year,totalAmount);   }    //displays the values of itemNo, unitPrice, quantity, month, day, year and computed totalAmount with tab space between each

Explanation:

Lets suppose user enters 583 as item number, 13.5 as unit price, 2 as quantity and 09/15/2016 as date so

itemNo = 583

unitPrice = 13.5

quantity = 2

month = 09

day = 15

year = 2016

totalAmount is computed as:

  totalAmount = unitPrice * quantity;

 totalAmount = 13.5* 2

totalAmount = 27.00

Hence the output of the entire program is:

Item    Unit Price      QTY     Purchase Date   Total Amount      

583     $  13.50           2           09/15/2016         $  27.00  

The screenshot of the program along with its output is attached.

4 0
3 years ago
Refer to the film, actor, and film_actor tables of the Sakila database. The tables in this lab have the same columns and data ty
julsineya [31]

Using the knowledge of computational language in python it is possible to write a code that organizes film, actor and film and actor at the same time.

<h3>Writting the code:</h3>

<em>SELECT a.last_name, a.first_name, ROUND(AVG(f.length)) AS 'average' </em>

<em>FROM film f </em>

<em>INNER JOIN film_actor fa ON f.film_id = fa.film_id</em>

<em>INNER JOIN actor a ON a.actor_id = fa.actor_id</em>

<em>GROUP BY a.last_name, a.first_name</em>

<em>ORDER BY average DESC, last_name ASC;</em>

See more about python at brainly.com/question/18502436

#SPJ1

5 0
2 years ago
1 Type the correct answer in the box. Spell all words correctly. Which skill type refers to the ability to interact and communic
joja [24]

Answer:

interpersonal skills

Explanation:

please rate

8 0
3 years ago
1. Samantha uses the RSA signature scheme with primes p = 541 and q = 1223 and public verification exponent e = 159853. (a) What
ira [324]

Answer:

Why is samantha asking us? She should find out herself smh

Explanation:

4 0
3 years ago
What key should you press and hold to select and open multiple files at one time? Enter Alt Control Esc
Yuliya22 [10]

Press and hold down the CTRL key while you drag the file to another folder. Press and hold down CTRL+SHIFT while you drag a file to the desktop or a folder.

3 0
3 years ago
Read 2 more answers
Other questions:
  • The figure below shows a black and white image with a resolution of 720×504. Identify how much memory (in bits) will be occupied
    15·1 answer
  • Assume that a text box named PhoneNumberTextBox appears on a form and contains the phone number 414-555-5555. What value will di
    5·1 answer
  • Other side for bullying
    7·1 answer
  • Create a class Str that subclasses str. Add a method to the subclass that checks if the string does not start with a given strin
    15·1 answer
  • Angelika just submitted her product for the first time, and she had 2 errors, 3 warnings, and 5 notifications. What does she nee
    7·2 answers
  • The basic input/output system (bios is stored on a ________ chip.
    5·1 answer
  • You give an object a more meaningful name by setting the object’s _________________ property.
    7·1 answer
  • A Homecoming Crossword Puzzle
    12·1 answer
  • Which line of code will use the overloaded multiplication operation?
    8·1 answer
  • In the following code fragment, how many times is the count() method called as a function of the variable n? Use big-O notation,
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!