| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 934 人关注过本帖
标题:括号匹配程序怎么总是不能出现正确结果
取消只看楼主 加入收藏
xiaofkg
Rank: 1
等 级:新手上路
威 望:1
帖 子:77
专家分:0
注 册:2007-9-17
收藏
 问题点数:0 回复次数:1 
括号匹配程序怎么总是不能出现正确结果
括号匹配程序怎么总是不能出现正确结果,有了堆栈的函数,高手看看,谢谢了。。



#define OK 1
#define OVERFLOW -2
#define ERROR 0
#include <stdlib.h>   
#define STACK_INIT_SIZE 100
#define STACKINCREMENT  100
typedef struct {
 char *base;
 char *top;
 int stacksize;
 }sqstack;
initstack(sqstack *s){
 s->base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
if(!s->base)exit(OVERFLOW);
s->top=s->base;
 s->stacksize=STACK_INIT_SIZE;
 return OK;
 }
gettop(sqstack *s,char e){
 if(s->top==s->base) return ERROR;
 e=*(s->top-1);
 return OK;
}
push(sqstack *s,char e){
if(s->top-s->base>=s->stacksize){
 s->base=(char*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(char));
 if(!s->base) exit(OVERFLOW);
 s->top=s->base+s->stacksize;
 s->stacksize+=STACKINCREMENT;
}
*(s->top++)=e;
return OK;
}
pop (sqstack *s ,char *e)
{if(s->top==s->base) return ERROR;
e=--(s->top);
return OK;
}
input (sqstack *s)
{char n,e;
do
{
scanf("%c",&n);
switch (n)
{case '(':push(s,n);break;
case '[':push(s,n);break;
case ']':gettop(s,e);
          if(e=='[')
           pop(s,&e);
          else push(s,n);
         break;
case ')':gettop(s,e);
         if(e=='(')
         pop(s,&e);
         else push(s,n);
         break;
}
}while(n!='0');
}
choice(sqstack *s)
{if(s->top==s->base)
printf("correct");
else printf("wrong");
}
main()
{sqstack s1;
printf("when you are input 0,the input of sign the  is over:\n");
initstack(&s1);
input(&s1);
choice(&s1);
getch();
}
搜索更多相关主题的帖子: 括号 结果 
2007-11-29 21:43
xiaofkg
Rank: 1
等 级:新手上路
威 望:1
帖 子:77
专家分:0
注 册:2007-9-17
收藏
得分:0 
感谢zbqf109会员,不过这是C程序,用DEV编译的确是要出错的。而且你改过之后的程序,链接出错。
在C语言中默认函数的返回值是INT
我修改了我的程序,这是在C编译器中执行出真确结果的程序源代码。与你分享,再次谢谢了。
#define OK 1
#define OVERFLOW -2
#define ERROR 0
#include <stdlib.h>   
#define STACK_INIT_SIZE 100
#define STACKINCREMENT  100
typedef struct {
char *base;
char *top;
int stacksize;
}sqstack;
initstack(sqstack *s){
s->base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
if(!s->base)exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return OK;
}
gettop(sqstack *s,char m){
if(s->top==s->base) return ERROR;
m=*(s->top-1);
return m;
}
push(sqstack *s,char e){
if(s->top-s->base>=s->stacksize){
s->base=(char*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(char));
if(!s->base) exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*(s->top++)=e;
return OK;
}
pop (sqstack *s ,char *k)
{if(s->top==s->base) return ERROR;
k=--(s->top);
return OK;
}
input (sqstack *s)
{char n,g;
do
{
scanf("%c",&n);
switch (n)
{case '(':push(s,n);break;
case '[':push(s,n);break;
case ']':

          if(gettop(s,g)=='['&&!empty(s))
           pop(s,&g);
          else push(s,n);
         break;
case ')':
         if(gettop(s,g)=='('&&!empty(s))
         pop(s,&g);
         else push(s,n);
         break;
}
}while(n!='\n');
}
choice(sqstack *s)
{if(s->top==s->base)
printf("correct");
else printf("wrong");
}
empty(sqstack *s)
{if (s->top==s->base)
  return OK;
  else
  return ERROR;
}

main()
{sqstack s1;
printf("when you are input enter,the input of sign the  is over:\n");
initstack(&s1);
input(&s1);
choice(&s1);
getch();
}
2007-11-30 09:22
快速回复:括号匹配程序怎么总是不能出现正确结果
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058570 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved