栈的应用(括号匹配、后缀表式计算、中缀转后缀)

#ifndef _Stack_h

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
typedef float ElementType;

int IsEmpty(Stack s);
Stack CreateStack(void);
void DisposeStack(Stack s);
void MakeEmpty(Stack s);
void Push(ElementType x,Stack s);
ElementType Top(Stack s);
void Pop(Stack s);
#endif
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
struct Node{
	ElementType Element;
	PtrToNode Next;
}; 
//判空 
int IsEmpty(Stack s){
	return s->Next==NULL;
}
//创建空栈 
Stack CreateStack(){
	Stack s=(Stack)malloc(sizeof(struct Node));
	if(s==NULL){
		printf("Out of space!!!");
	}
	s->Next=NULL;
	MakeEmpty(s);
	return s;
}
//清空
void MakeEmpty(Stack s){
	if(s==NULL){
		printf("Must use CreateStack first");
	}else{
		while(!IsEmpty(s)){
			Pop(s);
		}
	}
} 
//弹出
void Pop(Stack s){
	PtrToNode FirstCell;
	if(IsEmpty(s)){
		printf("Empty stack");
	}else{
		FirstCell=s->Next;
		s->Next=s->Next->Next;
		free(FirstCell);
	}
	
} 
//弹出并返回top
ElementType PopAndTop(Stack s){
	PtrToNode FirstCell;
	if(IsEmpty(s)){
		printf("Empty stack");
		return '\0';
	}else{
		ElementType ch=Top(s);
		FirstCell=s->Next;
		s->Next=s->Next->Next;
		free(FirstCell);
		return ch;
	}
} 
//压入
void Push(ElementType x,Stack s){
	PtrToNode TmpCell;
	TmpCell=(PtrToNode)malloc(sizeof(struct Node));
	if(TmpCell==NULL){
		printf("Out of space");
	}else{
		TmpCell->Element=x;
		TmpCell->Next=s->Next;
		s->Next=TmpCell;
	}
} 
ElementType Top(Stack s){
	if(!IsEmpty(s)){
		return s->Next->Element;
	}else{
	//	printf("Empty stack");
		return '\0';
	}
}

//栈应用三、中缀到后缀的转换
void convert(char *ch){
	int i=0;
	Stack s=CreateStack();
	while(*(ch+i)!='\0'){
		char c=*(ch+i);
		char op=Top(s);
		//printf("op:%c \n",op);
		switch(c){
			case '+':
			case '-':
				while(op=='-'||op=='+'||op=='*'||op=='/'){
					printf("%c ",op);
					Pop(s);
					op=Top(s);
				}
				Push(c,s);
				break;
			case '*':
			case '/':
				while(op=='*'||op=='/'){
					printf("%c ",op);
					Pop(s);
					op=Top(s);
				}
				Push(c,s);
				break;
			case '(':
				Push(c,s);
				break;
			case ')':
				while(op!='('){
					printf("%c ",op);
					Pop(s);
					op=Top(s);
				}
				Pop(s);
				break;
			default:
				printf("%c ",c);
			break;
		}
		
		i++;
	}
	
	while(!IsEmpty(s)){
		printf("%c ",(char)PopAndTop(s));
	}
	
} 


//栈应用二、后缀表达式的计算
float getResult(const char **ch,int len){
	int i=0;
	Stack s=CreateStack();
//	printf("%c",*ch[9]);
	while(i<len){
		const char *c=ch[i];
		float num1,num2;
		float result=0.0f;
		switch(*c){
			case '+':
			case '-':
			case '*':
			case '/':
				num1=PopAndTop(s);
				num2=PopAndTop(s);
				result=0;
				if(*c=='+'){
					result=num2+num1;
				}else if(*c=='-'){
					result=num2-num1;
				}else if(*c=='*'){
					result=num2*num1;
				}else if(*c=='/'){
					result=num2/num1;
				}
				printf("num1:%f,num2:%f,result:%f  ",num1,num2,result);
				Push(result,s);
				break;
			default:
			//先转为浮点数 
			 	float num=atof(c); 
			 	printf("%f\n",num);
				Push(num,s);
				break;
		}
		i++; 
	}
	return Top(s);
} 
 
//栈应用一、实现括号匹配
bool check(char *ch,int len){
	int i=0;
	Stack s=CreateStack();
	int num2=0;
	while(*(ch+i)!='\0'){
		//用栈来检测
		char c=*(ch+i);
		
		switch(c){
			char result;
			case '{':
			case '[':
			case '(':
				Push(c,s);
				num2=0;
				break;
			case '}':
				result=PopAndTop(s);
				printf("%c %c 第一对该括号出现在%d 和%d\n",result,c,i-num2,i+1);
				if(result!='{'){
					return false;
				}
				break;
			case ']':
				result=PopAndTop(s);
				printf("%c %c 第一对该括号出现在%d 和%d\n",result,c,i-num2,i+1);
				if(result!='['){
					return false;
				}
				break;
			case ')':
				result=PopAndTop(s);
				printf("%c %c 第一对该括号出现在%d 和%d\n",result,c,i-num2,i+1);
				if(result!='('){
					return false;
				}
				break;
			default:num2++;break;
		} 
		i++;
	}
	//对栈中剩余进行判断
	char top=Top(s); 
	if(top!='\0'){
		//匹配失败
		return false; 
	}else{
		return true;
	}
	
} 

int main(){
//	char a[]={"(1+2)+3+4+(1+2)()"};
//	bool b=check(a,sizeof(a));
//	if(b){
//		printf("匹配成功");
//	}else{
//		printf("匹配失败");
//	}
	//测试后缀表达式 
//	const char *a[]={"4.99","1.06","*","5.99","+","6.99","1.06","*","+"};
//	float result=getResult(a,9);
//	printf("\n最后的结果:%f",result);
	//测试中缀表达式转成后比表达式
	char a[]={"a+b*c+(d*e+f)*g"}; 
	convert(a);

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值