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

来源:互联网 发布:mac文件夹不能重命名 编辑:程序博客网 时间:2024/04/27 14:11

/*   

* 烟台大学计算机与控制工程学院   

* 作者:王雪松  

* 完成日期:2016年9月29日   

* 问题及代码

* 问题描述:假设表达式中允许三种括号:圆括号、方括号和大括号。编写一个算法,判断表达式中的各种左括号是否与右括号匹配。

* 输入描述:输入2+(3+4)*[2+{[3]}-8,输出匹配正确;输入2+(3+4*[2)+{[3]}-8,输出匹配错误。   

* 程序输出:结果

*/

代码:

  1.头文件:sqstack.h,包含定义顺序栈数据结构的代码、宏定义、要实现算法的函数的声明;

[csharp] view plain copy
  1. #ifndef SQSTACK_H_INCLUDED  
  2. #define SQSTACK_H_INCLUDED  
  3.  
  4. #define MaxSize 100  
  5. typedef char ElemType;  
  6. typedef struct  
  7. {  
  8.     ElemType data[MaxSize];  
  9.     int top;                //栈指针  
  10. } SqStack;                  //顺序栈类型定义  
  11.   
  12. void InitStack(SqStack *&s);    //初始化栈  
  13. void DestroyStack(SqStack *&s);  //销毁栈  
  14. bool StackEmpty(SqStack *s);     //栈是否为空  
  15. int StackLength(SqStack *s);  //返回栈中元素个数——栈长度  
  16. bool Push(SqStack *&s,ElemType e); //入栈  
  17. bool Pop(SqStack *&s,ElemType &e); //出栈  
  18. bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素  
  19. void DispStack(SqStack *s);  //输出栈  
  20.  
  21. #endif // SQSTACK_H_INCLUDED  


  2.源文件:sqstack.cpp,包含实现各种算法的函数的定义

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

   3.main.cpp完成测试

[csharp] view plain copy
  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
原创粉丝点击