给出一个表达式,求出其值。表达式中只存在 +、-、*、三种运算,我们假设表达式是正确的,
且不存在除数为零的情况。
使用STL的stack容易实现。
#include<iostream>#include<stack>//remember top#include<algorithm>#include<iterator>#include<iomanip>using namespace std;int main(){ int n; while(cin>>n) { double a; char b; stack<double> d_sta; stack<char> c_sta; while(n--) { cin>>a>>b; if(!d_sta.empty())//d,c all can { if(c_sta.top()==‘-‘) a=-a; else if(c_sta.top()==‘*‘) { a*=d_sta.top(); d_sta.pop();//dessert } } d_sta.push(a); c_sta.push(b); } a=0; while(!d_sta.empty())//as if have not iterator { a+=d_sta.top(); d_sta.pop(); } cout<<fixed<<setprecision(2)<<a<<endl; } return 0;}Problem D: STL——表达式求值(用的vector没用stack)
原文:http://www.cnblogs.com/TogetherLaugh/p/6653709.html