[b]编程要求如下:
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3
[(])
(])
([[]()])
样例输出
No
No
Yes
程序如下:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct Node
{
char data;
struct Node *pNext;
}NODE,*PNODE;
typedef struct Stack
{
PNODE pTop;
PNODE pBottom;
}STACK,*PSTACK;
void pipei(char *p);
void init(PSTACK pS);
void push(PSTACK pS,char val);
void pop(PSTACK pS);
bool isempty(PSTACK pS);
int M=0;
char a[100];
int main(void)
{
int N;
char ch;
printf("请输入测试数据的数目:");
scanf("%d",&N);
ch=getchar();
if(N>100)
exit(-1);
for(int i=0;i<N;++i)
{
int j=0;
gets(a);
while(a[j]!='\0')
{
j++;
M++;
}
pipei(a);
}
return 0;
}
void pipei(char *p)
{
int i=0,j=0;
STACK S;
init(&S);
for(i;i<=M;i++)
{
switch(p[i])
{
case '(':
case '[': push(&S,p[i]);
case ')': if(p[i-1]=='(') pop(&S); else j=1;break;
case ']': if(p[i-1]=='[') pop(&S); else j=1;break;
}
}
if(j==0)
printf("Yes\n");
else
printf("No\n");
}
void init(PSTACK pS)
{
pS->pTop=(PNODE)malloc(sizeof(NODE));
if(NULL==pS->pTop)
{
printf("out of space!");
exit(-1);
}
pS->pBottom=pS->pTop;
pS->pTop->pNext=NULL;
}
void push(PSTACK pS,char val)
{
PNODE pNew=(PNODE)malloc(sizeof(NODE));
if(NULL==pNew)
{
printf("out of space!");
exit(-1);
}
pNew->pNext=pS->pTop;
pNew->data=val;
pS->pTop=pNew;
}
bool isempty(PSTACK pS)
{
if(pS->pTop==pS->pBottom)
return true;
else
return false;
}
void pop(PSTACK pS)
{
if(!isempty(pS))
{
PNODE r=pS->pTop;
pS->pTop=r->pNext;
free(r);
r=NULL;
}
}
一直输出No,没找出原因,请教各位大神,谢谢
[/b]