C语言实现逆波兰法计算表达式

主体:两个函数

	transMtoA(std,stdA);//将中缀表达式->后缀表达式
	caculate(stdA,stacknum);//计算结果

注意!!! 减法和除法时stack[top - 2] - stack[top - 1]!!!
全部代码:

#include<stdio.h> 
int cur = 0;
int isDigit(char* s) {
	return s >= '0' && s <= '9';
}
int getBit(int num) {
	int count = 0;
	while(num > 0) {
		num = num/10;
		count++;
	}
	return count;
}
int getPri(char op) {
	switch(op){
		case '+':return 1;
		case '-':return 1;
		case '*':return 2;
		case '/':return 2;
		case '(':return 0;
		default: return 0;
	}
}
void transMtoA(char* strM,char* strA) {
	int i;
	char out[100] = {0};
	int  lo = 0;
	
	char stack[100] = {0};
	int top = 0;
	
	for(i = 0;i < strlen(strM);i++) {
		if(isDigit(strM[i])) {
			out[lo++] = strM[i];
		}else if(strM[i] == '(') {
			stack[top++] = strM[i];
		}else if(strM[i] == ')') {
			while(top > 0 && stack[top - 1] != '(') {
				out[lo++] = stack[top - 1];
				top--;
			}
			top--;//弹出左括号 
		}else {
			if(getPri(strM[i]) > getPri(stack[top - 1])) {
				stack[top++] = strM[i];
			}else {
				while( top > 0 && getPri(stack[top - 1]) >= getPri(strM[i]) ) {
					out[lo++] = stack[top - 1];
					top--;	
				}
				stack[top++] = strM[i];
			}
		} 
	} 
	while(top > 0) {
		out[lo++] = stack[top - 1];
		top--;
	}
	strcpy(strA,out);
	return;
}
void caculate(char* stdA,int* stacknum) {
	double stack[100] = {0};
	int top = 0;
	int cur = 0;
	int count = 0;
	double tmp = 0;
	while(cur < strlen(stdA)) {
		if(isDigit(stdA[cur])) {
			stack[top++] = (double)stacknum[count];
			cur = cur+getBit(stacknum[count]);
			count++;
		}else {
			switch (stdA[cur]){
				case '+' : 
					tmp = stack[top - 1] + stack[top - 2]; 
					//printf("%.2f + %.2f -> %.2f\n",stack[top - 1],stack[top - 2],tmp);
					top = top - 2; 
					stack[top++] = tmp;
					break;
				case '-' :
					tmp = stack[top - 2] - stack[top - 1];
					//printf("%.2f - %.2f -> %.2f\n",stack[top - 2],stack[top - 1],tmp);
					top = top - 2;
					stack[top++] = tmp;
					break;
				case '*' :
					tmp = stack[top - 1] * stack[top - 2];
					//printf("%.2f * %.2f -> %.2f\n",stack[top - 1],stack[top - 2],tmp);
					top = top - 2;
					stack[top++] = tmp;
					break;
				case '/' :
					tmp = stack[top - 2] / stack[top - 1];
					//printf("%.2f / %.2f -> %.2f\n",stack[top - 2],stack[top - 1],tmp);
					top = top - 2;
					stack[top++] = tmp;
					break;
				default:
				 	break;
			}
			cur++;
		}
	}
	printf("%.2f",stack[0]);
}
int main() {
	char str[100] = {0};
	char std[100] = {0};
	char stdA[100] = {0};
	int stacknum[100] = {0};
	int num = 0;
	int topN = 0;
	gets(str);
	int i;
	int length = 0;
	for(i = 0;i < strlen(str);i++) {
		if(str[i] == ' ') {
			continue;
		}else if(str[i] == '=') {
			continue;
		}else std[length++] = str[i];
	}
	std[length++] = '\0';
	int isNum = 0;
	//---------------------------------------
	i = 0;
	while(i < strlen(std)) {
		isNum = 0;
		while(isDigit(std[i])) {
			num = num*10 + std[i] - '0';
			i++;
			isNum = 1;
		}
		if(isNum) {
			stacknum[topN++] = num;
			num = 0;
			//printf("%d ",stacknum[topN - 1]);
		}else {
			i++;
		}
	}
	//-----------------------------------------
	//printf("\n");
	transMtoA(std,stdA);
	//printf("%s\n",stdA);
	caculate(stdA,stacknum);
	return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值