括号匹配

风玉 2010-11-16 12:26:47
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char data[100];
int top;
}zy;
enum
{
match=1,
notmatch=2,
leftmore=3,
rightmore=4,
};
void initial(zy *stack)
{
stack->top=-1;
}
int isempty(zy *stack)
{
return stack->top==-1;
}
void push(zy *stack,char ch)
{
stack->data[++stack->top]=ch;
}
char pop(zy *stack)
{
return stack->data[stack->top--];
}

int ispair(char cleft,char cright)
{
return cleft=='('&& cright==')'||cleft=='['&& cright==']'||cleft=='{'&& cright=='}';
}
int ismatch(char str[])
{
char ch ,temp;
int i=0;
zy stack;
initial(&stack);
while((ch=str[i++])!='/0')
{
switch(ch)
{
case'(':
case'[':
case'{':
push(&stack,ch);
break;
case')':
case']':
case'}':
if (isempty(&stack))
return rightmore;
temp=pop(&stack);
if(!ispair(temp,ch))
return notmatch;
break;
default:
break;
}
}
if(!isempty(&stack))
return leftmore;
return match;
}
void main()
{
int a;
char str[50];
printf("please input: ");
gets(str);
a=ismatch(str);
if(a==1)
printf("match");
if(a==2)
printf("not match");
if(a==3)
printf("left more");
if(a==4)
printf("right more");
}






if(!isempty(&stack))
return leftmore;
return match;这一段为什么不起作用啊
...全文
92 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jmj49314 2010-11-17
  • 打赏
  • 举报
回复
while((ch=str[i++])!='/0')
应为'\0'
if(!ispair(temp,ch))
return notmatch;
这一段不起作用
你说的那一段有显示结果了
無_1024 2010-11-17
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char data[100];
int top;
}zy;
enum
{
match=1,
notmatch=2,
leftmore=3,
rightmore=4,
};
void initial(zy *stack)
{
stack->top=-1;
}
int isempty(zy *stack)
{
return stack->top==-1;
}
void push(zy *stack,char ch)
{
stack->data[++stack->top]=ch;
}
char pop(zy *stack)
{
return stack->data[stack->top--];
}

int ispair(char cleft,char cright)
{
return cleft=='('&& cright==')'||cleft=='['&& cright==']'||cleft=='{'&& cright=='}';
}
int ismatch(char str[])
{
char ch ,temp;
int i=0;
zy stack;
initial(&stack);
while((ch=str[i++])!='\0')
{
switch(ch)
{
case'(':
case'[':
case'{':
push(&stack,ch);
break;
case')':
case']':
case'}':
if (isempty(&stack))
return rightmore;
temp=pop(&stack);
if(!ispair(temp,ch))
return notmatch;
break;
default:
break;
}
}
if(!isempty(&stack))
return leftmore;
return match;
}
void main()
{
int a;
char str[50];
printf("please input: ");
gets(str);
a=ismatch(str);
if(a==1)
printf("match\n");
if(a==2)
printf("not match\n");
if(a==3)
printf("left more\n");
if(a==4)
printf("right more\n");
}


赵4老师 2010-11-17
  • 打赏
  • 举报
回复
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
whlie (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。
bluejays 2010-11-16
  • 打赏
  • 举报
回复
return (cleft=='('&& cright==')')||(cleft=='['&& cright==']')||(cleft=='{'&& cright=='}');
while((ch=str[i++])!='\0')
运行结果貌似正确了
bluejays 2010-11-16
  • 打赏
  • 举报
回复
return cleft=='('&& cright==')'||cleft=='['&& cright==']'||cleft=='{'&& cright=='}';
LZ对这里的优先级很有把握吗?加上几个括号吧

while((ch=str[i++])!='/0')
应该是\0吧?
风玉 2010-11-16
  • 打赏
  • 举报
回复
结果还是有问题呀

33,311

社区成员

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

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