c语言程序验证括号匹配问题在线等

#include<stdio.h>#include<string.h>#definem20typedefcharElemtype;typedefstruct{Elemty... #include<stdio.h>
#include<string.h>
#define m 20
typedef char Elemtype;
typedef struct
{
Elemtype stack[m];
int top;
}stacknode;
stacknode *sp;
void init(stacknode *st)
{

st->top=0;

}
void push(stacknode *st, Elemtype x)
{
if(st->top==m)
printf("栈溢出");
else
{
st->top=st->top+1;
st->stack[st->top]=x;
}
}
void pop(stacknode *st)
{
st->top=st->top-1;
}
void main()
{
char s[m];
int i,k;
printf("creat a empty stack!\n");
init(sp);
printf("input a expression:\n");
gets(s);
for(i=0;i<=strlen(s);i++)
{
if(s[i]=='(')

push(sp,s[i]);
pop(sp);

}

if(sp->top==0)
printf("左右括号是匹配的!\n");
else
printf("左右括号 不是匹配的!\n");
}

怎么错了呢
谢谢你啊,不过似乎还有个小问题
当输入“)(”时它显示是配对的
OK了,搞定了,再次谢谢你!
展开
 我来答
flying_search
2007-12-06 · TA获得超过131个赞
知道答主
回答量:207
采纳率:0%
帮助的人:0
展开全部
//你看看吧!能用了
//那就是你程序的逻辑有问题了,该你自己考虑了
#include<stdio.h>
#include<string.h>
#define m 20
typedef char Elemtype;
typedef struct
{
Elemtype stack[m];
int top;
}stacknode;
stacknode *sp;
void init(stacknode *st)
{

st->top=0;

}
void push(stacknode *st, Elemtype x)
{
if(st->top==m)
printf("栈溢出");
else
{
++st->top;
st->stack[st->top]=x;
}
}
void pop(stacknode *st)
{
--st->top;
}
void main()
{
char s[m];
printf("creat a empty stack!\n");
stacknode st;//我加的
sp=&st;//我加的
init(sp);
printf("input a expression:\n");
gets(s);
for(int i=0;i<strlen(s);i++)
{
if(s[i]=='(')
push(sp,s[i]);
else//我加的
pop(sp);

}

if(sp->top==0)
printf("左右括号是匹配的!\n");
else
printf("左右括号 不是匹配的!\n");
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式