Question

In: Computer Science

Write a C++ program that converts an infix expression, which includes (, ), +, -, *,...

Write a C++ program that converts an infix expression, which includes (, ), +, -, *, and / operations to postfix notation. The program should allow the user to enter an infix expression using lower case characters, then it will display the result of conversion on the screen. (Note: Use the STL stack class to accomplish the solution.).

Solutions

Expert Solution

SOLUTION:

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int precedence(char ch);
//This function is used to the precedence of the operators
//operator precedence is like + and - has same precedence
// * and / has same precedence and has greater than + and -
// braces has the highesht precedence than * and /


int precedence(char ch)
{

   if(ch == '+' || ch == '-')
   return 1;
else if(ch == '*' || ch == '/')
   return 2;
   return -1;
}
  
int main()
{
string s,output;
cout<<"Enter the infix Expression:";
//Read the infix expression
   cin>>s;
int len = s.length();
//Declare the stack
stack<char> stac;
for(int i = 0; i < len; i++)
{
// if the variable is from a to z append to the output
if(s[i] >= 'a' && s[i] <= 'z'){
   output+=s[i];
       }
       // if we encounter an ( push it to the stack
else if(s[i] == '(') {
   stac.push('(');   
       }
       // if we encounter ) pop all the data till the ( and append to the output and pop (
else if(s[i] == ')')
{
while(!stac.empty() && stac.top() != '(')
{
  
char c = stac.top();
               stac.pop();
               output += c;
}
stac.pop();
}
// when we see the operator we need to check the precedence of the operator
// we have to follow three rules when we encounter an operator
//1. If the operator has equal precedence pop the operator from the stack and append to the output and push the encountered
//operator to the stack
//2. If we encounter an operator which is having the greater precedence than the encountered operator
//push it onto the stack
//3. if we encounter an operator which is having the less precedence than the encountered operator
//pop from the stack and append it onto the output and push the encountered operator
else{
while(!stac.empty() && precedence(s[i]) <= precedence(stac.top()))
{
char c = stac.top();
stac.pop();
output += c;
}
stac.push(s[i]);
}
  
}
//if we have completed the parsing the data we need to pop all the data from stack and append it to the output
while(!stac.empty())
{
char c = stac.top();
stac.pop();
output += c;
}
//print the final output
cout <<"PostFix expression is :"<< output;
return 0;
}

EXPLANATION:

Rules of the stack :

1. If the operator has equal precedence pop the operator from the stack and append to the output and push the encountered
operator to the stack
2. If we encounter an operator which is having the greater precedence than the encountered operator push it onto the stack
3. if we encounter an operator which is having the less precedence than the encountered operator
pop from the stack and append it onto the output and push the encountred operator

Precedence Rules:

1 operator precedence is like + and - has the same precedence
2 * and / has the same precedence and has greater than + and -
3 braces have the highest precedence than * and /

SOURCE CODE

OUTPUT:


Related Solutions

Write a class PostFix that has one method covert that converts an infix expression (as a...
Write a class PostFix that has one method covert that converts an infix expression (as a string input) to a postfix. Then Test it in a different class. code in java.
Write a program that takes an infix expression as input and produces a postfix expression. The...
Write a program that takes an infix expression as input and produces a postfix expression. The infix and postfix expressions are in the form of vectors of strings. We will test with a main method that takes a file name as input from the command line. We will test your implementation using printPostFix() method.   There are sample input and output files in this folder. You may edit the header file. Example 1 infix expression = apple + banana * cat...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix...
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and Queue classes (my code is a...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
Following is an infix expression.                                       &n
Following is an infix expression.                                           ((A ^ B) ^ C ^ M * W / X ) ^ Y ^ Z Convert it into postfix and prefix using stack and verify through binary tree. Evaluate infix, postfix and prefix with the following values. A = 2, B = 2, C = 3, M = 1, W = 4, X = 8, Y = 1, Z = 3
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
2. Write a C++ program that; Takes in the weight of a person in Kilograms, converts...
2. Write a C++ program that; Takes in the weight of a person in Kilograms, converts and outputs the equivalent weight in pounds. Format your output to 3 decimal places. Your output should look like this 53.000 Kg is equivalent to 123.459 Ibs (Note 1Kg = 2.2046226218488 lbs) Takes in the price of an item on an online store in pound sterling, converts and outputs the equivalent price in U.S dollars. Format your output to 2 decimal places. Your output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT