#include<iostream> using namespace std; template<typename T> class Stack { public: Stack(int size = 0); ~Stack(); bool push(T element); void pop(T &p); void clearStack(); bool isFull(); bool isEmpty(); void StackTraverse(); int StackLength(); //char operator[](int index); private: T *m_iStack; int m_iSize; int m_iTop; }; //template<typename T> //char Stack<T>::operator[](int index) //{ // return (char)m_iStack[index]; //} template<typename T> Stack<T>::Stack(int size) { m_iSize = size; m_iStack = new T[m_iSize]; m_iTop = 0; } template<typename T> Stack<T>::~Stack() { delete[]m_iStack; m_iStack = NULL; } template<typename T> bool Stack<T>::isFull() { return m_iTop == m_iSize ? true : false; } template<typename T> bool Stack<T>::isEmpty() { return 0 == m_iTop ? true : false; } template<typename T> void Stack<T>::pop(T &p) { if (!isEmpty()) { p = m_iStack[--m_iTop]; } } template<typename T> bool Stack<T>::push(T element) { if (isFull()) { return false; } m_iStack[m_iTop++] = element; return true; } template<typename T> void Stack<T>::clearStack() { m_iTop = 0; } template<typename T> int Stack<T>::StackLength() { return m_iTop; } template<typename T> void Stack<T>::StackTraverse() { for (int i = --m_iTop; i >= 0; i--) { cout << m_iStack[i] ; } } int main() { Stack<char> *p = new Stack<char>(30); Stack<char> *pNeed = new Stack<char>(30); char a = 0; char N[] = "[()]"; char CurrentNeed = 0; for (int i = 0;i < (int)strlen(N) ; i++) { if (N[i] != CurrentNeed) { p->push(N[i]); switch (N[i]) { case '[': if (CurrentNeed != 0) { pNeed->push(CurrentNeed); } CurrentNeed = ']'; break; case '(': if (CurrentNeed != 0) { pNeed->push(CurrentNeed); } CurrentNeed = ')'; break; } } else { char elem; p->pop(elem); pNeed->pop(CurrentNeed); } /*cout << CurrentNeed << "Current" << endl; p->StackTraverse(); cout << "p" <<endl; pNeed->StackTraverse(); cout << "pNeed" <<endl;*/ } if (p->StackLength() == 0) if (pNeed->StackLength() == 0) cout << "匹配" << endl; cout << "不匹配" << endl; delete p; //断点调试 p = NULL; delete pNeed; pNeed = NULL; system("pause"); return 0; }
在断点调试的时候说在p的析构函数上面发生异常,但是如果把注释的那段注释掉就不会出现问题,请问为什么?
onemoo
相关分类