[栈的简单应用有关问题] 看看小弟我的有关问题出在哪
[栈的简单应用问题] 看看我的问题出在哪?
题目:假设一个算术表达式中可包含三种括号:圆括号(),方括号[]以及花括号{},且这三种括号可以按任意的次序相互嵌套使用。是编写判别给定的表达式所是否正确配对出现的算法。
C源程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 100
typedef int status;
typedef char SElemType;
typedef struct
{ SElemType * base;
SElemType * top;
int stacksize;
}SqStack;
status InitStack(SqStack *S) /*初始化栈*/
{ S->base=(SqStack*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!(S->base)) return 0;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}
status DestroyStack(SqStack *S) /*销毁栈*/
{ if(S->base!=NULL) free(S->base);
S->base=NULL;
S->stacksize=0;
return 1;
}
Status StackEmpty(SqStack *S) /判断栈是否为空*/
{ if(S->base==S->top) return 1;
else return 0;
}
status Pop(SqStack *S) /*栈顶元素出栈*/
{ if(S->top==S->base) return 0;
e=*(S->top-1);
(S->top--);
return 1;
}
status Push(SqStack *S,SElemType e) /*把e送入栈顶*/
{ if((S->top)-(S->base)>=S->stacksize) return 0;
*(S->top)=e;
(S->top)++;
return 1;
}
char GetTop(SqStack *S) /*返回栈顶元素*/
{ char e;
if(S->top==S->base) return ERROR;
e=*(S->top-1);
return e;
}
int Test(char a) /*测试一个字符是否为括号,并返回下表值表示的括号类型*/
{ char str[6]={'{','[','(',')',']','}'}; int i;
for(i=0;i<=6;i++)
{ if(a==str[i]) return i;
}
return -1;
}
main()
{ int i,j,flag; char a; char string[100]; SqStack S; SElemType e;
InitStack(&S);
printf("please input the string:");
gets(string);
for(i=0;i<=strlen(string)-1;i++)
{ a=string[i];flag=TestChar(a); /*测试字符串中第i个字符是否是括号,并将返回值附给flag*/
if(flag>=0&&flag<=2) Push(S,a); /*如果这个字符是左括号,把此字符进栈*/
else if(flag>=3&&flag<=5) /*如果这个字符是右括号*/
{ if(StackEmpty(S)||TestChar(GetTop(S))+flag!=5)
break; /*如果此时栈空或者栈不空但此左括号与栈顶的括号不能配对成一对,跳出循环*/
else Pop(S); /*如果此这个括号与栈顶的括号可以配对,栈顶元素出栈*/
} /*对于其他非括号字符,不于操作,进行下个字符的判断*/
}
if(i==strlen(string)&&StackEmpty(S)) /*循环结束后,如果已经测试完所有字符且栈中无多余的括号,说明配对正确*/
printf("Match!");
else printf("Not Match!"); /*如果中途跳出循环,说明配对不正确*/
}
这个源程序是自己写的,编译时提示 warning 14: Suspicious pointer conversion in function InitStack
Error 28: Declaration syntax error
本人新手,不知道怎么改,希望会的朋友帮忙指点下!
------解决方案--------------------
帮你修改了一下源程序,在Visual Studio 2005中调试通过。唯一修改的地方是Pop()函数,你的代码有些小问题。
题目:假设一个算术表达式中可包含三种括号:圆括号(),方括号[]以及花括号{},且这三种括号可以按任意的次序相互嵌套使用。是编写判别给定的表达式所是否正确配对出现的算法。
C源程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 100
typedef int status;
typedef char SElemType;
typedef struct
{ SElemType * base;
SElemType * top;
int stacksize;
}SqStack;
status InitStack(SqStack *S) /*初始化栈*/
{ S->base=(SqStack*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!(S->base)) return 0;
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 1;
}
status DestroyStack(SqStack *S) /*销毁栈*/
{ if(S->base!=NULL) free(S->base);
S->base=NULL;
S->stacksize=0;
return 1;
}
Status StackEmpty(SqStack *S) /判断栈是否为空*/
{ if(S->base==S->top) return 1;
else return 0;
}
status Pop(SqStack *S) /*栈顶元素出栈*/
{ if(S->top==S->base) return 0;
e=*(S->top-1);
(S->top--);
return 1;
}
status Push(SqStack *S,SElemType e) /*把e送入栈顶*/
{ if((S->top)-(S->base)>=S->stacksize) return 0;
*(S->top)=e;
(S->top)++;
return 1;
}
char GetTop(SqStack *S) /*返回栈顶元素*/
{ char e;
if(S->top==S->base) return ERROR;
e=*(S->top-1);
return e;
}
int Test(char a) /*测试一个字符是否为括号,并返回下表值表示的括号类型*/
{ char str[6]={'{','[','(',')',']','}'}; int i;
for(i=0;i<=6;i++)
{ if(a==str[i]) return i;
}
return -1;
}
main()
{ int i,j,flag; char a; char string[100]; SqStack S; SElemType e;
InitStack(&S);
printf("please input the string:");
gets(string);
for(i=0;i<=strlen(string)-1;i++)
{ a=string[i];flag=TestChar(a); /*测试字符串中第i个字符是否是括号,并将返回值附给flag*/
if(flag>=0&&flag<=2) Push(S,a); /*如果这个字符是左括号,把此字符进栈*/
else if(flag>=3&&flag<=5) /*如果这个字符是右括号*/
{ if(StackEmpty(S)||TestChar(GetTop(S))+flag!=5)
break; /*如果此时栈空或者栈不空但此左括号与栈顶的括号不能配对成一对,跳出循环*/
else Pop(S); /*如果此这个括号与栈顶的括号可以配对,栈顶元素出栈*/
} /*对于其他非括号字符,不于操作,进行下个字符的判断*/
}
if(i==strlen(string)&&StackEmpty(S)) /*循环结束后,如果已经测试完所有字符且栈中无多余的括号,说明配对正确*/
printf("Match!");
else printf("Not Match!"); /*如果中途跳出循环,说明配对不正确*/
}
这个源程序是自己写的,编译时提示 warning 14: Suspicious pointer conversion in function InitStack
Error 28: Declaration syntax error
本人新手,不知道怎么改,希望会的朋友帮忙指点下!
------解决方案--------------------
帮你修改了一下源程序,在Visual Studio 2005中调试通过。唯一修改的地方是Pop()函数,你的代码有些小问题。
- C/C++ code
#include <stdio.h> #include <stdlib.h> #include <string.h> #define STACK_INIT_SIZE 100 typedef int status; typedef char SElemType; typedef struct { SElemType * base; SElemType * top; int stacksize; }SqStack; status InitStack(SqStack *S) { S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!(S->base)) return 0; S->top=S->base; S->stacksize=STACK_INIT_SIZE; return 1; } status DestroyStack(SqStack *S) { if(S->base!=NULL) free(S->base); S->base=NULL; S->stacksize=0; return 1; } status StackEmpty(SqStack *S) { if(S->base==S->top) return 1; else return 0; } status Pop(SqStack *S,SElemType *e) { if(S->top==S->base) return 0; *e=*(S->top-1); (S->top)--; return 1; } status Push(SqStack *S,SElemType e) { if((S->top)-(S->base) >=S->stacksize) return 0; *(S->top)=e; (S->top)++; return 1; } char GetTop(SqStack *S) { char e; if(S->top==S->base) return 0; e=*(S->top-1); return e; } int Test(char a) { char str[6]={ '\x7B', '\x5B', '\x28', '\x29', '\x5D', '\x7D'}; int i; for(i=0;i <=6;i++) { if(a==str[i]) return i; } return -1; } void main() { SElemType e; int i,j,flag; char a; char string[100]; SqStack S; InitStack(&S); printf("please input the string:"); gets(string); for(i=0;i <=strlen(string)-1;i++) { a=string[i]; flag=Test(a); if(flag >=0&&flag <=2) Push(&S,a); else if(flag >=3 && flag <=5) { if(StackEmpty(&S) || Test(GetTop(&S))+flag!=5) break; else Pop(&S,&e); } } if(i==strlen(string)&&StackEmpty(&S)) printf("Match!\n"); else printf("Not Match!\n"); system("pause"); }