这个算法我运行不了,不知道怎么找错!

slykaluo 2011-11-08 05:58:17
#define TRUE 1
#define FLASE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status


#include<stdio.h>
#include"Status.h"
#include<stdlib.h>
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
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)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack S,SElemType &e){
if(S.top==S.base)return ERROR;
e=*(S.top-1);
return OK;
}
Status Push(SqStack &S,SElemType e){
if(S.top-S.base>=S.stacksize){
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e){
if(S.top==S.base)return ERROR;
e=*--S.top;
return OK;
}
Status StackEmpty(SqStack S){
if(S.top==S.base)return TRUE;
return FLASE;
}



#include"Stack.cpp"
#include"Status.h"
#include<stdio.h>
Status MatchBracket(char *ch)
{ SqStack S; InitStack(S);
while (ch[i]!='\0'){
if (ch[i]=='['||ch[i]=='(') Push(S,ch[i]);
else if (ch[i]==']'||ch[i]==')')
if (GetTop(S,ch1))
if ((ch1=='['&&ch[i]==']')||(ch1=='('&&ch[i]==')'))
Pop(S,ch1);
else return ERROR;
else return ERROR;
i++; }
if (!StackEmpty(S)) return ERROR;
return OK;
}
void main()
{
char a[],i=0,ch;
printf("请输入算式:\n");
while((ch=getchar())!='\n')
{
a[i++]=ch;
}
MatchBracket(a);
}





...全文
222 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序员小迷 2012-02-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 slykaluo 的回复:]

我把第一段的代码当做头文件了引用 7 楼 song19891121 的回复:
缺少 Status.h 头文件
[/Quote]

#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;
错了
这加什么分号啊。。。
slykaluo 2011-11-08
  • 打赏
  • 举报
回复
我把第一段的代码当做头文件了[Quote=引用 7 楼 song19891121 的回复:]
缺少 Status.h 头文件
[/Quote]
宋志辉 2011-11-08
  • 打赏
  • 举报
回复
缺少 Status.h 头文件
slykaluo 2011-11-08
  • 打赏
  • 举报
回复
也不是 加了两个分号后错误更多[Quote=引用 5 楼 super_miker 的回复:]
typedef int Status
typedef char SElemType

这两行缺分号
[/Quote]
super_miker 2011-11-08
  • 打赏
  • 举报
回复
typedef int Status
typedef char SElemType

这两行缺分号
slykaluo 2011-11-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xiangyu_lg 的回复:]
while (ch[i]!='\0') 这个是你在Status MatchBracket(char *ch)中的循环终止条件,同时你传递的数组MatchBracket(a);中的a[]不是以'\0'结束的。
[/Quote]我在这里修改后还是一样
void main()
{
char a[],i=0,ch;
printf("请输入算式:\n");
while((ch=getchar())!='\n')
{
a[i++]=ch;
}
a[i]='\0';MatchBracket(a);
}
运行时出现两错报错
c:\documents and settings\user\vc98\include\stdlib.h(37) : error C2144: syntax error : missing ';' before type 'int'
c:\documents and settings\user\vc98\include\stdlib.h(37) : fatal error C1004: unexpected end of file found

xiangyu_lg 2011-11-08
  • 打赏
  • 举报
回复
while (ch[i]!='\0') 这个是你在Status MatchBracket(char *ch)中的循环终止条件,同时你传递的数组MatchBracket(a);中的a[]不是以'\0'结束的。
slykaluo 2011-11-08
  • 打赏
  • 举报
回复
报错[Quote=引用 1 楼 xiangyu_lg 的回复:]
具体是怎么运行不了?
[/Quote]
xiangyu_lg 2011-11-08
  • 打赏
  • 举报
回复
具体是怎么运行不了?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧