Sicily 4960 Identity Checker

来源:互联网 发布:双色球软件破解版 编辑:程序博客网 时间:2024/06/17 16:36

直接用栈模拟,“-”顺序别弄错,不然会WA...

#include<iostream>#include<string>#include<cmath>#include<stack>using namespace std;int main() {    int n;        while (cin >> n && n) {        stack<double> stk;        string str;        for (int i = 0; i < n; i++) {            cin >> str;            if (str == "x") {                stk.push(2);            } else if (str == "sin") {                double temp = stk.top();                stk.pop();                stk.push(sin(temp));            } else if (str == "cos") {                double temp = stk.top();                stk.pop();                stk.push(cos(temp));            } else if (str == "tan") {                double temp = stk.top();                stk.pop();                stk.push(tan(temp));            } else if (str == "+") {                double temp1 = stk.top();                stk.pop();                double temp2 = stk.top();                stk.pop();                stk.push(temp1 + temp2);            } else if (str == "-") {                double temp1 = stk.top();                stk.pop();                double temp2 = stk.top();                stk.pop();                stk.push(temp2 - temp1);            } else if (str == "*") {                double temp1 = stk.top();                stk.pop();                double temp2 = stk.top();                stk.pop();                stk.push(temp1 * temp2);            }        }        if (fabs(stk.top()) < 0.00000001) {            cout << "Identity" << endl;        } else {            cout << "Not an identity" << endl;        }    }}


0 0
原创粉丝点击