- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- typedef char SElemType;
- typedef int Status;
- #define OVERFLOW -2
- #define ERROR 0
- #define STACK_INIT_SIZE 100
- #define STACKINCREMENT 10
- typedef struct
- {
- SElemType *base;
- SElemType *top;
- int stacksize;
- }SqStack;
- Status InitStack(SqStack &S);
- bool StackEmpty(SqStack S);
- Status Push(SqStack &S,SElemType e);
- Status Pop(SqStack &S,SElemType &e);
- void conversion(SqStack &S);
- Status Bracket(SqStack &S);
- void main()
- {
- SqStack S;
- Bracket(S);
- }
- Status InitStack(SqStack &S)
- {
- S.base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
- if(!S.base)exit(OVERFLOW);
- S.top=S.base;
- S.stacksize=STACK_INIT_SIZE;
- return 1;
- }
- bool StackEmpty(SqStack S)
- {
- if(S.top==S.base)
- return true; //kong
- else
- return false;
- }
- Status Push(SqStack &S,SElemType e)
- {
- if(S.top-S.base>=S.stacksize)
- {
- S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT) *sizeof(SElemType));
- if(!S.base)exit(OVERFLOW);
- S.top=S.base+S.stacksize;
- S.stacksize+=STACKINCREMENT;
- }
- *S.top++=e;
- return 1;
- }
- Status Pop(SqStack &S,SElemType &e)
- {
- if(S.top==S.base) return ERROR;
- e=*--S.top;
- return 1;
- }
- Status POP(SqStack &S,char &e)
- {
- if(S.top==S.base) return ERROR;
- e=*--S.top;
- return 1;
- }
- Status Bracket(SqStack &S)
- {
- char str[100];
- printf("请输入你要匹配的括号:");
- scanf("%s",str);
- InitStack(S);
- int i;
- bool t=true,t1=true,t2=true;
- char e;
- for(i=0;str[i]!='/0';i++)
- {
- if (str[i]=='(')
- Push(S,'(');
- if (str[i]=='[')
- Push(S,'[');
- if (str[i]=='{')
- Push(S,'{');
- if (str[i]==')')
- {
- if(StackEmpty(S))t=false; //栈空无左括号与之匹配;则为假
- Pop(S,e);
- if(e!='(') t1=false;
- }
- if (str[i]==']')
- {
- if(StackEmpty(S))t=false;
- Pop(S,e);
- if(e!='[') t1=false;
- }
- if (str[i]=='}')
- {
- if(StackEmpty(S))t=false;
- Pop(S,e);
- if(e!='{')t1=false;
- }
- else
- {
- printf("输入不正确!/n");
- exit(0);
- }
- }
- if(t&& t1) printf("括号匹配!/n/n");
- else printf("括号不匹配!/n/n");
- return 0;
- }
- //顺序栈的应用----数制转换
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- typedef int SElemType;
- typedef int Status;
- #define OVERFLOW -2
- #define ERROR 0
- #define STACK_INIT_SIZE 100
- #define STACKINCREMENT 10
- typedef struct
- {
- SElemType *base;
- SElemType *top;
- int stacksize;
- }SqStack;
- Status InitStack(SqStack &S);
- Status StackEmpty(SqStack S);
- Status Push(SqStack &S,SElemType e);
- Status Pop(SqStack &S,SElemType &e);
- void conversion(SqStack &S);
- Status Bracket(SqStack &S);
- void main()
- {
- SqStack S;
- conversion(S);
- }
- //数制转换
- void conversion(SqStack &S)
- {
- SElemType N,d,e;
- char ch[]="0123456789ABCDEF";
- InitStack(S);
- printf("请输入你要转换的进制:");
- scanf("%d",&d);
- printf("请输入一个你要转换的十进制数:");
- scanf("%d",&N);
- while (N)
- {
- Push(S,ch[N%d]);
- N=N/d;
- }
- printf("转换后的结果为:");
- while (!StackEmpty(S))
- {
- Pop(S,e);
- printf("%c",e);
- }
- printf("/n");
- }
- Status InitStack(SqStack &S)
- {
- S.base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
- if(!S.base)exit(OVERFLOW);
- S.top=S.base;
- S.stacksize=STACK_INIT_SIZE;
- return 1;
- }
- Status StackEmpty(SqStack S)
- {
- if(S.top==S.base)
- return 1; //kong
- else
- return 0;
- }
- Status Push(SqStack &S,SElemType e)
- {
- if(S.top-S.base>=S.stacksize)
- {
- S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT) *sizeof(SElemType));
- if(!S.base)exit(OVERFLOW);
- S.top=S.base+S.stacksize;
- S.stacksize+=STACKINCREMENT;
- }
- *S.top++=e;
- return 1;
- }
- Status Pop(SqStack &S,SElemType &e)
- {
- if(S.top==S.base) return ERROR;
- e=*--S.top;
- return 1;
- }
- Status POP(SqStack &S,char &e)
- {
- if(S.top==S.base) return ERROR;
- e=*--S.top;
- return 1;
- }