第五周 项目3 --括号的匹配
来源:互联网 发布:isomap算法介绍 编辑:程序博客网 时间:2024/04/27 17:54
/* Copyright (c)2016,烟台大学计算机与控制工程学院 All rights reserved. 文件名称:第5周项目3 - 括号的匹配.cpp 作 者:滕健 完成日期:2016年9月29日 问题描述:假设表达式中允许三种括号:圆括号、方括号和大括号。编写一个算法,判断表达式中的各种左括号是否与右括号匹配。 例如,输入2+(3+4)*[2+{[3]}]-8,输出匹配正确;输入2+(3+4*[2)+{[3]}-8,输出匹配错误。 输入描述:输入表达式 程序输出:输出判断结果 */ //(1)头文件:#include<stdio.h> #include<malloc.h> typedef char ElemType; typedef struct linknode { ElemType data; //数据域 struct linknode *next; //指针域 } LiStack; //链栈类型定义 void InitStack(LiStack *&s); //初始化栈 void DestroyStack(LiStack *&s); //销毁栈 int StackLength(LiStack *s); //返回栈长度 bool StackEmpty(LiStack *s); //判断栈是否为空 void Push(LiStack *&s,ElemType e); //入栈 bool Pop(LiStack *&s,ElemType &e); //出栈 bool GetTop(LiStack *s,ElemType &e); //取栈顶元素 void DispStack(LiStack *s); //输出栈中元素 //(2)源文件:#include"head.h" void InitStack(LiStack *&s) { s=(LiStack *)malloc(sizeof(LiStack)); s->next=NULL; } void DestroyStack(LiStack *&s) { free(s); } int StackLength(LiStack *s) { int i=0; s=s->next; while(s!=NULL) { i++; s=s->next; } return i; } bool StackEmpty(LiStack *s) { if(s->next==NULL) { return true; } else return false; } void Push(LiStack *&s,ElemType e) { LiStack *l; l=(LiStack *)malloc(sizeof(LiStack)); l->data=e; l->next=s->next; s->next=l; } bool Pop(LiStack *&s,ElemType &e) { LiStack *l; l=s; if(l->next==NULL) { return false; } l=s->next; e=l->data; s->next=l->next; free(l); return true; } bool GetTop(LiStack *s,ElemType &e) { if(s->next==NULL) { return false; } e=s->next->data; return true; } void DispStack(LiStack *s) { LiStack *l; l=s; l=l->next; while(l!=NULL) { printf("%c",l->data); l=l->next; } printf("\n"); } //(3)main函数:#include "sqstack.h" bool isMatch(char *st) { int d=1, i; char c; SqStack *s; InitStack(s); for(i=0; st[i]!='\0'&&d; i++) { switch(st[i]) { case'(': case'[': case'{': Push(s, st[i]); break; case')': Pop(s, c); if(c!='(') d=0; break; case']': Pop(s, c); if(c!='[') d=0; break; case'}': Pop(s,c); if(c!='{') d=0; break; } } if(StackEmpty(s)&&d==1) return true; else return false; } int main() { char st[50]; printf("请输入表达式:"); scanf("%s", st); if(isMatch(st)) printf("配对正确!!\n"); else printf("配对错误!!\n"); return 0; }
运行结果:
0 0
- 第五周项目3--括号的匹配
- 第五周 项目3-括号的匹配
- 第五周项目3 -括号的匹配
- 第五周 项目3 - 括号的匹配
- 第五周项目3-括号的匹配
- 第五周项目3-括号的匹配
- 第五周项目3-括号的匹配
- 第五周 项目3-括号的匹配
- 第五周项目3-括号的匹配
- 第五周--项目3-括号的匹配
- 第五周 项目3 --括号的匹配
- 第五周项目3-括号的匹配
- 第五周项目3-括号的匹配
- 【第五周项目3-括号的匹配】
- 第五周项目3--括号的匹配
- 第五周项目3-括号的匹配
- 第五周项目3-括号的匹配
- 第五周--项目3括号的匹配 .
- 第五周项目1—建立顺序栈算法库
- 第5周项目1 建立顺序栈算法库
- 多例设计模式
- 发工资
- 第四周 项目三-单链表的应用(三)
- 第五周 项目3 --括号的匹配
- Android之AlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作
- C++中的static关键字的总结
- 第五周项目4 —— 数制转换
- Linux入门:常用命令:cd - 返回上一次目录
- IOS OpenGLES2.0 入门04 加载纹理
- 第五周--括号的匹配
- 第五周-raptor 简单数列求和
- 各种颜色的透明色