1

where is this header file is defined explain how this header file is used in this program and how it works.This code is working but I don't know how this header file is used in this program.Usually we define class for stack and its function inside it but here we have not done that we just use this header file and functionality of how we can convert infix to post fix expression.

#include<bits/stdc++.h> 
using namespace std; 
int prec(char c) 
{ 
    if(c == '^') 
    return 3; 
    else if(c == '*' || c == '/') 
    return 2; 
    else if(c == '+' || c == '-') 
    return 1; 
    else
    return -1; 
} 
void infixToPostfix(string s) 
{ 
    std::stack<char> st; 
    st.push('N'); 
    int l = s.length(); 
    string ns; 
    for(int i = 0; i < l; i++) 
    { 
        if((s[i] >= 'a' && s[i] <= 'z') ||  
           (s[i] >= 'A' && s[i] <= 'Z')) 
        ns+=s[i]; 
        else if(s[i] == '(') 
          
        st.push('('); 
        else if(s[i] == ')') 
        { 
            while(st.top() != 'N' && st.top() != '(') 
            { 
                char c = st.top(); 
                st.pop(); 
               ns += c; 
            } 
            if(st.top() == '(') 
            { 
                char c = st.top(); 
                st.pop(); 
            } 
        } 
        else{ 
            while(st.top() != 'N' && prec(s[i]) <=  
                                   prec(st.top())) 
            { 
                char c = st.top(); 
                st.pop(); 
                ns += c; 
            } 
            st.push(s[i]); 
        } 
  
    } 
    while(st.top() != 'N') 
    { 
        char c = st.top(); 
        st.pop(); 
        ns += c; 
    } 
      
    cout << ns << endl; 
  
} 
int main() 
{ 
    string exp = "a+b*(c^d-e)^(f+g*h)-i"; 
    infixToPostfix(exp); 
    return 0; 
}  
  • 12
    We don't use it. At least not, if we want to be taken serious by other c++ programmers. – πάντα ῥεῖ Feb 02 '21 at 11:18
  • 1
    Also make sure that your program has elaborate and descriptive variable names, that others can better read, what your code is doing (usually better than explanation in comments). – πάντα ῥεῖ Feb 02 '21 at 11:23
  • 1
    Here is a practical example which provides insight into why this "We don't." is NOT pure theoretical nitpicking: https://stackoverflow.com/questions/65122780/why-do-i-get-an-error-in-this-code-when-using-using-namespace-std-and-bits-s/65123038#65123038 – Yunnosch Feb 02 '21 at 11:27
  • I don't use it because it doesn't work on my machine. (But I wouldn't use it anyway, as per the linked answer.) – Eljay Feb 02 '21 at 12:31

0 Answers0