概述
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
char* addBinary(char* a, char* b)
{
int n1,n2,i,j,k,fab=0,g;
char* c,*d;
n1=strlen(a);
n2=strlen(b);
if(n1>=n2)
{
g=k=n1-1;
d=(char*)malloc(k+2);
strcpy(d,a);
}
else if(n1<n2)
{
g=k=n2-1;
d=(char*)malloc(k+1);
strcpy(d,b);
}
c=(char*)malloc(k+2);
for(i=n1-1,j=n2-1;i>=0&&j>=0;i--,j--,k--)
{
if((a[i]-'0')+(b[j]-'0')+fab==0||(a[i]-'0')+(b[j]-'0')+fab==1)
{
c[k]=a[i]+b[j]+fab-'0';
fab=0;
}
else if((a[i]-'0')+(b[j]-'0')+fab==2)
{
c[k]='0';
fab=1;
}
else if((a[i]-'0')+(b[j]-'0')+fab==3)
{
c[k]='1';
fab=1;
}
}
for(;k>=0;k--)
{
c[k]=d[k]+fab;
if(c[k]=='2')
{
c[k]='0';
fab=1;
}
else fab=0;
}
if(fab==1)
{
for(i=g;i>=0;i--)
{
c[i+1]=c[i];
}
c[0]='1';
}
for(i=0;i<=g+1;i++)
{
if(c[i]!='0'&&c[i]!='1')
{
c[i]='