解题思路:
注意事项:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n;
string order;
string Left,Right,oper;
string Hex="10";
//任意进制转换为十进制
LL hexToTen(string num)
{
LL HEX=atoll(Hex.c_str());
LL res=0,tmp=1;
for (int i=num.size()-1;i>=0;i--){
int base;
if (num[i]>='A'&&num[i]<='Z'){
base=num[i]-'A'+10;
}
else{
base=num[i]-'0';
}
res+=tmp*base;
tmp*=HEX;
}
return res;
}
LL getResult()
{
LL a=atoll(Left.c_str());
LL b=atoll(Right.c_str());
LL c=0;
if (oper=="ADD"){
c=a+b;
}
if (oper=="SUB"){
c=a-b;
}
if (oper=="MUL"){
c=a*b;
}
if (oper=="DIV"){
c=a/b;
}
if (oper=="MOD"){
c=a%b;
}
return c;
}
//将结果转化为当前进制 ,并输出
void hexToNow(LL result)
{
if (result==0){
puts("0");
return;
}
vector <char> v;
LL HEX=atoll(Hex.c_str());
LL t;
while (result!=0){
t=result%HEX;
if (t>=10){
v.push_back((char)('A'+t-10));
}
else{
v.push_back((char)(t+'0'));
}
result/=HEX;
}
for (int i=v.size()-1;i>=0;i--){
cout << v[i];
}
cout << endl;
}
void solveOper(string order,string num)
{
char tmp[105];
if (order=="NUM"){
if (oper==""){
sprintf(tmp,"%lld",hexToTen(num));
Left=string(tmp);
}
else{
sprintf(tmp,"%lld",hexToTen(num));
Right=string(tmp);
sprintf(tmp,"%lld",getResult());
Left=string(tmp); oper=Right="";
}
}
else if (order=="CHANGE"){
Hex=num;
}
else if (order=="EQUAL"){
hexToNow(atoll(Left.c_str()));
}
else if (order=="CLEAR"){
Left=oper=Right="";
}
else{
oper=order;
}
}
int main()
{
cin >> n;
getchar();
while (n--){
getline(cin,order);
int index=order.find(' ');
if (index < 0){
solveOper(order,"");
}
else{
solveOper(order.substr(0,index),order.substr(index+1));
}
}
}
0.0分
2 人评分
jalonjia 2020-02-24 21:48:18 |
我的官网满分在这报错
stone 2020-03-10 20:05:25 |
绝了,我笑了。。。