一、原题
原题链接Binary Addition
mplement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition.
The binary number returned should be a string.
Examples:(Input1, Input2 --> Output (explanation)))
1, 1 --> "10" (1 + 1 = 2 in decimal or 10 in binary)
5, 9 --> "1110" (5 + 9 = 14 in decimal or 1110 in binary)
二、解题
一、分析
1、所求出是整形二进制0或1,但题中存储类型是char *,直接存入结果会变成奇异字符;采用结合ASCII解决此问题,0的ASCII为48,1为1+48,故有:(否则会有下面错误)
b=b+48;//结果与ASCII对应,与题目存储结构相同,避免显示错误
*item =(char) b;
2、int,unsigned,long,long long,存储范围不同, 题中有组测试数据超出题目所给的unsigned的存储范围,致使结果错误,故需要换存储范围更大的。
1)unsigned结果:
2)long long结果:
long long sum,a1,b1;/*int,unsigned,long,long long,都有存储范围, 题中有组测试数据超出题目所给的unsigned的存储范围,致使结果错误,故需要换存储范围更大的。*/
3细小问题:
1)本题通过指针函数返回binary值,是否可以通过动态申请空间,存储相关值?
char * item=(char *)malloc(sizeof(char)*50);
/*动态申请空间,用于配合char* fan,存储所获得二进制*/
2)存储过程中,会用到地址++的操作,可通过“另存”地址便于返回。
it=binary;//另存初始地址,便于返回
.......
.......
return it;//返回初始地址
3)因fan()函数,使传入的地址循环结束使白白++一次却不存入任何东西,故其返回值需要--操作,才能正常使用。
s=fan(sum,item);
s--;//因fan()问题需s--;
4)*binary = '\0';进行封尾
5)此题是否需要区分a+b结果是否为零,取决于自己的转二进制的方式。(因为本人使用的是while(a>0),故区分好一些。)
if(sum==0){//单独摘出a+b结果为零的情况
*binary = '0';
binary++;
*binary = '\0';
}
6)本人通过fan()函数,获取的地址指向尾部,因此逆向读取,通过--操作,将值存入birnay中,其顺序刚好与结果相同。(while(*s)使有效值都能读取)
while(*s){
*binary = *s;/*本人通过fan()函数,获取的地址指向尾部,因此逆向读取,通过--操作,将值存入birnay中,其顺序刚好与结果相同。*/
binary++;
s--;
}
二、Myway
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* fan(long long a,char * item){
int b;
while(a>0){
b=a%2;
b=b+48;//结果与ASCII对应,与题目存储结构相同,避免显示错误
*item =(char) b;
item++;
a=a/2;
}
return item;
}
char *binary_add (unsigned a, unsigned b, char *binary)
{
// write to the binary string and return it
char * item=(char *)malloc(sizeof(char)*50);
/*动态申请空间,用于配合char* fan,存储所获得二进制*/
char * it,*s;
it=binary;//另存初始地址,便于返回
long long sum,a1,b1;/*int,unsigned,long,long long,都有存储范围, 题中有组测试数据超出题目所给的unsigned的存储范围,致使结果错误,故需要换存储范围更大的。*/
a1=a;
b1=b;
sum=a1+b1;
printf("%lld",sum);
if(sum==0){//单独摘出a+b结果为零的情况
*binary = '0';
binary++;
*binary = '\0';
}
else{
s=fan(sum,item);
s--;//因fan()问题需s--;
while(*s){
*binary = *s;/*本人通过fan()函数,获取的地址指向尾部,因此逆向读取,通过--操作,将值存入birnay中,其顺序刚好与结果相同。*/
binary++;
s--;
}
*binary = '\0';
}
return it;//返回初始地址
}
三、总结
1、存储范围
int -2147483648~2147483647 unsigned int 0~4294967295 |
参考链接:int、long int 和 long long int 的取值范围
unsigned int (unsigned long) 4字节8位可表达位数:2^32=42 9496 7296 int (long) 4字节8位可表达位数:2^32=42 9496 7296 long long (__int64) 8字节8位可表达位数:2^64=1844 6744 0737 0960 0000 |
2、全局变量与局部变量
问题源于程序编写时的错误思路:我在外部定一个指针,后将申请的动态地址给他。写了一个fan()函数,对其进行操作,没讲地址传入,没报错,但是结果不对(就是因为局部变量无法通过常规方法将值传给同名的全局变量)
1、局部变量的生命周期只在函数内部,如果出了函数,局部变量就会销毁
2、为了避免数据的混淆,通常时不建议局部变量和全局变量重名的, 清晰的变量名,可以反应对应的意义。
1) main()中定义的函数,与fan()中定义的虽然重名,但由于fan()中item随fan()调用结束而结束,故无法对main()中item产生变化,可将fan()变为有返回值的函数解决。
2) 以下是错误思路的错误代码,错误根源与上面类似。主要在于fan()没有传入地址,没有返回。正确方法应是
char* fan(long long a,char * item){//正确
int b;
while(a>0){
b=a%2;
b=b+48;//结果与ASCII对应,与题目存储结构相同,避免显示错误
*item =(char) b;
item++;
a=a/2;
}
return item;
}
char * fan(long long a){//错误
int b;
while(a>0){
b=a%2;
b=b+48;
*item =(char) b;
item++;
a=a/2;
}
return item;
}
错误代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char * item;
char * fan(long long a){
int b;
while(a>0){
b=a%2;
b=b+48;
*item =(char) b;
item++;
a=a/2;
}
return item;
}
char *binary_add (unsigned a, unsigned b, char *binary)
{
// write to the binary string and return it
char * item=(char *)malloc(sizeof(char)*50);
char * it,*s;
it=binary;
long long sum,a1,b1;
a1=a;
b1=b;
sum=a1+b1;
printf("%lld",sum);
if(sum==0){
*binary = '0';
binary++;
*binary = '\0';
}
else{
item--;
s=item;
while(*s){
*binary = *s;
binary++;
s--;
}
*binary = '\0';
}
return it;
}