#include<iostream>
#include<queue>
#include<sstream>
#include<stack>
#include<vector>
typedef long long ll;
using namespace std;
queue<string> code;
vector<ll> num;
string compute[5]={"ADD","SUB","MUL","DIV","MOD"};
ll change(string NUM,ll k){//其他进制转换为10进制
ll x;
stringstream ss;
ss<<NUM;
ss>>x;
if(k==10) return x;
ll n=1;
ll res=0;
for(ll i=NUM.length()-1;i>=0;i--){
if(NUM[i]>='0'&&NUM[i]<='9')res+=(NUM[i]-'0')*n;
else res+=(NUM[i]-'A'+10)*n;
n*=k;
}
return res;
}
string change1(string NUM,ll k){//10进制转换为k进制
stack<char> s1;
ll x;
stringstream ss;
ss<<NUM;
ss>>x;
if(k==10) return NUM;
while(x!=0){
if(x%k>=0&&x%k<=9) s1.push(x%k+'0');
else s1.push(x%k-10+'A');
x/=k;
}
string res="";
while(!s1.empty()){
res+=(char)s1.top();
s1.pop();
}
return res;
}
int main(){
ll n;
cin>>n;
getchar();
string s;
ll K=10;//k表示当前进制
for(ll i=1;i<=n;i++){
getline(cin,s);
code.push(s);
//cout<<s<<endl;
}
ll flag=0,op=-1;
while(!code.empty()){
string now =code.front();
code.pop();
if(now=="CLEAR"){//清除当前数字
if(!num.empty()) num.pop_back();
continue;
}
if(now.substr(0,3)=="CHA"){
string str=now.substr(7,2);
if(str.size()==1) K=str[0]-'0';
else if(str.size()==2) K=(str[0]-'0')*10+(str[1]-'0');
continue;
}
if(now.substr(0,3)=="NUM"){//给数字
string str=now.substr(4);//截取出数字
ll t=change(str,K);
//cout<<"t="<<t<<endl;
num.push_back(t);
if(flag==1){
ll b=num.back();
num.pop_back();
ll a=num.back();
num.pop_back();
if(op==0) num.push_back(a+b);
else if(op==1) num.push_back(a-b);
else if(op==2) num.push_back(a*b);
else if(op==3) num.push_back(a/b);
else if(op==4) num.push_back(a%b);
flag=0;
}
continue;
}
if(now=="EQUAL"){
ll x=num.back();
stringstream ss;
string str1;
ss<<x;
ss>>str1;
//cout<<str1<<endl;
if(str1=="0"){
cout<<0<<endl;
continue;
}
cout<<change1(str1,K)<<endl;
// continue;
}
for(ll i=0;i<5;i++){
if(now==compute[i]){
flag=1;
op=i;
break;
}
}
// cout<<"now:"<<now<<endl;
// for(ll i=0;i<num.size();i++) cout<<num[i]<<" ";
// cout<<endl;
// system("pause");
}
}
通过这道题,也学习到一些东西吧。还是很菜哎。。。。