第五周项目3
来源:互联网 发布:windows下安装mysql 编辑:程序博客网 时间:2024/05/14 10:41
/*
*Copyright (c)2017,烟台大学计算机与控制工程学院
*All rights reservrd.
*文件名称 :test.cpp
*作者:杜昕晔
*完成时间:2017年10月09日
*版本号:v1.0
*问题描述:判断左右括号是否匹配
问题及代码:
sqstack.h函数:
#ifndef SQSTACK_H_INCLUDED#define SQSTACK_H_INCLUDED#define MaxSize 100typedef char ElemType;typedef struct{ ElemType data[MaxSize]; int top; //栈指针} SqStack; //顺序栈类型定义void InitStack(SqStack *&s); //初始化栈void DestroyStack(SqStack *&s); //销毁栈bool StackEmpty(SqStack *s); //栈是否为空int StackLength(SqStack *s); //返回栈中元素个数——栈长度bool Push(SqStack *&s,ElemType e); //入栈bool Pop(SqStack *&s,ElemType &e); //出栈bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素void DispStack(SqStack *s); //输出栈#endif // SQSTACK_H_INCLUDEDsqstack.cpp函数:
#include <stdio.h>#include <malloc.h>#include "sqstack.h"void InitStack(SqStack *&s){ s=(SqStack *)malloc(sizeof(SqStack)); s->top=-1;}void DestroyStack(SqStack *&s){ free(s);}int StackLength(SqStack *s) //返回栈中元素个数——栈长度{ return(s->top+1);}bool StackEmpty(SqStack *s){ return(s->top==-1);}bool Push(SqStack *&s,ElemType e){ if (s->top==MaxSize-1) //栈满的情况,即栈上溢出 return false; s->top++; s->data[s->top]=e; return true;}bool Pop(SqStack *&s,ElemType &e){ if (s->top==-1) //栈为空的情况,即栈下溢出 return false; e=s->data[s->top]; s->top--; return true;}bool GetTop(SqStack *s,ElemType &e){ if (s->top==-1) //栈为空的情况,即栈下溢出 return false; e=s->data[s->top]; return true;}void DispStack(SqStack *s) //输出栈{ int i; for (i=s->top;i>=0;i--) printf("%c ",s->data[i]); printf("\n");}
main.cpp函数:
#include <stdio.h>#include "sqstack.h"int main(){ char c; char st[50]; int d=1, i; SqStack *s; InitStack(s); printf("请输入表达式:"); scanf("%s", st); 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) 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
- spring MVC 整合Swagger
- 控制器添加悬浮窗
- wifi-bt常见问题总结
- 数据库复习--学生课业数据库设计
- 欢迎使用CSDN-markdown编辑器
- 第五周项目3
- Oracle学习笔记
- 3S基础知识:基于MapX的多字段专题饼图的设计
- DUBBO+SPRINGMVC+MYBATIS+EHCACHE+REDIS J2EE分布式架构
- UBUNTU 10.04环境下交叉编译sqlite3.20.1源码
- 使用winsock2.h造成的重定义解决办法
- 多版本Python共存时pip给指定版本的python安装package的方法
- 张量分析初步和矢量恒等式
- Android数据库操作工具类