#include<stdio.h> #include<string.h> #define m 20; typedef char ElemType; typedef struct { ElemType

#include<stdio.h>
#include<string.h>
#define m 20;
typedef char ElemType;
typedef struct
{
ElemType stack[m];
int top;
}stacknode;
stacknode sp;
init(stacknode st)
{
st.top=0;
return 0;
}
void push(stacknode st,ElemType x)
{
if(st.top==m)
printf("the stack is overflow!\n");
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("create 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]);
if(s[i]==')')
pop(sp);
}
if(sp.top==0)
printf("左右括号是匹配的:\n");
else
printf("左右括号不匹配:\n");
} 帮忙调试下吧!谢谢!

第1个回答  2011-03-29
照着你的程序修改的,能够运行
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define m 20
typedef char ElemType;
typedef struct
{
ElemType stack[m];
int top;
}stacknode;
void init(stacknode *&s)
{
s=(stacknode *)malloc(sizeof(stacknode)); //申请空间
s->top=-1;
}
int push(stacknode *s,ElemType x)
{
if(s->top==m-1) //栈满的情况
printf("the stack is overflow!\n");
else
{
s->top++;
s->stack[s->top]=x;
}
}
int pop(stacknode *&s,ElemType &x)
{
if(s->top==-1) //栈为空的情况
printf("the stack is empty!\n");
return 0;
x=s->stack[s->top];
s->top--;
return 1;
}
int main()
{
stacknode *s;
char a[m];
int i,k;
printf("create a empty stack!\n");
init(s);
printf("input a expression:\n");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]=='(')
push(s,a[i]);
if(a[i]==')')
pop(s,a[i]);
}
if(s->top==0)
printf("左右括号是匹配的:\n");
else
printf("左右括号不匹配:\n");
}本回答被提问者采纳
第2个回答  2011-03-29
了下调通了*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define stack_size 20
typedef struct {
char zifu[stack_size];
int top;
}seqstack;
void Initstack(seqstack *S)
{
S->top=-1;
}
int push(seqstack *S,char *zimu) /* 进栈 */
{
if(S->top>=stack_size-1)
return 0;
S->top++;
strcpy(&(S->zifu[S->top]),zimu);
/*注意函数的申明,两个都是必须地址,其他调用strcpy、strcmp函数都有此错误*/
return 1;
}
int pop(seqstack *S) /* 出栈 */
{
if(S->top<0)/*S你都写错了,C++是区分大小写的*/
return 0;
else
{
return(S->zifu[S->top]);
S->top--;
}
}
void main()
{
seqstack *S;
int i=0;
char zimu,ch;
system( "cls ");
/*clrscr()是C下面清屏的,在头文件conio.h里,但是VC的conio.h没有这个函数,我用了替代的办法*/
S=(seqstack *)malloc(sizeof(seqstack));
Initstack(S);
printf("please enter a string end with @:\n");
scanf("%c",&zimu);
while(strcmp(&zimu,"@")!=0)
{
i=push(S,&zimu);
if(i==0)
{
printf("The stack is full! sorry!\n");
break;
}
scanf("%c",&zimu);
}
while((ch=pop(S))!='\0')
printf("%c",ch);
}
相似回答