栈的应用

Code:
  1. #include <iostream>   
  2. #include <stdio.h>   
  3. #include <string.h>   
  4. typedef char SElemType;   
  5. typedef int Status;   
  6. #define OVERFLOW -2   
  7. #define ERROR 0   
  8. #define STACK_INIT_SIZE 100   
  9. #define STACKINCREMENT 10    
  10. typedef struct    
  11. {   
  12.     SElemType *base;   
  13.     SElemType *top;   
  14.     int stacksize;   
  15. }SqStack;   
  16.   
  17. Status InitStack(SqStack &S);   
  18. bool StackEmpty(SqStack S);   
  19. Status Push(SqStack &S,SElemType e);   
  20. Status Pop(SqStack &S,SElemType &e);   
  21. void conversion(SqStack &S);   
  22. Status Bracket(SqStack &S);   
  23.   
  24.   
  25. void main()   
  26. {   
  27.     SqStack S;   
  28.     Bracket(S);   
  29. }   
  30.   
  31. Status InitStack(SqStack &S)   
  32. {   
  33.     S.base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));   
  34.     if(!S.base)exit(OVERFLOW);   
  35.     S.top=S.base;   
  36.     S.stacksize=STACK_INIT_SIZE;   
  37.     return 1;   
  38. }   
  39.       
  40.   
  41. bool StackEmpty(SqStack S)   
  42. {   
  43.     if(S.top==S.base)   
  44.         return true;    //kong   
  45.     else    
  46.         return false;   
  47. }   
  48.   
  49.   
  50. Status Push(SqStack &S,SElemType e)   
  51. {   
  52.     if(S.top-S.base>=S.stacksize)   
  53.     {   
  54.         S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT) *sizeof(SElemType));   
  55.         if(!S.base)exit(OVERFLOW);   
  56.         S.top=S.base+S.stacksize;   
  57.         S.stacksize+=STACKINCREMENT;   
  58.     }   
  59.     *S.top++=e;   
  60.     return 1;   
  61. }   
  62.   
  63.   
  64.   
  65. Status Pop(SqStack &S,SElemType &e)   
  66. {   
  67.     if(S.top==S.base) return ERROR;   
  68.     e=*--S.top;   
  69.     return 1;   
  70. }   
  71.   
  72.   
  73. Status POP(SqStack &S,char &e)   
  74. {   
  75.     if(S.top==S.base) return ERROR;   
  76.     e=*--S.top;   
  77.     return 1;   
  78. }   
  79.   
  80.   
  81. Status Bracket(SqStack &S)   
  82. {   
  83.     char str[100];   
  84.     printf("请输入你要匹配的括号:");   
  85.     scanf("%s",str);   
  86.   
  87.     InitStack(S);   
  88.     int i;   
  89.     bool t=true,t1=true,t2=true;   
  90.     char e;   
  91.    for(i=0;str[i]!='/0';i++)   
  92.     {   
  93.   
  94.         if (str[i]=='(')   
  95.             Push(S,'(');   
  96.         if (str[i]=='[')   
  97.             Push(S,'[');   
  98.         if (str[i]=='{')   
  99.             Push(S,'{');   
  100.         if (str[i]==')')   
  101.             {   
  102.                 if(StackEmpty(S))t=false;   //栈空无左括号与之匹配;则为假   
  103.                 Pop(S,e);   
  104.                 if(e!='(') t1=false;    
  105.             }    
  106.             if (str[i]==']')   
  107.             {   
  108.                 if(StackEmpty(S))t=false;   
  109.                 Pop(S,e);   
  110.                 if(e!='[') t1=false;   
  111.             }    
  112.         if (str[i]=='}')   
  113.             {   
  114.                 if(StackEmpty(S))t=false;   
  115.                 Pop(S,e);   
  116.                 if(e!='{')t1=false;   
  117.             }   
  118.         else    
  119.         {   
  120.             printf("输入不正确!/n");   
  121.             exit(0);   
  122.         }   
  123.     }   
  124.     if(t&& t1) printf("括号匹配!/n/n");   
  125.     else printf("括号不匹配!/n/n");   
  126.     return 0;   
  127. }   
  128.   
Code:
  1. //顺序栈的应用----数制转换   
  2. #include <iostream>   
  3. #include <stdio.h>   
  4. #include <string.h>   
  5. typedef int SElemType;   
  6. typedef int Status;   
  7. #define OVERFLOW -2   
  8. #define ERROR 0   
  9. #define STACK_INIT_SIZE 100   
  10. #define STACKINCREMENT 10    
  11. typedef struct    
  12. {   
  13.     SElemType *base;   
  14.     SElemType *top;   
  15.     int stacksize;   
  16. }SqStack;   
  17.   
  18. Status InitStack(SqStack &S);   
  19. Status StackEmpty(SqStack S);   
  20. Status Push(SqStack &S,SElemType e);   
  21. Status Pop(SqStack &S,SElemType &e);   
  22. void conversion(SqStack &S);   
  23. Status Bracket(SqStack &S);   
  24.   
  25.   
  26. void main()   
  27. {   
  28.     SqStack S;   
  29.     conversion(S);   
  30. }   
  31. //数制转换   
  32. void conversion(SqStack &S)   
  33. {   
  34.     SElemType N,d,e;   
  35.     char ch[]="0123456789ABCDEF";   
  36.     InitStack(S);   
  37.     printf("请输入你要转换的进制:");   
  38.     scanf("%d",&d);   
  39.     printf("请输入一个你要转换的十进制数:");   
  40.     scanf("%d",&N);   
  41.     while (N)   
  42.     {   
  43.         Push(S,ch[N%d]);   
  44.         N=N/d;   
  45.     }   
  46.     printf("转换后的结果为:");   
  47.     while (!StackEmpty(S))   
  48.     {   
  49.         Pop(S,e);   
  50.         printf("%c",e);   
  51.     }   
  52.     printf("/n");   
  53. }   
  54.   
  55. Status InitStack(SqStack &S)   
  56. {   
  57.     S.base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));   
  58.     if(!S.base)exit(OVERFLOW);   
  59.     S.top=S.base;   
  60.     S.stacksize=STACK_INIT_SIZE;   
  61.     return 1;   
  62. }   
  63.       
  64.   
  65. Status StackEmpty(SqStack S)   
  66. {   
  67.     if(S.top==S.base)   
  68.         return 1;    //kong   
  69.     else    
  70.         return 0;   
  71. }   
  72.   
  73.   
  74. Status Push(SqStack &S,SElemType e)   
  75. {   
  76.     if(S.top-S.base>=S.stacksize)   
  77.     {   
  78.         S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT) *sizeof(SElemType));   
  79.         if(!S.base)exit(OVERFLOW);   
  80.         S.top=S.base+S.stacksize;   
  81.         S.stacksize+=STACKINCREMENT;   
  82.     }   
  83.     *S.top++=e;   
  84.     return 1;   
  85. }   
  86.   
  87.   
  88.   
  89. Status Pop(SqStack &S,SElemType &e)   
  90. {   
  91.     if(S.top==S.base) return ERROR;   
  92.     e=*--S.top;   
  93.     return 1;   
  94. }   
  95.   
  96.   
  97. Status POP(SqStack &S,char &e)   
  98. {   
  99.     if(S.top==S.base) return ERROR;   
  100.     e=*--S.top;   
  101.     return 1;   
  102. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值