Problem H: STL——表达式求值

xiaoxiao2021-02-28  124

HomeWeb BoardProblemSetStandingStatusStatistics

Problem H: STL——表达式求值

Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 2535   Solved: 1453 [ Submit][ Status][ Web Board]

Description

给出一个表达式,求出其值。表达式中只存在 +、-、*、三种运算,我们假设表达式是正确的,       且不存在除数为零的情况。

Input

第一行输入一个正整数 n(1<=n<=30) ,表示有表达式 n 个数(每个数均小于100),表达式中只有数值(都是大于零的数)       和运算符(包括+、-、*、=四种运算符,其中 = 只在表达式最后,表示一个表达式输出结束,且整个表达式不存在空格)

Output

表达式的值(表达式的值不会超出 double 的范围并保留两位小数)

Sample Input

51*2*3*4*5=55-1-2+3+4=

Sample Output

120.009.00

HINT

使用STL的stack容易实现。

Append Code

[ Submit][ Status][ Web Board]

한국어<  中文 فارسی English ไทย All Copyright Reserved 2010-2011 SDUSTOJ TEAM GPL2.0 2003-2011 HUSTOJ Project TEAM Anything about the Problems, Please Contact Admin:admin

#include<iostream> #include<stack> #include<iomanip> using namespace std; int main() { stack<double>a; int n; while(cin>>n) { double temp, sum; sum = 0; char c; cin >> temp; a.push(temp); for(int i = 1; i < n; i++) { cin >> c >> temp; if( c == '+' ) a.push(temp); else if( c == '-' ) a.push(-temp); else if(c == '*') { temp *= a.top(); a.pop(); a.push(temp); } } cin >> c; while(!a.empty()) { sum += a.top(); a.pop(); } cout << fixed << setprecision(2) << sum << endl; } }
转载请注明原文地址: https://www.6miu.com/read-44194.html

最新回复(0)