#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;??//斷點(diǎn)調(diào)試
????p?=?NULL;
????delete?pNeed;
????pNeed?=?NULL;
????system("pause");
????return?0;
}在斷點(diǎn)調(diào)試的時(shí)候說在p的析構(gòu)函數(shù)上面發(fā)生異常,但是如果把注釋的那段注釋掉就不會(huì)出現(xiàn)問題,請(qǐng)問為什么?
1 回答
已采納

onemoo
TA貢獻(xiàn)883條經(jīng)驗(yàn) 獲得超454個(gè)贊
StackTraverse 函數(shù)中,你是不是想讓 i 從 m_iTop-1 循環(huán)到 0 來依次打印整個(gè)棧?
如果是這樣的話,那 for 循環(huán)的初始部分?int?i?=?--m_iTop 就有問題了! ?這樣做雖然 i 確實(shí)初始化為 m_iTop-1,但是 m_iTop 也自減了!結(jié)果每調(diào)用一次?StackLength() 棧頂就同時(shí)減少一位,這不是你想要的結(jié)果吧。
事實(shí)上,105 行的 for 循環(huán)中,只要 107 行?N[i]?!=?CurrentNeed,就會(huì)先 push——棧頂+1,后?StackLength() 又會(huì)使棧頂減 1 重新變?yōu)?0。 而只要某一次循環(huán)時(shí)?N[i] ==?CurrentNeed,就會(huì)調(diào)用 pop,這會(huì)訪問?m_iStack[-1],恐怕這會(huì)引起問題。
- 1 回答
- 0 關(guān)注
- 1203 瀏覽
添加回答
舉報(bào)
0/150
提交
取消