算法笔记7.栈的应用:简单计算器

codeup 问题 A: 简单计算器

http://codeup.cn/problem.php?cid=100000605&pid=0

思路:
1.先将中缀表达式转成后缀表达式,
2.计算后缀表达式

#include <cstdio>
#include <iostream> 
#include <string>
#include <stack>
#include <queue>
#include <map>
 
using namespace std;

struct node{
	
	double num;		//操作数 
	char op;		//操作符 
	bool flag;		//操作符为false,操作数为true	
}; 

string str;
stack<node> s;		//在change()存放操作符,在Cal()中存放操作数 
queue<node> q;		//存放后缀表达式的队列 
map<char,int> map;	//关于操作符的映射 

void Change(){		//中缀表达式转后缀表达式 
	
	double num;
	node temp;		
	
	for(int i=0; i<str.length(); ){
		
		if(str[i]>='0' && str[i]<= '9'){	//若是操作数 
			temp.flag = true;
			temp.num = str[i++] - '0';		//操作数可能不是个位数,因此要存储多位 
			while( i<str.length() && str[i] >='0' && str[i]<='9'){	
				temp.num = temp.num *10 + (str[i] - '0');	//若为十位以上,则将化为十进制 
				i++;										//存入后缀表达式的队列中 
			}
			q.push(temp);		//存入后缀表达式的队列中 
		}else {			//若是操作符 
			
			temp.flag = false;
			
			while(!s.empty() && map[str[i]] <= map[s.top().op]  ){		//比较当前操作符与栈顶的优先级,不大于的都弹出 
				q.push(s.top());						//注:s.top()为node型, node.op == '+' 
				s.pop();
			}
			temp.op = str[i] ;		//将当前操作符 
			s.push(temp);			// 入栈
			i++;	
		}
	}//for
	
	while(!s.empty() ){			//若遍历完栈中还有操作符,全部弹出 
		q.push(s.top());
		s.pop();
	}
} 
	
double Cal(){			//计算后缀表达式 

	double x1,x2;
	node temp,cur;
	
	while(!q.empty()){

		cur = q.front();	//cur 记录队首 
		q.pop();			//出队列 
		if( cur.flag == true) 	s.push(cur);	//若为操作数,则入栈 
		else{		//若为操作符,则弹出2个操作数 
			
			x2 = s.top().num;	//先弹第2个数 
			s.pop();
			x1 = s.top().num;	//再弹第1个数,顺序不能乱 
			s.pop();
			
			temp.flag = true;		//临时记录一个操作数 
			if(cur.op == '+') temp.num = x1 + x2;
			else if(cur.op == '-') temp.num = x1- x2;
			else if(cur.op == '*' ) temp.num = x1 *x2;
			else if(cur.op == '/') temp.num = x1/x2;
			
			s.push(temp); 	 
			
		}
	}
	
	return s.top().num; 	//最后剩下的就是结果 
	
	
} 

int main(){
	
	map['+'] = map['-'] = 1;
	map['*'] = map['/'] = 2;	
	
	while(getline(cin,str) , str !="0"){		//getline是获取一整行的字符串 
		
		string::iterator it ;
		for(it = str.end(); it != str.begin() ; it--){
			if(*it == ' ') 			//注意这是指针与int型的比较,不是" "
				str.erase(it);		//将字符串的空格全部清除 
		}
		
		while(!s.empty()) s.pop();	//初始化栈,避免栈不为空,留了东西 
		Change();

		printf("%.2lf\n" , Cal());
		 
		
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值