第五周项目三

来源:互联网 发布:阿里云ecs快照服务 编辑:程序博客网 时间:2024/05/21 06:57
/*
Copyright (c++) 2017,烟台大学计算机与控制工程学院
文件名称:sqstack
作 者:白苗苗
完成日期:2017年9月28日
版 本 号:12.11
问题描述:定义顺序栈存储结构,实现其基本运算,并完成测试。
输入描述:表达式
输出描述:配对错误/正确

*/

[cpp] 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  

[cpp] 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.   
  11. void DestroyStack(SqStack *&s)  
  12. {  
  13.     free(s);  
  14. }  
  15.   
  16. int StackLength(SqStack *s)  //返回栈中元素个数——栈长度  
  17. {  
  18.     return(s->top+1);  
  19. }  
  20.   
  21. bool StackEmpty(SqStack *s)//栈是否为空  
  22. {  
  23.     return(s->top==-1);  
  24. }  
  25.   
  26. bool Push(SqStack *&s,ElemType e)  
  27. {  
  28.     if (s->top==MaxSize-1)    //栈满的情况,即栈上溢出  
  29.         return false;  
  30.     s->top++;  
  31.     s->data[s->top]=e;  
  32.     return true;  
  33. }  
  34.   
  35. bool Pop(SqStack *&s,ElemType &e)  
  36. {  
  37.     if (s->top==-1)     //栈为空的情况,即栈下溢出  
  38.         return false;  
  39.     e=s->data[s->top];  
  40.     s->top--;  
  41.     return true;  
  42. }  
  43.   
  44. bool GetTop(SqStack *s,ElemType &e)  
  45. {  
  46.     if (s->top==-1)         //栈为空的情况,即栈下溢出  
  47.         return false;  
  48.     e=s->data[s->top];  
  49.     return true;  
  50. }  
  51.   
  52. void DispStack(SqStack *s)  //输出栈  
  53. {  
  54.     int i;  
  55.     for (i=s->top;i>=0;i--)  
  56.         printf("%c ",s->data[i]);  
  57.     printf("\n");  
  58. }  

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