+A -A +1A 12 1A -9 -1A -12 1A -AA
0 2C 11 -2C -90
遇到的问题和思路:
水题。虽然一次AC了,不过在过程中出现了好多错误。因为个人的懒惰加上前后步骤相似,所以就复制粘贴了,然而付出的代价是惨痛的,浪费了很多时间在调试上面,以后应当慢慢打出来,总比复制粘贴以后调试所来的要快。
给出代码:
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define inf 1e-12
using namespace std;
long long sum1,sum2;
char a[16],b[16],c[19];
int main(){
while(scanf("%s %s",&a,&b)!=EOF){
sum1 = sum2 = 0;
getchar();
memset(c,0,sizeof(c));
//转换成十进制
for(int i = 0;i < strlen(a);i++){
if(a[i]=='-'||a[i] == '+')continue;
else if(a[i] >= 'A'&&a[i] <= 'F')sum1 = sum1 * 16 + a[i]-'A'+10;
else sum1 = sum1 * 16 + a[i]-'0';
}
if(a[0] == '-')sum1*=-1;
for(int i = 0;i < strlen(b);i++){
if(b[i]=='-'||b[i] == '+')continue;
else if(b[i] >= 'A'&&b[i] <= 'F')sum2 = sum2 * 16 + b[i]-'A'+10;
else sum2 = sum2 * 16 + b[i]-'0';
}
if(b[0] == '-')sum2*=-1;
long long summ = sum1 + sum2;//求和
//printf("%I64d %I64D %I64D\n",sum1,sum2,summ);
int count1 = 0;
//转换回十六进制
if(summ < 0){
summ*=-1;
int i = 0;
while(summ){
if(summ % 16 >= 10)c[i] = summ % 16 + 'A' - 10;
else c[i] = summ % 16 + '0';
summ/=16;
i++;
}
c[i] = '-';
c[++i] = '\0';
}
else if(summ > 0){
int i = 0;
while(summ){
if(summ % 16 >= 10)c[i] = summ % 16 + 'A' - 10;
else c[i] = summ % 16 + '0';
summ/=16;
i++;
}
c[i] = '\0';
}
else {
c[0] = '0';
c[1] = '\0';
}
for(int i = strlen(c)-1;i >= 0;i--)printf("%c",c[i]);
printf("\n");
}
return 0;
}