找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1338|回复: 8

[求助]括号匹配问题的求助

[复制链接]
发表于 2009-11-3 03:50:31 | 显示全部楼层 |阅读模式 IP:江苏扬州
程序如下:
#include<iostream.h>
#include<string.h>
class stack
{
public:
stack(){}
stack(int sz):size(sz),n(0),k(0),top(0),bottom(0)
{
s=new char[100];
s[99]='\0';
m=new char[100];
}
void push(char c)
{
if(top-bottom>=size)
cout<<"full!"<<endl;
else
{
s[top]=c;
top=top+1;
}
}

char pop()
{
if(top<=bottom)
cout<<"empty!"<<endl;
else
{
return s[top-1];
top=top-1;
}
}
void pick()
{
for(int i=0;i<top;i++)
{
if(s[i]=='{'&&s[i]=='('&&s[i]=='['&&s[i]==']'&&s[i]==')'&&s[i]=='}')
{
m[n]=s[i];
n=n+1;
}
}
}
bool fb(char a,char b)
{
if((a=='('&&b==')')||(a=='['&&b==']')||(a=='{'&&b=='}'))
return true;
else
return false;
}
bool pipei()
{
if(n%2==0)
{
for(int j=0;j<n;j++)
{
if(fb(m[j],m[n-j-1]))
{
k=k+1;
}
}
if(k==n/2)
{
return true;
}
else
return false;
}
else
return false;
}
void disp()
{
for(int i=bottom;i<top;i++)
{
cout<<s[i];
}
cout<<endl;
}
private:
int bottom;
int top;
int n;
int k;
int size;
char *s;
char *m;
};
void main()
{
stack a(10);
a.push(')');
a.push('[');
a.push('(');
a.push(')');
a.push(']');
a.push('}');
a.disp();
if(a.pipei())
{
cout<<"right!"<<endl;
}
else
{
cout<<"wrong!"<<endl;
}
}
这样匹配应该显示wrong 为什么是right啊
肯定是哪个地方错了 我没找到 大家按照我的这个思路 帮我找找错误吧
谢谢大家的指教了
发表于 2009-11-3 03:50:32 | 显示全部楼层 IP:江苏扬州
怎么没人解答啊
是不是问题太幼稚了啊
帮帮小弟吧
回复

使用道具 举报

发表于 2009-11-3 03:50:32 | 显示全部楼层 IP:江苏扬州
没编译器。。
回复

使用道具 举报

发表于 2009-11-3 03:50:34 | 显示全部楼层 IP:江苏扬州
以下是引用heliujin在2006-7-16 21:55:06的发言:

程序如下:
#include<iostream.h>
#include<string.h>
class stack
{
public:
stack(){}
stack(int sz):n(0),k(0),top(0),bottom(0)
{
if(sz<=0||sz>100)size=100;
else size=sz;
s=new char[size];
s[size-1]='\0';
m=new char[size];
}
void push(char c)
{
if(top-bottom>=size)
cout<<"full!"<<endl;
else
{
s[top]=c;
top=top+1;
}
}

char pop()
{
if(top<=bottom)
cout<<"empty!"<<endl;
else
{
return s[top-1];
top=top-1;
}
}
void pick()
{
for(int i=0;i<top;i++)
{
if(s[i]=='{' || s[i]=='(' || s[i]=='[' || s[i]==']' || s[i]==')' || s[i]=='}')
{
m[n]=s[i];
n++;
}
}
}
bool fb(char a,char b)
{
return (a=='(' && b==')') || (a=='[' && b==']') || (a=='{' && b=='}');
}
bool pipei()
{
if(n%2==0)
{
for(int j=0;j<=n/2;j++)
if(fb(m[j],m[n-j-1]))k++;
if(k==n/2)return true;
}
return false;
}
void disp()
{
for(int i=bottom;i<top;i++)
{
cout<<s[i];
}
cout<<endl;
}
private:
int bottom;
int top;
int n;
int k;
int size;
char *s;
char *m;
};
void main()
{
stack a(10);
a.push(')');
a.push('[');
a.push('(');
a.push(')');
a.push(']');
a.push('}');
a.pick();
a.disp();
if(a.pipei())
{
cout<<"right!"<<endl;
}
else
{
cout<<"wrong!"<<endl;
}
}
这样匹配应该显示wrong 为什么是right啊
肯定是哪个地方错了 我没找到 大家按照我的这个思路 帮我找找错误吧
谢谢大家的指教了
回复

使用道具 举报

发表于 2009-11-3 03:50:34 | 显示全部楼层 IP:江苏扬州
你有几个地方有点问题,最低级的错误在于,你的pick函数都没调用,n一直为0
回复

使用道具 举报

发表于 2009-11-3 03:50:35 | 显示全部楼层 IP:江苏扬州
不好意思 我会注意的
我加了PICK函数后怎么还是运行不出想要的结果啊?
大家指点指点小弟吧 谢谢大家了
回复

使用道具 举报

发表于 2009-11-3 03:50:37 | 显示全部楼层 IP:江苏扬州
你没运行我在4楼写的程序吗?
回复

使用道具 举报

发表于 2009-11-3 03:50:38 | 显示全部楼层 IP:江苏扬州
哦 不好意思
我运行了 谢谢四楼的了
回复

使用道具 举报

发表于 2009-11-3 03:50:39 | 显示全部楼层 IP:江苏扬州
给你一个类似的例子
体现了原地踏步的思想的例子(C)
#include<stdio.h>
int main()
{
int n=0;
char c='0';
while( c = getchar() )
{
if( c == '\n' )
break;
else if( c == '{' )
n++;
else if( c == '}' )
n--;
else
;
}
if( 0 == n )
printf("OK\n");
else
printf("ERROR\n");
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 15:20 , Processed in 0.281470 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表