数据结构07-栈的应用:表达式求值

这篇看学长的思路用C写了一遍

目录

结构定义

初始化

压栈

弹栈

优先级判断

计算

主函数

完整代码

运行结果示例


结构定义

typedef struct Stack{
	int numdata[MAXSIZE];
	char chdata[MAXSIZE];
	
	int numtop;
	int chtop;	
}*StackPtr;

初始化

StackPtr iniStack(){
	StackPtr p = (StackPtr)malloc(sizeof(struct Stack));
	p->chtop = -1;
	p->numtop = -1;
	
	return p;
}

压栈

void numpush(StackPtr pStack,int pnum){
	if(pStack->numtop >= MAXSIZE-1){
		printf("Cannot push element: stack full.\n");
		return;
	}
	
	pStack->numtop++;

	pStack->numdata[pStack->numtop] = pnum;
}

void chpush(StackPtr pStack,char pch){
	if(pStack->chtop >= MAXSIZE-1){
		printf("Cannot push element: stack full.\n");
		return;
	}
	
	pStack->chtop++;
	
	pStack->chdata[pStack->chtop] = pch;
}

弹栈

int numpop(StackPtr pStack){
	if(pStack->numtop < 0){
		printf("Cannot pop element: stack empty.\r\n");
        return 0;
	}
	
	pStack->numtop--;
	
	return pStack->numdata[pStack->numtop+1];	
}

char chpop(StackPtr pStack){
	if(pStack->chtop < 0){
		printf("Cannot pop element: stack empty1.\r\n");
        return '\0';
	}
	
	pStack->chtop--;
	
	return pStack->chdata[pStack->chtop+1];
}

优先级判断

int judgePriority(char pch){
	if(pch == '+' || pch == '-'){
		return 1;
	}else if(pch == '*' || pch == '/'){
		return 2;
	}
}

计算

void eval(StackPtr pStack){
	int b = numpop(pStack);
	int a = numpop(pStack);
	char c = chpop(pStack);
	
	int x;
	
	if(c == '+'){
		x = a + b;
	}else if((c == '-')){
		x = a - b;
	}else if (c == '*'){
		x = a * b;
	}else{
		x = a / b;
	}
	numpush(pStack,x);
}

主函数

int main(){
	StackPtr tempStack = iniStack();
	
	char a[MAXSIZE];
	gets(a);
	int size = strlen(a);
	
	int i;
	for(i = 0;i < size;i++){
		char c = a[i];
		
		if(isdigit(c)){
			int x = 0,j = i;
			
			while(j < size && isdigit(a[j])){
				x = x * 10 + a[j++] - '0';
			}
			i = j - 1;
			numpush(tempStack,x);
		}else if(c == '('){
			chpush(tempStack,c);
		}else if(c == ')'){
			while (tempStack->chdata[tempStack->chtop] != '('){
				eval(tempStack);
			}
			chpop(tempStack);
		}else{
			while( tempStack->chtop != -1 && tempStack->chdata[tempStack->chtop] != '(' && judgePriority(tempStack->chdata[tempStack->chtop]) >= judgePriority(c)){
				eval(tempStack);
			}
			chpush(tempStack,c);
		}
	}
	while(tempStack->chtop != -1){
		eval(tempStack);
	}
	printf("%d",tempStack->numdata[tempStack->numtop]);
	return 0;
}

完整代码

#include<stdio.h>
#include<malloc.h>
#include<string.h>

#define MAXSIZE 100

typedef struct Stack{
	int numdata[MAXSIZE];
	char chdata[MAXSIZE];
	
	int numtop;
	int chtop;	
}*StackPtr;

StackPtr iniStack(){
	StackPtr p = (StackPtr)malloc(sizeof(struct Stack));
	p->chtop = -1;
	p->numtop = -1;
	
	return p;
}


void numpush(StackPtr pStack,int pnum){
	if(pStack->numtop >= MAXSIZE-1){
		printf("Cannot push element: stack full.\n");
		return;
	}
	
	pStack->numtop++;

	pStack->numdata[pStack->numtop] = pnum;
}

void chpush(StackPtr pStack,char pch){
	if(pStack->chtop >= MAXSIZE-1){
		printf("Cannot push element: stack full.\n");
		return;
	}
	
	pStack->chtop++;
	
	pStack->chdata[pStack->chtop] = pch;
}

int numpop(StackPtr pStack){
	if(pStack->numtop < 0){
		printf("Cannot pop element: stack empty.\r\n");
        return 0;
	}
	
	pStack->numtop--;
	
	return pStack->numdata[pStack->numtop+1];	
}

char chpop(StackPtr pStack){
	if(pStack->chtop < 0){
		printf("Cannot pop element: stack empty1.\r\n");
        return '\0';
	}
	
	pStack->chtop--;
	
	return pStack->chdata[pStack->chtop+1];
}

int judgePriority(char pch){
	if(pch == '+' || pch == '-'){
		return 1;
	}else if(pch == '*' || pch == '/'){
		return 2;
	}
}

void eval(StackPtr pStack){
	int b = numpop(pStack);
	int a = numpop(pStack);
	char c = chpop(pStack);
	
	int x;
	
	if(c == '+'){
		x = a + b;
	}else if((c == '-')){
		x = a - b;
	}else if (c == '*'){
		x = a * b;
	}else{
		x = a / b;
	}
	numpush(pStack,x);
}

int main(){
	StackPtr tempStack = iniStack();
	
	char a[MAXSIZE];
	gets(a);
	int size = strlen(a);
	
	int i;
	for(i = 0;i < size;i++){
		char c = a[i];
		
		if(isdigit(c)){
			int x = 0,j = i;
			
			while(j < size && isdigit(a[j])){
				x = x * 10 + a[j++] - '0';
			}
			i = j - 1;
			numpush(tempStack,x);
		}else if(c == '('){
			chpush(tempStack,c);
		}else if(c == ')'){
			while (tempStack->chdata[tempStack->chtop] != '('){
				eval(tempStack);
			}
			chpop(tempStack);
		}else{
			while( tempStack->chtop != -1 && tempStack->chdata[tempStack->chtop] != '(' && judgePriority(tempStack->chdata[tempStack->chtop]) >= judgePriority(c)){
				eval(tempStack);
			}
			chpush(tempStack,c);
		}
	}
	while(tempStack->chtop != -1){
		eval(tempStack);
	}
	printf("%d",tempStack->numdata[tempStack->numtop]);
	return 0;
}

运行结果示例

(1+2*6)-12+9/3
4

总结:

一味地照着修改让我走了一些弯路,主要是在运算是否完成的判断上。另外,dev c++居然会有逐步debug能出正确答案而直接编译运行不能出正确答案的情况,令我百思不得其解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值