2014-11-14 75 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