
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了,搞定了,再次谢谢你! 展开
#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了,搞定了,再次谢谢你! 展开
1个回答
展开全部
//你看看吧!能用了
//那就是你程序的逻辑有问题了,该你自己考虑了
#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");
}
//那就是你程序的逻辑有问题了,该你自己考虑了
#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");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询