数据结构实验之栈四:括号匹配
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。
输入
输入数据有多组,处理到文件结束。
输出
如果匹配就输出“yes”,不匹配输出“no”
示例输入
sin(20+10){[}]
示例输出
yesno
思路都写代码里了。。。。
#include //STL栈之括号匹配 #include #include #include #include #include #include using namespace std; int main() { char c[60],t; int i; while(gets(c)!=NULL) { stack s; bool valid=true; //先假定字符串合法 for(i=0;i
当我们学习了STL这么强大的工具之后 再去敲一个栈。。什么感觉?。。。
#include
#include
#include
#include
#include
#include
const int MAX=9999; using namespace std; typedef struct stack { int top; char a[MAX]; }*S; int empty(S p) { if(p->top==-1) return 1; else return 0; } void push(S p,char x) { if(p->top
a[++(p->top)]=x; } void pop(S p) { if(!empty(p)) p->top--; } char top(S p) { if(!empty(p)) return p->a[p->top]; } int main() { char c[55]; while(gets(c)!=NULL) { stack s; s.top=-1; int len=strlen(c); bool ok=1; for(int i=0;i