圆括号匹配c语言代码,求救!!一道关于表达式圆括号匹配的经典题目
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
#include
#include
struct Listack{
char data;
struct Listack*next;};bool GetTop(Listack*&s,char e)
{
if(s->next==NULL){printf("incorrect"); return false;
}
if(e==s->next->data)
return true;
}
void Push(Listack* &s,char e)
{
Listack*p;
p=(Listack*)malloc(sizeof(Listack));
p->data=e;
p->next=s->next;
s->next=p;
}
bool StackEmpty(Listack*s)
{
return(s->next==NULL);
}
bool Pop(Listack*&s,char e)
{
Listack * p;
if(s->next==NULL)
{printf("incorrect"); return false;
}
p=s->next;
e=p->data;
s->next=p->next;
free(p);
return true;
}
void Match(char exp[])
{
Listack* st;
st=(Listack*)malloc(sizeof(Listack));
st->next=NULL;
char ch='(';int i;
for(i=0;exp[i]=='('||')';i++)
{if(exp[i]=='(')
Push(st,exp[i]);
else if(exp[i]==')')
{
if(GetTop(st,ch))
Pop(st,ch);
}
}
if(!StackEmpty(st))
printf("incorrect");
else printf("correct");
}int main()
{
char str[100];
printf("please input:");
gets(str);
Match(str);
}
總結
以上是生活随笔為你收集整理的圆括号匹配c语言代码,求救!!一道关于表达式圆括号匹配的经典题目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据集制作_轻松学Pytorch自定义数
- 下一篇: 安装mysql 环境变量_win10系统