栈应用

括号匹配

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50
typedef char ElemType;

typedef struct{
	ElemType data[MaxSize];
	int top;
}SqStack;

void InitStack(SqStack &S){
	S.top = -1;
}

bool StackEmpty(SqStack &S){
	if(-1 == S.top) return true;
	else return false;
}

bool Push(SqStack &S,ElemType x){
	if(MaxSize - 1 == S.top) return false;
	S.data[++S.top] = x;
	return true;
}

bool Pop(SqStack &S,ElemType &x){
	if(-1 == S.top) return false;
	x = S.data[S.top--];
	return true;
}

bool GetTop(SqStack &S,ElemType &x){
	if(-1 == S.top) return false;
	x = S.data[S.top];
	return true;
}

char * s_gets(char * st, int n){
	char * ret_val;
	int i = 0;
	ret_val = fgets(st, n, stdin);
	if(ret_val){
		while (st[i] != '\n' && st[i] != '\0')
			i++;
		if(st[i] == '\n'){
			st[i] = '\0';
		}else{
			while(getchar() != '\n')
				continue;
		}
	}
	return ret_val;
}
SqStack S;

bool Match(char * input){
	ElemType data;
	while(*input != '\0'){
		switch (*input){
			case '(':
				Push(S,*input);
				break;
			case '[':
				Push(S,*input);
				break;
			case '{':
				Push(S,*input);
				break;
			case ')':
				Pop(S,data);
				if(data != '('){
					return false;
				}
				break;
			case '}':
				Pop(S,data);
				if(data != '{'){
					return false;
				}
				break;
			case ']':
				Pop(S,data);
				if(data != '['){
					return false;
				}
				break;
		}
		input++;
	}
	if(StackEmpty(S)) return true;
	else return false;
}


int main(){
	InitStack(S);
	char input[50];
	s_gets(input,50);
	printf("%s",input);
	bool ret_val = Match(input);
	if(ret_val) printf("Yes");
	else printf("No");
	
	return 0;
}

中缀表达式转后缀表达式

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50
typedef char ElemType;


/**
* 判断操作 
*/

#define ISOPERATOR(ch)  ((ch) == '+' || (ch) == '-' || (ch) == '*' || (ch) == '/')


/**
* 栈结构的定义 
*/
typedef struct{
	ElemType data[MaxSize];
	int top;
}SqStack;

void InitStack(SqStack &S){
	S.top = -1;
}

bool StackEmpty(SqStack &S){
	if(-1 == S.top) return true;
	else return false;
}

bool Push(SqStack &S,ElemType x){
	if(MaxSize - 1 == S.top) return false;
	S.data[++S.top] = x;
	return true;
}

bool Pop(SqStack &S,ElemType &x){
	if(-1 == S.top) return false;
	x = S.data[S.top--];
	return true;
}

bool GetTop(SqStack &S,ElemType &x){
	if(-1 == S.top) return false;
	x = S.data[S.top];
	return true;
}


/**
* 输入处理 
*/
char * s_gets(char * st, int n){
	char * ret_val;
	int i = 0;
	ret_val = fgets(st, n, stdin);
	if(ret_val){
		while (st[i] != '\n' && st[i] != '\0')
			i++;
		if(st[i] == '\n'){
			st[i] = '\0';
		}else{
			while(getchar() != '\n')
				continue;
		}
	}
	return ret_val;
}

/**
* 中缀到后缀 
*/
bool Infix2Suffix(char *input){
	SqStack S;
	InitStack(S);
	char TopElem;
	while(*input){
		if(ISOPERATOR(*input)){//操作符 
			switch(*input){
				case '*':
					Push(S,*input);
					break;
				case '/':
					Push(S,*input);
					break;
				case '+':

					GetTop(S,TopElem);
					while('*' == TopElem || '/' == TopElem || '-' == TopElem){
						Pop(S,TopElem);
						printf("%c",TopElem);
						if(!GetTop(S,TopElem)){
							break;
						}
					}
					Push(S,*input);
					break;
				case '-':
					GetTop(S,TopElem);
					while('*' == TopElem || '/' == TopElem || '+' == TopElem){
						Pop(S,TopElem);
						printf("%c",TopElem);
						if(!GetTop(S,TopElem)){
							break;
						}
					}
					Push(S,*input);
					break;
				default:
					return false;
			}
		}else if('(' == *input){//左括号 
			Push(S,*input);
		}else if(')' == *input){//右括号 
			char PopElem;
			while(!StackEmpty(S)){
				Pop(S,PopElem);
				if('(' == PopElem) break;
				else{
					printf("%c",PopElem);
				} 
			}
			if('(' != PopElem){
				return false;
			}
		}
		else{//字符直接输出 
			printf("%c",*input);
		}
		input++;
	}
	
	while(!StackEmpty(S)){
		Pop(S,TopElem);
		printf("%c",TopElem);
	}
}

int main(){
	char input[MaxSize];
	s_gets(input,MaxSize);
	Infix2Suffix(input);
	return 0;
}

后缀表达式求值

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 50
typedef char ElemType;


/**
* 判断操作
*/
#define ISOPERATOR(ch)  ((ch) == '+' || (ch) == '-' || (ch) == '*' || (ch) == '/')

#define ISDIGIT(ch) ((ch) >= '0' && (ch) <= '9')

#define CACULATE(lnum,rnum) do{ }while(0)

/**
* 栈结构的定义
*/
typedef struct{
	ElemType data[MaxSize];
	int top;
}SqStack;

void InitStack(SqStack &S){
	S.top = -1;
}

bool StackEmpty(SqStack &S){
	if(-1 == S.top) return true;
	else return false;
}

bool Push(SqStack &S,ElemType x){
	if(MaxSize - 1 == S.top) return false;
	S.data[++S.top] = x;
	return true;
}

bool Pop(SqStack &S,ElemType &x){
	if(-1 == S.top) return false;
	x = S.data[S.top--];
	return true;
}

bool GetTop(SqStack &S,ElemType &x){
	if(-1 == S.top) return false;
	x = S.data[S.top];
	return true;
}


/**
* 输入处理
*/
char * s_gets(char * st, int n){
	char * ret_val;
	int i = 0;
	ret_val = fgets(st, n, stdin);
	if(ret_val){
		while (st[i] != '\n' && st[i] != '\0')
			i++;
		if(st[i] == '\n'){
			st[i] = '\0';
		}else{
			while(getchar() != '\n')
				continue;
		}
	}
	return ret_val;
}

/**
* 后缀表达式求值
*/
bool CaculateSuffix(char *input, char *ret_val){
	SqStack num;
	char lnum = 0;
	char rnum = 0;
	InitStack(num);
	while(*input){
		if(ISDIGIT(*input)){//数字直接压栈
			Push(num,*input);
		}else if(ISOPERATOR(*input)){//操作数取数运算
			if(Pop(num,rnum) && Pop(num,lnum)){
                char result;
				switch (*input){
                    case '+':
                        result = (char)((lnum - '0') + (rnum - '0') + '0');
                        break;
                    case '-':
                        result = (char)((lnum - '0') - (rnum - '0') + '0');
                        break;
                    case '*':
                        result = (char)((lnum - '0') * (rnum - '0') + '0');
                        break;
                    case '/':
                        result = (char)((lnum - '0') / (rnum - '0') + '0');
                        break;
                    default:
                        return false;
                }
                Push(num, result);
			}else{//栈中的操作数不够,输入违法
				return false;
			}
		}else{//未知符号直接报错 ,输入违法
			return false;
		}
		input++;
	}
    Pop(num, *ret_val);
    if(StackEmpty(num)) return true;
    return false;
}


int main(){
	char input[MaxSize];
	s_gets(input,MaxSize);
    char ret_val;
	if(CaculateSuffix(input,&ret_val)){
        printf("%d",ret_val - '0');
    }else{
        printf("error");
    }
	return 0;
}

posted @ 2023-03-26 22:22  破忒头头  阅读(36)  评论(0)    收藏  举报