直接用栈模拟,“-”顺序别弄错,不然会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;
}
}
}