Infix to Postfix notation C

0 votes

Hello there, Stack. 

I'm currently learning C++ and trying to create an RPN converter. 

But I'm having difficulties. 

Hopefully, I'll be able to describe the issues in detail. 

For stacking my operators, I'm using an array. 

Let's look at the example "5 + 8." 

When it comes down to:

else if(infix[i] == '+' or infix[i] == '-' or infix[i] == '*' or infix[i] == '/' or infix[i] == '^'){
                while(isp(stack1.top()) >= icp(infix[i])){
                    postfix += stack1.pop();
                }
                if(isp(stack1.top()) < icp(infix[i])){
                    stack1.push(infix[i]);      
                }

For whatever reason, it will add the operator to the stack but not to the postfix string variable to which I am adding my items. 

The output would be something like "5 8." 

I checked my pop function and everything appears to be correct, but I'm at a loss. 

If you could point me in the correct way, that would be fantastic.

This is my whole code:

#include <iostream>
#include <string>
#define DEFAULT_SIZE 100

using namespace std;

class Stack{

private:
    char *array;
    int tos, capacity;

public:
    //constructors
    Stack();

    //Destructor
    ~Stack();

    //Methods
    void push(char a);
    char pop();
    char top();
    int get_size();
    bool is_empty();
    bool is_full();
    void display();    

};

Stack::Stack(){
    array = new char[DEFAULT_SIZE];
    tos = 0;
    capacity = DEFAULT_SIZE;
}

Stack::~Stack(){
    delete[] array;
}

void Stack::push(char a){
    if(!is_full())
        array[tos++] = a;
}

char Stack::pop(){
        return array[--tos];
}

char Stack::top(){
    return array[tos];
}

int Stack::get_size(){
    return tos; 
}

bool Stack::is_empty(){
    if(tos == 0)
        return true;
    else
        return false;
}

bool Stack::is_full(){
    if(tos == capacity)
        return true;
    else
        return false;
}

void Stack::display(){
    if (tos == 0)
        cout<<"The stack is empty"<<endl;
    else{
        for (int i=0; i<tos;i++)
                cout<<array[i]<<" ";
        cout<<endl;        
    }
}

int isp(char a){
    if(a == '^'){
        return 3;
    }
    else if (a == '*' or a == '/'){
        return 2;
    }
    else if(a == '+' or a == '-'){
        return 1;
    }
    else if(a == '('){
        return 0;
    }
    else
        return -1;
}

int icp(char a){
    if(a == '^'){
        return 4;
    }
    else if (a == '*' or a == '/'){
        return 2;
    }
    else if(a == '+' or a == '-'){
        return 1;
    }
    else if(a == '('){
        return 4;
    }
}



int main(){
    string infix, postfix;
    Stack stack1;

    cout << "This is a Infix to Postfix Expression converter." << endl;
    cout << "Enter your Infix Expression: ";
    cin >> infix;
    stack1.push('#');

    for(int i=0;i<infix.length();i++){
        if(isdigit(infix[i]) or isalpha(infix[i])){
            postfix += infix[i];
        }
        else if(infix[i] == '+' or infix[i] == '-' or infix[i] == '*' or infix[i] == '/' or infix[i] == '^'){
            while(isp(stack1.top()) >= icp(infix[i])){
                postfix += stack1.pop();
            }
            if(isp(stack1.top()) < icp(infix[i])){
                stack1.push(infix[i]);      
            }
        }
    }
    cout << postfix;


    return 0;
}

Also, if you know of any decent C++ RPN converter resource sites, please share them since they would be quite helpful! 

I'm using a random algorithm I discovered on Google.

Jun 29 in C++ by Nicholas
• 4,720 points
15 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1 in C++ by Damon
• 4,760 points
47 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 4,760 points
36 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1 in C++ by Nicholas
• 4,720 points
24 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered Jun 21 in C++ by Damon
• 4,760 points
27 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 4,760 points
40 views
0 votes
1 answer

C++ - Decimal to binary converting

The.to string() function of std::bitset returns a std::string containing a binary text representation with leading-zero padding. Use std::bitset32> to get 32-character strings from 32-bit integers, or std::bitset32> to get 32-character strings from 32-bit integers. #include <iostream> #include <bitset> int main() { ...READ MORE

answered Jun 10 in C++ by Damon
• 4,760 points
16 views
0 votes
0 answers

How to traverse stack in C++?

Is traversing std::stack possible in C++? It is not possible to traverse using the following method.  Because there is no member end in std::stack. std::stack<int> foo; // .. for (__typeof(foo.begin()) it = foo.begin(); ...READ MORE

Jun 1 in C++ by Nicholas
• 4,720 points
61 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
204 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,409 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
803 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP