#ifndef STACK_H_
#define STACK_H_
#include <new>
#include <initializer_list>
template <typename T>
class Stack
{
private:
T * base;
T * top;
unsigned int StackSize; //表示栈内剩余容量
public:
Stack(const unsigned int size, const T * begin, const T * end);
Stack(const unsigned int size, const T arr[], const int n);
Stack(const unsigned int size, const std::initializer_list<T> & l);
Stack(const unsigned int size = 100);
~Stack() {delete [] base;}
bool Push(const T & item);
bool Pop(T & e);
bool Clear();
bool Is_Empty()const {return base == top;}
unsigned int Remaining()const {return StackSize;}
unsigned int StackLen()const {return top - base;}
unsigned int Size()const {return StackSize + (top - base);}
};
template <typename T>
Stack<T>::Stack(const unsigned int size, const T * begin, const T * end) : StackSize(size)
{
if (StackSize < end - begin)
StackSize = end - begin;
try {
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (T * p = begin; p != end; p++)
Push(*p);
}
template <typename T>
Stack<T>::Stack(const unsigned int size, const T arr[], const int n) : StackSize(size)
{
if (StackSize < n)
StackSize = n + 1;
try {
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (int i = 0; i < n; i++)
Push(arr[i]);
}
template <typename T>
Stack<T>::Stack(const unsigned int size, const std::initializer_list<T> & l) : StackSize(size)
{
int i = 0;
try{
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
for (auto p = l.begin(); p != l.end(); p++, i++)
Push(*p);
}
template <typename T>
Stack<T>::Stack(const unsigned int size) : StackSize(size)
{
try {
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
base = top = nullptr;
StackSize = 0;
throw;
}
}
template <typename T>
bool Stack<T>::Push(const T & item)
{
if (StackSize == 0)
{
T * temp = base;
unsigned int size = top - base;
StackSize = size + 10;
try {
base = top = new T[StackSize];
}
catch (std::bad_alloc & ba) {
StackSize = 0;
base = top = nullptr;
delete [] temp;
return false;
}
for (int i = 0; i < size; i++)
Push(temp[i]);
Push(item);
delete [] temp;
return true;
}
*(top++) = item;
StackSize--;
return true;
}
template <typename T>
bool Stack<T>::Pop(T & e)
{
if (top && !Is_Empty())
{
e = *(--top);
StackSize++;
return true;
}
return false;
}
template <typename T>
bool Stack<T>::Clear()
{
if(top)
{
StackSize += top - base;
top = base;
return true;
}
return false;
}
#endif
#include <iostream>
#include "Stack.h"
bool is_match (const char c1, const char c2);
int main()
{
using std::cout;
using std::endl;
using std::cin;
cout << "请输入一组括号,程序将判断是否匹配: ";
char ch;
Stack<char> par(5);
while (cin.get(ch) && ch != '\n')
{
if (ch == '(' || ch == '<'
|| ch == '{')
par.Push(ch);
else if (ch == ')' || ch == '>'
|| ch == '}')
{
char temp = ch;
par.Pop(ch);
if (!is_match(ch, temp))
{
cout << "您输入的括号不完全匹配.\n";
return 0;
}
}
}
cout << "您输入的括号完全匹配!\n";
return 0;
}
bool is_match(const char c1, const char c2)
{
switch (c1)
{
case '(': return c2 == ')';
break;
case '<': return c2 == '>';
break;
case '{': return c2 == '}';
break;
}
return false;
}
|