关于两个16进制字符串的加法
题目要求是输入两个最大位数为255位的16进制字符串然后求和并以字符串形式输出16进制结果以下是我的部分代码
跑出来有问题 有大佬帮忙解决一下吗#include <stdio.h>
#include <string.h>
int main()
{
char hex1, hex2, hexsum, hex; unsigned int m,n,k,x = 0,i,j,num;
printf("hexadecimal number 1: ");
scanf("%s", hex1);
printf("hexadecimal number 2: ");
scanf("%s", hex2);
m = strlen(hex1); n = strlen(hex2);//m是hex1的长度 n 是hex2的长度
k = (m > n) ? m:n;//k是 m n中大的那一个数字
if(m > n)//分类讨论 m 和 n谁大,我现在只写了一种情况 就是m更大的时候
{
for (i = m-1, j = n-1; j >= 0; j--,i--)//
{ //将 hex1 中的字母先转化为数字
if (hex1 == 'A') { hex1 = 10; }
if (hex1 == 'B') { hex1 = 11; }
if (hex1 == 'C') { hex1 = 12; }
if (hex1 == 'D') { hex1 = 13; }
if (hex1 == 'E') { hex1 = 14; }
if (hex1 == 'F') { hex1 = 15; }
//将 hex2中的字母转化为数字
if (hex2 == 'A') { hex2 = 10; }
if (hex2 == 'B') { hex2 = 11; }
if (hex2 == 'C') { hex2 = 12; }
if (hex2 == 'D') { hex2 = 13; }
if (hex2 == 'E') { hex2 = 14; }
if (hex2 == 'F') { hex2 = 15; }
//将每一位逐个加起来
num = hex1 + hex2 + x;
if (num > 15)
{
x = 1;//x代表进1 ,如果一位数字加起来超过15 则进1 并且该数字-16
num = num - 16;
}
else
{x = 0;}
if(num == 1){hex = '1';}
if(num == 2){hex = '2';}
if(num == 3){hex = '3';}
if(num == 4){hex = '4';}
if(num == 5){hex = '5';}
if(num == 6){hex = '6';}
if(num == 7){hex = '7';}
if(num == 8){hex = '8';}
if(num == 9){hex = '9';}
if(num == 10){hex = 'A';}
if(num == 11){hex = 'B';}
if(num == 12){hex = 'C';}
if(num == 13){hex = 'D';}
if(num == 14){hex = 'E';}
if(num == 15){hex = 'F';}
hexsum = hex;//将结果存入hexsum 也就是计算结果
}
for(i = m - n - 1;i >= 0;i--)//这一块是hex1中位数超过hex2的数字就可以直接判断是否要进一还是直接写入结果
{
if (hex1 == 'A') { hex1 = 10; }
if (hex1 == 'B') { hex1 = 11; }
if (hex1 == 'C') { hex1 = 12; }
if (hex1 == 'D') { hex1 = 13; }
if (hex1 == 'E') { hex1 = 14; }
if (hex1 == 'F') { hex1 = 15; }
num = hex1 + x;
if (num > 16) {num = num - 16;x = x + 1;}
else
x = 0;
if(num == 1){hex = '1';}
if(num == 2){hex = '2';}
if(num == 3){hex = '3';}
if(num == 4){hex = '4';}
if(num == 5){hex = '5';}
if(num == 6){hex = '6';}
if(num == 7){hex = '7';}
if(num == 8){hex = '8';}
if(num == 9){hex = '9';}
if(num == 10){hex = 'A';}
if(num == 11){hex = 'B';}
if(num == 12){hex = 'C';}
if(num == 13){hex = 'D';}
if(num == 14){hex = 'E';}
if(num == 15){hex = 'F';}
hexsum = hex;
}
}
printf("Their sum is %s\n",hexsum); // hexsum is the sum of hex1 and hex2 )
return 0;
}
本帖最后由 xieglt 于 2020-11-2 16:17 编辑
看看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//判定字符是否16进制数的宏
#define IS_HEX(c) ((c>='0'&&c<='9')||(c>='a'&&c<='f')||(c>='A'&&c<='F'))
//将小写字符转变为大写字符
#define LCase2UCase(c) ((c>='a' && c<='z') ? (c-32) : c)
//将ascii字符转化为16进制数
#define ASCII2HEX(c) ((c-0X30)>9 ? (c-0X37) : (c-0X30))
//将16进制数转化为ascii字符
#define HEX2ASCII(c) ((c+0X30)>0X39 ? (c+0x37) : (c+0X30))
int main()
{
int m,n;
int i,max;
char Hex1 = {0};
char Hex2 = {0};
char Hex3 = {0};
char *p = Hex3+1;
char carry = 0;
char value = 0;
printf("hexadecimal number 1: ");
scanf("%s", Hex1);
printf("hexadecimal number 2: ");
scanf("%s", Hex2);
m = strlen(Hex1);
n = strlen(Hex2);
//判定输入Hex1是否16进制数
for(i=0 ; i<m ; i++)
{
if(!IS_HEX(Hex1))
{
printf("%s is not a hexadecimal number!",Hex1);
return 0;
}
}
//判定输入Hex2是否16进制数
for(i=0 ; i<m ; i++)
{
if(!IS_HEX(Hex2))
{
printf("%s is not a hexadecimal number!",Hex2);
return 0;
}
}
//将两个数对齐
if(m > n)
{
//Hex2位数少则对齐 Hex2
memcpy(Hex2+(m-n),Hex2,n);
//前面置字符'0'
memset(Hex2,0X30,m-n);
}
else if(m < n)
{
//Hex1位数少则对齐 Hex1
memcpy(Hex1+(n-m),Hex1,m);
//前面置字符'0'
memset(Hex1,0X30,n-m);
}
max = m > n ? m : n;
for(i=max-1 ; i >=0 ; i--)
{
//取得进位
value = carry;
//+数1
value += ASCII2HEX(LCase2UCase(Hex1));
//+数2
value += ASCII2HEX(LCase2UCase(Hex2));
//计算是否进位
carry = value/16;
//计算结果
value %= 16;
//转化为ASCII码保存
Hex3 = HEX2ASCII(value);
}
//如果还有进位,则保存进位
if(carry !=0)
{
Hex3 = HEX2ASCII(carry);
p = Hex3;
}
printf("%s\n",p);
return 0;
} xieglt 发表于 2020-11-2 15:10
看看
谢谢大佬 我试试
页:
[1]