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
Given class triangle (in files triangle.h and triangle.cpp), complete main() to read and set the base and height of triangle1 an
kow [346]

The C++ program that would complete the main () and set the base and height of triangle1 and of triangle2 is:

main.cpp

#include <iostream>

#include "Triangle.h"

using namespace std;

int main()

{

   Triangle Tri1;  

Triangle Tri2;

   double base1, height1, base2, height2;

   cout << "Enter a base for your Triangle1: ";

   cin >> base1;

   cout << "Enter a height for your Triangle1: ";

   cin >> height1;

   cout << endl;

   cout << "Enter a base for your Triangle2: ";

   cin >> base2;

   cout << "Enter a height for your Triangle2: ";

   cin >> height2;

   cout << endl;

   

   cout << "################################" << endl;

   

   cout << "Triangle with larger area:" << endl;

   if ((0.5)*base1*height1 > (0.5)*base2*height2){

      Tri1.setValues(base1, height1);

      Tri1.getValues();

      cout << "Area: " << Tri1.getArea() << endl << endl;

}

else{

 Tri2.setValues(base2, height2);

 Tri2.getValues();

    cout << "Area: " << Tri2.getArea() << endl;

}

   

   return 0;

}

Read more about C++ programs here:

brainly.com/question/20339175

#SPJ1

3 0
1 year ago
Computer _____ begins with regular maintenance of the computer system such as firewalls, antivirus programs, defragmentation, de
Dima020 [189]
Computer checkup/maintenance. You forgot to mention windows updates, it is critical to perform that action as well cause of the recent ransomeware malware that is going around lately and Microsoft and other OS vendors yes even Apple have released patches to prevent it spreading even further. 
4 0
3 years ago
Which option is used in emails to inform the recipient that they should exercise discretion in accordance with sharing the conte
Kay [80]
Priority levels hehe good luck!
5 0
3 years ago
Please help it's my last question
Yuki888 [10]

Explanation:

here is your answer.. of. different between client / server architecture and peer to peer architecture of the network.

6 0
2 years ago
Read 2 more answers
in Google, how should you phrase your search if you want to exclude a certain word from your results(for example,"chocolate")?
dusya [7]
I found this:
ou can exclude words from your search by using the - operator; any word in your query preceded by the - sign is automatically excluded from the search results. Remember to always include a space before the - sign, and none after

I found it here:
http://www.informit.com/articles/article.aspx?p=675274&seqNum=3
3 0
2 years ago
Other questions:
  • To create a digital file from an old photo, I would use a _____.
    10·1 answer
  • What is called photo and video edition?
    8·1 answer
  • In addition to training on the products and on company policy, it does not make sense to be prepared to speak about your company
    6·2 answers
  • In your memo, give three new employees directions for starting the computer and opening a word-processing document.
    9·1 answer
  • 2 (01.01 LC)
    5·1 answer
  • Write the code to replace only the first two occurrences of the word second by a new word in a sentence. Your code should not ex
    15·1 answer
  • Which of the following statement is correct? Select one: a. Base register holds the size of a process. b. Limit register holds t
    10·1 answer
  • How to be fluent in computer
    10·2 answers
  • What is a photographic print made from a negative image?
    7·1 answer
  • A user calls the help desk reporting that a laptop with Linux freezes on startup and displays kernel panic. What could cause thi
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!