#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int fdlth = 1024;
typedef struct
{
char *base;
char *top;
int size;
}stack;
void insertstack(stack &s)
{
s.base = new char[fdlth];
while (!s.base)
s.base = new char[fdlth];
s.top = s.base;
s.size = fdlth;
}
void push(stack &s, char c)
{
*s.top = c;
s.top++;
}
void pop(stack &s)
{
if (s.top != s.base)
--s.top;
}
int main()
{
stack stac;
string str;
int i,len;
char c;
insertstack(stac);
cout<<"输入要判断是否匹配正确的括号:\n";
getline(cin,str);
len=str.size();
for(i=0;i<len;)
{
if((str[i]+1!=str[i+1])&&(str[i]+2)!=str[i+1])
{
if(stac.top==stac.base)
push(stac,str[i]);
else
{
c=*(stac.top-1);
if((str[i]==c+1)||(str[i]==c+2))
pop(stac);
else
push(stac,str[i]);
}
}
else
i+=1;
i+=1;
}
if(stac.top==stac.base)
cout<<"匹配正确\n";
else
cout<<"匹配不正确!\n";
delete stac.base;
stac.top=stac.base;
return 0;
}
转载于:https://my.oschina.net/u/3026393/blog/798968