C++-蓝桥杯-小计算器[进制转化][模拟]

 1 #include <cstdio>
 2 #include <string>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 typedef long long ll;
 7 ll num,k=10,ans;int t,calc,len;string str;char s[30];
 8 ll get10(){
 9     ans=0,scanf("%s",s+1),len=strlen(s+1);
10     for(int i=1;i<=len;i++)ans=ans*k+(s[i]>'9'?s[i]-'A'+10:s[i]-'0');
11     return ans;
12 }
13 void print(ll x){if(x)print(x/k),putchar(x%k<=9?x%k+'0':x%k+'A'-10);}
14 void print(){if(num==0)putchar('0');else print(num);}//之前没考虑到答案为0 
15 void oper(){
16     cin>>str;
17     if(str=="CHANGE"){scanf("%lld",&k);return;}
18     if(str=="EQUAL"){print(),puts("");return;}
19     if(str=="CLEAR"){num=calc=0;return;}
20     if(str=="ADD"){calc=1;return;}
21     if(str=="SUB"){calc=2;return;}
22     if(str=="MUL"){calc=3;return;}
23     if(str=="DIV"){calc=4;return;} 
24     if(str=="MOD"){calc=5;return;}
25     if(str=="NUM")
26         switch(calc){
27             case 0:num+=get10();break;//这里意思是刚刚CLEAR过 
28             case 1:num+=get10();break;
29             case 2:num-=get10();break;
30             case 3:num*=get10();break;
31             case 4:num/=get10();break;
32             case 5:num%=get10();break;    
33         }        
34 }
35 int main(){
36     for(scanf("%d",&t);t--;)oper();
37     return 0;
38 } 

 

posted @ 2020-02-28 16:26  墨鳌  阅读(297)  评论(0编辑  收藏  举报