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



#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
zbqf109
Rank: 1
等 级:新手上路
帖 子:289
专家分:0
注 册:2006-12-31
收藏
得分:0 
简单说几点
1、很多函数没有返回类型
    我不知道你用什么编译器,反正我用的DEVCPP是报错的。
2、尤其main的返回类型一定要是int
     main函数最后返回0就可以了
3、错误的地方是gettop(sqstack *s,char e)
       这个函数的第二个参数要改成一个引用即gettop(sqstack *s,char& e)
4、能用const的地方尽量使用const
2007-11-29 23:20
zbqf109
Rank: 1
等 级:新手上路
帖 子:289
专家分:0
注 册:2006-12-31
收藏
得分:0 
这是改过之后的程序:
前面几行我加了几个缩进符,后面不管了

#define STATUS int
#define OK 1
#define OVERFLOW -2
#define ERROR 0

#include <stdlib.h>
#include <stdio.h>  

#define STACK_INIT_SIZE 100
#define STACKINCREMENT  100

typedef struct {
        char *base;
        char *top;
        int stacksize;
}sqstack;

STATUS 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;
}
STATUS gettop(sqstack *s,char& e){
       if(s->top==s->base)
                           return ERROR;
       e=*(s->top-1);
       return OK;
}
STATUS 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;
}
STATUS pop (sqstack *s ,char *e)
{if(s->top==s->base) return ERROR;
e=--(s->top);
return OK;
}
void 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');
}
void choice(sqstack *s)
{if(s->top==s->base)
printf("correct");
else printf("wrong");
}
int main()
{sqstack s1;
printf("when you are input 0,the input of sign the  is over:\n");
initstack(&s1);
input(&s1);
choice(&s1);
system("pause");
return 0;
}
2007-11-29 23:22
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.016802 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved