热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

c语言括号匹配题目,求救!!一道关于表达式圆括号匹配的经典题目

该楼层疑似违规已被系统折叠隐藏此楼查看此楼#include#includestructListack{chardata;structListack*next;};boolGetTo

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#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);

}



推荐阅读
author-avatar
wen-1225
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有