中缀到Postfix代码转换

时间:2014-11-14 14:58:56

标签: c++ stack doubly-linked-list

我有问题将中缀转换为postfix这是我的代码它需要characterinfix输入但不显示任何后缀输出请告诉我什么是问题。我一直试图解决它但我发现它没有问题我会如果有人发现问题,请感谢你。另一件事是我怎么可以添加{}和[]括号?

#include <iostream>
#include<string.h>
using namespace std;

template <class T>
class Node
{
    public:
        T info;
        Node *ptrnext;
        Node *ptrprevious;
        Node()
        {
            info = 0;
            ptrnext = 0;
            ptrprevious = 0;
        }
        Node(T e, Node *n, Node *p)
        {
            info = e;
            ptrnext = n;
            ptrprevious = p;
        }
};

template <class T>
class DLinkedList
{
    private:
        Node<T> *head;
        Node<T> *tail;
    public:
        DLinkedList()
        {
            head = 0;
            tail = 0;
        }
        bool isEmpty();
        void addToHead(T e);
        void addToTail(T e);
        void deleteFromHead();
        void deleteFromTail();
        void display();
        T getHead();
        int numofNodes();
        ~DLinkedList(){
            if(!isEmpty())
            {
                while(head!=0){
            if(head==tail)
            {
                delete head;
                head=0;
                tail=0;
            }
            else{
                Node<T>  *ptrtemp=head;
                head=head->ptrnext;
                head->ptrprevious=NULL;
                delete ptrtemp;

            }


        }
            }
        }
};

template <class T>
bool DLinkedList<T>::isEmpty()
{
    if (head == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <class T>
void DLinkedList<T>::addToHead(T e)
{
    Node<T> *ptrnode = new Node<T>(e,0,0);
    if(isEmpty())
    {
        head = ptrnode;
        tail = ptrnode;
    }
    else
    {
        ptrnode->ptrnext = head;
        head->ptrprevious = ptrnode;
        ptrnode->ptrprevious = 0;
        head = ptrnode;
    }
}

template <class T>
void DLinkedList<T>::addToTail(T e)
{
    Node<T> *ptrnode = new Node<T>(e,0,0);
    if(isEmpty())
    {
        head = ptrnode;
        tail = ptrnode;
    }
    else
    {
        tail->ptrnext = ptrnode;
        ptrnode->ptrprevious = tail;
        ptrnode->ptrnext = 0;
        tail = ptrnode;
    }
}

template <class T>
void DLinkedList<T>::deleteFromHead()
{
    if(!isEmpty())
    {
        if(head == tail)
        {
            delete head;
            head = 0;
            tail = 0;
        }
        else
        {
            Node<T> *ptrtemp = head;
            head = head->ptrnext;
            head->ptrprevious = 0;
            delete ptrtemp;
        }
    }
}

template <class T>
void DLinkedList<T>::deleteFromTail()
{
    if(!isEmpty())
    {
        if(head == tail)
        {
            delete tail;
            head = 0;
            tail = 0;
        }
        else
        {
            Node<T> *ptrtemp = tail;
            tail = tail->ptrprevious;
            tail->ptrnext = 0;
            delete ptrtemp;
        }
    }
}

template <class T>
void DLinkedList<T>::display()
{
    if(!isEmpty())
    {
        Node<T> *ptrtemp = head;
        while(ptrtemp->ptrnext != 0)
        {
            cout<<ptrtemp->info;
            ptrtemp = ptrtemp->ptrnext;
        }
        cout<<ptrtemp->info<<endl;
    }
}

template <class T>
T DLinkedList<T>::getHead()
{

    if(isEmpty())
    {
        return 0;
    }
    else
    {
    return head->info;
    }
}

template <class T>
int DLinkedList<T>::numofNodes()
{
    if(isEmpty())
    {
        return 0;
    }
    else
    {
        int count = 0;
        Node<T> *ptrtemp = head;
        while(ptrtemp->ptrnext != 0)
        {
            count++;
            ptrtemp = ptrtemp->ptrnext;
        }
        count++;
        return count;

    }
}

template <class T>
class Stack:public DLinkedList<T>
{
    private:
        int maxStackSize;
    public:
        Stack()
        {
            maxStackSize = 10;
        }
        bool isEmpty();
        bool isFull();
        void Push(T e);
        T Pop();
        void display();
        T topvalue();
};

template <class T>
bool Stack<T>::isEmpty()
{


    bool r= DLinkedList<T>::isEmpty();
    return r;
}

template <class T>
bool Stack<T>::isFull()
{
    int totalNodes = DLinkedList<T>::numofNodes();
    if(totalNodes == maxStackSize)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <class T>
void Stack<T>::Push(T e)
{

    if( isFull() )
    {
    cout<<"Stack Full "<<endl;
    }
    else
    {
        DLinkedList<T>::addToHead(e);
    }

}

template <class T>
T Stack<T>::Pop()
{

    if(isEmpty())
    {

        return 0;
    }
    else
    {
        T n = DLinkedList<T>::getHead();
        DLinkedList<T>::deleteFromHead();
        return n;
    }

}

template <class T>
void Stack<T>::display()
{


    if( isEmpty() )
    {
        cout<<"Stack Empty!!"<<endl;
    }
    else
    {
        DLinkedList<T>::display();
    }

}

template<class T>
T Stack<T>::topvalue()
{
    T temp;
    temp=DLinkedList<T>::getHead();
    return temp;
}

int main()
{
    Stack<char> obj;
    char input[20];
    cout<<"Enter Values \n";
    cin>>input;
    int size= strlen(input);
    for(int i=0; i <size ; i++)
    {
          //======= For + or - Operators=========
        if(input[i]=='+' || input[i]=='-')
        {
          if(obj.topvalue()=='+' || obj.topvalue()=='-')
          {  //======= CASE-1=======
              cout<<obj.Pop();
              obj.Push(input[i]);
          }
          else if(obj.topvalue()=='*' || obj.topvalue()=='/')
          {
              //======= CASE-2=========
              cout<<obj.Pop();
              if(obj.topvalue()=='*' || obj.topvalue()=='/')
              {
                  cout<<obj.Pop();
              }
              obj.Push(input[i]);
           }
          else if(obj.topvalue()=='(')
          {
              //======= CASE-3=========
              obj.Push(input[i]);
          }
          else if(obj.isEmpty())
          {
              //======= CASE-4=========
              obj.Push(input[i]);
          }

        }
        // ++++++++++ FOR  * and / Operators ++++++++
        else if(obj.topvalue()=='*' || obj.topvalue()=='/')
        {
            if(obj.topvalue()=='+' || obj.topvalue()=='-')
            {
                 //======= CASE-1=========
                cout<<obj.Pop();
                obj.Push(input[i]);
            }
            else if(obj.isEmpty())
            {
                 //======= CASE-2=========
                obj.Push(input[i]);
            }
            else if(obj.topvalue()=='(')
            {
                 //======= CASE-3=========
                obj.Push(input[i]);
            }
            else
            {
                 //======= CASE-4=========
                obj.Push(input[i]);
            }

        }
        // ++++++++++ Opening bracket  ++++++++
        else if (obj.topvalue()=='(')
        {
            obj.Push(input[i]);
        }
        // ++++++++++ Closing Bracket ++++++++
        else if(input[i] != ')')
        {
            while(obj.topvalue() != '(')
            {
                cout<<obj.Pop();
            }
            obj.Pop();
        }
        // ++++++++++ Operands ++++++++
        else
        {
            cout<<input[i];
        }
    }
    // ++++++++++ Print All values from the Stack and empty it++++++++
    while(!obj.isEmpty())
    {
        cout<<obj.Pop();

    }



    return 0;


}


> 

1 个答案:

答案 0 :(得分:1)

你在下一行中犯了错误:

else if (input[i] != ')')

因为程序进入无限循环。

需要:

else if (input[i] == ')')