第五周项目3-括号的匹配

来源:互联网 发布:连云港seo服务 编辑:程序博客网 时间:2024/06/14 15:53
  1. /*  
  2.  * Copyright (c) 2016, 烟台大学计算机与控制工程学院  
  3.  * All rights reserved。  
  4.  * 文件名称 :1.cpp  
  5.  * 作    者 :杨俊杰  
  6.  * 完成日期 :2016年 10月8日  
  7.  * 版 本 号 :v1.0  
  8.  *  
  9.  * 问题描述 :假设表达式中允许三种括号:圆括号、方括号和大括号。编写一个算法,判断表达式中的各种左括号是否与右括号匹配。  
  10.  
  11.  
  12.  * 输入描述 :2+(3+4)*[2+{[3]}]-8;2+(3+4*[2)+{[3]}-8; 
  13.  * 输出描述 :匹配正确;匹配错误。  
  14.  */    

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //1.头文件:sqstack.h,包含定义顺序栈数据结构的代码、宏定义、要实现算法的函数的声明;  
  2.   
  3. #ifndef SQSTACK_H_INCLUDED  
  4. #define SQSTACK_H_INCLUDED  
  5.   
  6. #define MaxSize 100  
  7. typedef char ElemType;  
  8. typedef struct  
  9. {  
  10.     ElemType data[MaxSize];  
  11.     int top;                //栈指针  
  12. } SqStack;                  //顺序栈类型定义  
  13.   
  14. void InitStack(SqStack *&s);    //初始化栈  
  15. void DestroyStack(SqStack *&s);  //销毁栈  
  16. bool StackEmpty(SqStack *s);     //栈是否为空  
  17. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  18. bool Push(SqStack *&s,ElemType e); //入栈  
  19. bool Pop(SqStack *&s,ElemType &e); //出栈  
  20. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  21. void DispStack(SqStack *s);  //输出栈  
  22.   
  23. #endif // SQSTACK_H_INCLUDED  


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //2.源文件:sqstack.cpp,包含实现各种算法的函数的定义  
  2.   
  3. #include <stdio.h>  
  4. #include <malloc.h>  
  5. #include "sqstack.h"  
  6.   
  7. void InitStack(SqStack *&s)  
  8. {  
  9.     s=(SqStack *)malloc(sizeof(SqStack));  
  10.     s->top=-1;  
  11. }  
  12. void DestroyStack(SqStack *&s)  
  13. {  
  14.     free(s);  
  15. }  
  16. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  17. {  
  18.     return(s->top+1);  
  19. }  
  20. bool StackEmpty(SqStack *s)  
  21. {  
  22.     return(s->top==-1);  
  23. }  
  24. bool Push(SqStack *&s,ElemType e)  
  25. {  
  26.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  27.         return false;  
  28.     s->top++;  
  29.     s->data[s->top]=e;  
  30.     return true;  
  31. }  
  32. bool Pop(SqStack *&s,ElemType &e)  
  33. {  
  34.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  35.         return false;  
  36.     e=s->data[s->top];  
  37.     s->top--;  
  38.     return true;  
  39. }  
  40. bool GetTop(SqStack *s,ElemType &e)  
  41. {  
  42.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  43.         return false;  
  44.     e=s->data[s->top];  
  45.     return true;  
  46. }  
  47.   
  48. void DispStack(SqStack *s)  //输出栈  
  49. {  
  50.     int i;  
  51.     for (i=s->top;i>=0;i--)  
  52.         printf("%c ",s->data[i]);  
  53.     printf("\n");  
  54. }  


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include "sqstack.h"  
  3. int main()  
  4. {  
  5.     char c;  
  6.     char st[50];  
  7.     int d=1, i;  
  8.     SqStack *s;  
  9.     InitStack(s);  
  10.     printf("请输入表达式:");  
  11.     scanf("%s", st);  
  12.     for(i=0; st[i]!='\0'&&d; i++)  
  13.     {  
  14.         switch(st[i])  
  15.         {  
  16.         case'(':  
  17.         case'[':  
  18.         case'{':  
  19.             Push(s, st[i]);  
  20.             break;  
  21.         case')':  
  22.             Pop(s, c);  
  23.             if(c!='(') d=0;  
  24.             break;  
  25.         case']':  
  26.             Pop(s, c);  
  27.             if(c!='[') d=0;  
  28.             break;  
  29.         case'}':  
  30.             Pop(s,c);  
  31.             if(c!='{') d=0;  
  32.             break;  
  33.         }  
  34.     }  
  35.     if(StackEmpty(s)&&d==1)  
  36.         printf("配对正确!!\n");  
  37.     else  
  38.         printf("配对错误!!\n");  
  39.     return 0;  
  40. }  

运算结果:

0 0