回复 9楼 樾宝
你看看前面的帖子,分号的事有几个人说过了,还是不加。细心点这话哪过份了,如果连这话都受不了以后怎么面对社会
总有那身价贱的人给作业贴回复完整的代码
if(e=='('&&c==')') return 1; if(e=='['&&c==']' )return 1;//这块应该括号补全吧
#include"stdio.h" #include <malloc.h> #include <stdlib.h> #define STACK_INIT_SIZE 20 #define STACKINCREMENT 10 #define OK 1 #define ERROR 0 typedef char ElemType ; /*将char类定义为ElemType*/ typedef struct{ /*定义一个栈类型*/ ElemType *base; ElemType *top; int stacksize; }sqstack; void initstack(sqstack *s)//要声明返回为空类型 { s->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));//未引用头文件malloc.h;s->stack改为s->base; if(!s->base) exit(0);//为引用头文件stdlib.h s->top=s->base; s->stacksize=STACK_INIT_SIZE; } void push(sqstack *s, ElemType e)//少了参数e { if(s->top - s->base >= s->stacksize) { s->base=(ElemType *)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(ElemType));//函数malloc改为realloc if(!s->base) exit(0); s->top=s->base+s->stacksize; s->stacksize += STACKINCREMENT; } //去掉 *(s->stop)=e; *(s->top++)=e;//改为*(s->top++) = e; } bool pop(sqstack *s, ElemType *e) { if(s->top == s->base)//将=改为== return ERROR; *e = *--(s->top); return OK; } int stackLen(sqstack &s)//s前要加&改为引用 { return ((s.base-s.top)/sizeof(sqstack)); //计算栈的长度 } bool match(char e,char c) { if((e=='(') && (c=')') )//少了右括号 return OK; if((e=='[') && (c==']'))//少了右括号 return OK; return ERROR; } int main() { sqstack s; char e,c; initstack(&s); printf("请输入符号,最后以\"#\"结束\n"); scanf("%c",&c); while(c!='#') { if(!stackLen(s)) push(&s,c); else { pop(&s,&e); if(!match(e,c)) { push(&s,e); push(&s,c); } } scanf("%c",&c); } if(!stackLen(s)) printf("the branchets are matched\n"); else printf("the branchets are matched\n"); // getch(); return 0; }