【栈】简易计算器

题目描述:

    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入:
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出:
    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入:
1 + 2
4 + 2 * 5 - 7 / 11
0
样例输出:
3.00
13.36


解题思路:

能够用语言描述算法过程,是正确编程的第一步。

设立两个堆栈,一个保存运算符,另一个保存数字。

从左至右遍历字符串,若遇到操作符,则比较该操作符与栈顶操作符优先级大小。如果栈为空,或者该操作符优先级比栈顶操作符的优先级大,则将该操作符压入栈;否则,若该操作符不大于栈顶操作符,则弹出栈顶操作符,然后从数字栈中弹出两个栈顶数字完成运算后压入数字栈,重复比较该操作符与栈顶操作符优先级大小,直到该操作符优先级比栈顶操作符的优先级大,则将该操作符压入栈;

若遍历结束时,取出数字栈中数字就是所求。


实现上的技术:

对操作符编号,使用一个优先级矩阵判断优先级大小;

在表达式的首尾添加标记运算符,该运算符优先级最低;

使用gets读取一行,然后调用函数getop,不断取节点

使用double类型保存运算结果和数字栈类型


代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<stack>
using namespace std;
char str[201];
stack<int> op;
stack<double> num;
//优先级矩阵
//运算符编码:
//0 首尾 
//1:+
//2:-
//3:*
//4:/
int mat[5][5]={
	1,0,0,0,0,
	1,0,0,0,0,
	1,0,0,0,0,
	1,1,1,0,0,
	1,1,1,0,0
};
void getOp(bool &reto,int &retn,int &i){
	//reto: true为运算符 false 为数字
	//retn: 运算符编号 或者 返回的数字
	//i为遍历当面字符串的位置下标
	if(i==0&&op.empty()){
		//如果op一开始是空的,i=0
		//人为添加标记运算符
		reto=true;
		retn=0;
		return;
	}
	if(str[i]==0){
		//str结尾
		reto=true;
		retn=0;
		return;
	}
	if(str[i]>='0'&&str[i]<='9'){
		reto=false;
		int c=1;
		retn=0;
		for(;str[i]!=' '&&str[i]!=0;i++){
			retn*=10;
			retn+=(str[i]-'0');
		}
		if(str[i]==' '){
			i++;
		}
		return;
	}else{
		reto=true;
		switch(str[i]){
		case '+':
			retn=1;
			break;
		case '-':
			retn=2;
			break;
		case '*':
			retn=3;
			break;
		case '/':
			retn=4;
			break;
		}
		i+=2;
		return;
	}
}
int main(){
	while(gets(str)){
		if(str[0]=='0'&&str[1]==0)
			break;
		while(!op.empty()){
			op.pop();	
		}
		while(!num.empty()){
			num.pop();
		}
		int idx=0;
		bool retop;
		int retnum;
		while(true){
			getOp(retop,retnum,idx);
			if(retop){
				if(op.empty()||mat[retnum][op.top()]==1){
					op.push(retnum);
				}else{
					while(mat[retnum][op.top()]==0){
						int tmpop=op.top();
						double tmpnum;//double double double
						op.pop();
						double b=num.top();
						num.pop();
						double a=num.top();
						num.pop();
						if(tmpop==1){
							tmpnum=a+b;
						}else if(tmpop==2){
							tmpnum=a-b;
						}else if(tmpop==3){
							tmpnum=a*b;
						}else if(tmpop==4){
							tmpnum=a/b;
						}
						num.push(tmpnum);
					}
					op.push(retnum);//不要忘了压入栈 不要忘了压入栈 不要忘了压入栈
				}

			}else{
				//数字
				num.push((double)retnum);
			}
			if(op.size()==2&&op.top()==0){
				break;
			}
		}
		printf("%.2f\n",num.top());
	}

}

猜你喜欢

转载自blog.csdn.net/qq_33837704/article/details/80771553