2014-11-14 57 views
-5

我有問題轉換綴與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; 


} 


> 
+0

不完全是一個SSCCE就是它了。 – Bathsheba 2014-11-14 15:01:33

+0

@Bathsheba我沒有得到你? – 2014-11-14 15:03:00

+0

http://www.sscce.org – Bathsheba 2014-11-14 15:04:48

回答

1

你在下面一行犯的錯誤:

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

由於該程序進入無限循環。

它需要:

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

@rockder至少它在輸出中提供了一些東西,但問題仍然存在cinversion是錯誤的 – 2014-11-14 15:25:43

+0

太棒了。現在開始調試你的邏輯。 – rockoder 2014-11-14 15:28:06

+0

@rockder Thnx!我可以做剩下的事情,但我想知道如果我想添加這些xpressions {},[]將會有什麼條件,我應該如何對待他們()他們有相同的優先級? – 2014-11-14 15:30:32