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

来源:互联网 发布:e盘数据恢复 编辑:程序博客网 时间:2024/05/04 20:08
  1. /*  
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院  
  3. * All rights reserved.  
  4. * 文件名称:wu.cpp  
  5. * 作    者:武昊  
  6. * 完成日期:2016年9月29日  
  7. * 版 本 号:v1.0   
  8. *问题描述:假设表达式中允许三种括号:圆括号、方括号和大括号。编写一个算法,判断表达式中的各种左括号是否与右括号匹配。   
  9.           例如,输入2+(3+4)*[2+{[3]}]-8,输出匹配正确;输入2+(3+4*[2)+{[3]}-8,输出匹配错误。  
  10. *输入描述:输入表达式  
  11. *程序输出:输出判断结果  
  12. */    
main.cpp
  1. #include "sqstack.h"  
  2. int main()  
  3. {  
  4.     char c;  
  5.     char st[50];  
  6.     int d=1, i;  
  7.     SqStack *s;  
  8.     InitStack(s);  
  9.     printf("请输入表达式:");  
  10.     scanf("%s", st);  
  11.     for(i=0; st[i]!='\0'&&d; i++)  
  12.     {  
  13.         switch(st[i])  
  14.         {  
  15.         case'(':  
  16.         case'[':  
  17.         case'{':  
  18.             Push(s, st[i]);  
  19.             break;  
  20.         case')':  
  21.             Pop(s, c);  
  22.             if(c!='(') d=0;  
  23.             break;  
  24.         case']':  
  25.             Pop(s, c);  
  26.             if(c!='[') d=0;  
  27.             break;  
  28.         case'}':  
  29.             Pop(s,c);  
  30.             if(c!='{') d=0;  
  31.             break;  
  32.         }  
  33.     }  
  34.     if(StackEmpty(s)&&d==1)  
  35.         printf("配对正确!!\n");  
  36.     else  
  37.         printf("配对错误!!\n");  
  38.     return 0;  
  39. }  
sqstack.h
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #define MaxSize 100  
  4. typedef char ElemType;  
  5. typedef struct  
  6. {  
  7.     ElemType data[MaxSize];  
  8.     int top;                //栈指针  
  9. } SqStack;                  //顺序栈类型定义  
  10.   
  11. void InitStack(SqStack *&s);    //初始化栈  
  12. void DestroyStack(SqStack *&s);  //销毁栈  
  13. bool StackEmpty(SqStack *s);     //栈是否为空  
  14. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  15. bool Push(SqStack *&s,ElemType e); //入栈  
  16. bool Pop(SqStack *&s,ElemType &e); //出栈  
  17. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  18. void DispStack(SqStack *s);  //输出栈  
sqstack.cpp
  1. #include "sqstack.h"  
  2. void InitStack(SqStack *&s)//初始化栈  
  3. {  
  4.     s=(SqStack *)malloc(sizeof(SqStack));  
  5.     s->top=-1;  
  6. }  
  7. void DestroyStack(SqStack *&s)//销毁栈  
  8. {  
  9.     free(s);  
  10. }  
  11. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  12. {  
  13.     return(s->top+1);  
  14. }  
  15. bool StackEmpty(SqStack *s)//判断是否为空栈  
  16. {  
  17.     return(s->top==-1);  
  18. }  
  19. bool Push(SqStack *&s,ElemType e)  
  20. {  
  21.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  22.         return false;  
  23.     s->top++;  
  24.     s->data[s->top]=e;  
  25.     return true;  
  26. }  
  27. bool Pop(SqStack *&s,ElemType &e)  
  28. {  
  29.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  30.         return false;  
  31.     e=s->data[s->top];  
  32.     s->top--;  
  33.     return true;  
  34. }  
  35. bool GetTop(SqStack *s,ElemType &e)  
  36. {  
  37.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  38.         return false;  
  39.     e=s->data[s->top];  
  40.     return true;  
  41. }  
  42.   
  43. void DispStack(SqStack *s)  //输出栈  
  44. {  
  45.     int i;  
  46.     for (i=s->top;i>=0;i--)  
  47.         printf("%c ",s->data[i]);  
  48.     printf("\n");  
  49. }  
运行结果:


知识点总结:

       顺序栈算法库更加多元化,要熟练掌握。



0 0
原创粉丝点击