#include \"stdafx.h\" #include const int Increament = 20; template class SeqStack //顺序栈类定义 { public: SeqStack(int sz = 50); //构造函数 ~SeqStack() { delete[]elements; } //析构函数 void Push(const T& x); //进栈 bool Pop(T& x); //出栈 bool getTop(T& x); //取栈顶元素 bool IsEmpty() const { return top == -1; } bool IsFull() const { return top == maxSize - 1; } int getSize()const { return top + 1; } void MakeEmpty() { top = -1; } void output(); private: T *elements; //栈元素存放数组 int top; //栈顶指针 int maxSize; //栈最大容量 void overflowProcess(); //栈的溢出处理 }; template SeqStack elements = new T[maxSize]; assert(elements != NULL); } template void SeqStack T *newArray = new T[maxSize + Increament]; assert(newArray != NULL); for (int i = 0; i <= top; i++) newArray[i] = elements[i]; maxSize = maxSize + Increament; delete[]elements; elements = newArray; } template void SeqStack { //若栈不满, 则将元素x插入栈顶, 否则溢出处理 if (IsFull() == true) overflowProcess(); //栈满 elements[++top] = x; //栈顶指针先加1, x再进栈 } template bool SeqStack { //若栈不空,返回栈顶元素的值,栈顶指针退1 if (IsEmpty() == true) return false; x = elements[top--]; //栈顶指针退1 return true; //退栈成功 } template bool SeqStack { //若栈不空则函数返回该栈栈顶元素的值 if (IsEmpty() == true) return false; x = elements[top]; return true; } template void SeqStack cout << \"top = \" << top << endl; if (top == -1) cout << \"空栈!\"; else { for (int i = 0; i <= top; i++) cout << setw(5) << elements[i]; } cout << endl; } bool Match(char *expression) { SeqStack case'(': case'{': case'[':S.Push(ch);break; case')': { S.Pop(x); if (x == '(') t = 1; else t = 0; }break; case']': { S.Pop(x); if (x == '[') t = 1; else t = 0; }break; case'}': { S.Pop(x); if (x == '{') t = 1; else t = 0; }break; default:break; } if (!t) break; } if (t == 1 && S.IsEmpty() == true) { cout << \"success\" << endl; return true; } else { cerr << \"failure\" << endl; return false; } } int main() { char c[20]; cin >> c; Match(c); return 0; } 因篇幅问题不能全部显示,请点此查看更多更全内容