括号匹配问题
发布网友
发布时间:2022-05-08 03:51
我来回答
共2个回答
热心网友
时间:2023-11-25 05:22
#include<stdio.h>
#define MAX 100
int match(char *str)
{
char stack[MAX],*p=stack;
while(*str)
{
switch(*str)
{
case '(':
{
*p++=*str;
break;
}
case ')':
{
if(*--p!='(')
return 0;
break;
}
case '[':
{
*p++=*str;
break;
}
case ']':
{
if(*--p!='[')
return 0;
break;
}
case '{':
{
*p++=*str;
break;
}
case '}':
{
if(*--p!='{')
return 0;
break;
}
}
str++;
}
if(stack==p)
return 1;
else
return 0;
}
int main()
{
char str[MAX];
gets(str);
if(match(str))
{
printf("match\n");
}
else
{
printf("not match\n");
}
return 0;
}
热心网友
时间:2023-11-25 05:22
#include<stdio.h>
#include<malloc.h>
#include<windows.h>
struct Stack{char ch;struct Stack *next;};
#define LEN sizeof(struct Stack)
int n;char x;
struct Stack *creat()
{struct Stack *S;S=0;return S;}
struct Stack * Push(struct Stack *S,char c)
{struct Stack *p;
p=(struct Stack *)malloc(LEN);
p->ch=c;p->next=S;S=p;return S;
}
struct Stack * Pop(struct Stack *S,char c)
{ struct Stack *p;
if(S==0)printf("error!\n");
else p=S;S=S->next;delete p;return S;}
char Gettop(struct Stack *S)
{char c;if(S==0)c='?';else c=S->ch;return c;}
void Match(struct Stack *S)
{int flag=1;
char c;
printf("如下方式输入括号\n");printf("[](){}#\n");printf("请使用英文的半角符,符号#表示结束输入,最后按enter键进行判断\n");
printf("请先按shift键,后按括号\n");
scanf(" %c",&c);if(c=='#')printf("没有输入括号,请退出后重新输入括号\n");
else{ while(c!='#'){
switch(c)
{case '(':if(flag==0)flag=0;else {S=Push(S,c);flag=1;}break;
case '[':if(flag==0)flag=0;else {S=Push(S,c);flag=1;}break;
case '{':if(flag==0)flag=0;else {S=Push(S,c);flag=1;}break;
case ')':x=Gettop(S);if(x=='('){S=Pop(S,x);flag=1;}else flag=0;break;
case ']':x=Gettop(S);if(x=='['){S=Pop(S,x);flag=1;}else flag=0;break;
case '}':x=Gettop(S);if(x=='{'){S=Pop(S,x);flag=1;}else flag=0;break;
default:printf("所输入非英文半角括号,请重新输入正确格式的括号:如[、]、(、)、{、}\n");
}
scanf(" %c",&c);}printf("经判断:\n");
if(S==0&&flag==1)printf("匹配正确\n");else printf("匹配错误\n");};}
void main()
{struct Stack *S;int i;
do{
S=creat();
Match(S);
printf("\n如需再对括号进行判断,请按1,否则按0结束\n");
scanf(" %d",&i);system("cls");}while(i==1);
}
作者:zhengweiqing
热心网友
时间:2023-11-25 05:22
#include<stdio.h>
#define MAX 100
int match(char *str)
{
char stack[MAX],*p=stack;
while(*str)
{
switch(*str)
{
case '(':
{
*p++=*str;
break;
}
case ')':
{
if(*--p!='(')
return 0;
break;
}
case '[':
{
*p++=*str;
break;
}
case ']':
{
if(*--p!='[')
return 0;
break;
}
case '{':
{
*p++=*str;
break;
}
case '}':
{
if(*--p!='{')
return 0;
break;
}
}
str++;
}
if(stack==p)
return 1;
else
return 0;
}
int main()
{
char str[MAX];
gets(str);
if(match(str))
{
printf("match\n");
}
else
{
printf("not match\n");
}
return 0;
}
热心网友
时间:2023-11-25 05:23
#include<stdio.h>
#include<malloc.h>
#include<windows.h>
struct Stack{char ch;struct Stack *next;};
#define LEN sizeof(struct Stack)
int n;char x;
struct Stack *creat()
{struct Stack *S;S=0;return S;}
struct Stack * Push(struct Stack *S,char c)
{struct Stack *p;
p=(struct Stack *)malloc(LEN);
p->ch=c;p->next=S;S=p;return S;
}
struct Stack * Pop(struct Stack *S,char c)
{ struct Stack *p;
if(S==0)printf("error!\n");
else p=S;S=S->next;delete p;return S;}
char Gettop(struct Stack *S)
{char c;if(S==0)c='?';else c=S->ch;return c;}
void Match(struct Stack *S)
{int flag=1;
char c;
printf("如下方式输入括号\n");printf("[](){}#\n");printf("请使用英文的半角符,符号#表示结束输入,最后按enter键进行判断\n");
printf("请先按shift键,后按括号\n");
scanf(" %c",&c);if(c=='#')printf("没有输入括号,请退出后重新输入括号\n");
else{ while(c!='#'){
switch(c)
{case '(':if(flag==0)flag=0;else {S=Push(S,c);flag=1;}break;
case '[':if(flag==0)flag=0;else {S=Push(S,c);flag=1;}break;
case '{':if(flag==0)flag=0;else {S=Push(S,c);flag=1;}break;
case ')':x=Gettop(S);if(x=='('){S=Pop(S,x);flag=1;}else flag=0;break;
case ']':x=Gettop(S);if(x=='['){S=Pop(S,x);flag=1;}else flag=0;break;
case '}':x=Gettop(S);if(x=='{'){S=Pop(S,x);flag=1;}else flag=0;break;
default:printf("所输入非英文半角括号,请重新输入正确格式的括号:如[、]、(、)、{、}\n");
}
scanf(" %c",&c);}printf("经判断:\n");
if(S==0&&flag==1)printf("匹配正确\n");else printf("匹配错误\n");};}
void main()
{struct Stack *S;int i;
do{
S=creat();
Match(S);
printf("\n如需再对括号进行判断,请按1,否则按0结束\n");
scanf(" %d",&i);system("cls");}while(i==1);
}
作者:zhengweiqing