#include <stdio.h>
#include <string.h>
void input_binary_num(char* );
char* binary_add(char* , char* , char* , int );
int main(void)
{
char n1[80], n2[80];
char res1[90];
input_binary_num(n1);
input_binary_num(n2);
printf( "\n\nTotal of addition is %s\n\n", binary_add(n1, n2, &res1[89],90));
return 0;
}
void input_binary_num(char num[])
{
printf("\nEnter a binary number: ");
scanf("%s", num);
}
char* binary_add(char* n1, char* n2, char *res, int size_res)
{
int carry = 0, size1, size2, i, j, t;
size1 = strlen(n1);
size2 = strlen(n2);
*res = '\0';
for(i = size1-1, j = size2-1; i >= 0 && j >= 0 && size_res-- > 0; i--, j--)
{
carry = ( (t = ((n1 - '0') + (n2[j] - '0') + carry)) >> 1);
*(--res) = (t & 1) + '0';
}
if( i >= 0)
while(i >= 0 && size_res-- > 0)
{
carry = ( (t = ( (n1[i--] - '0') + carry )) >> 1);
*(--res) = (t & 1) + '0';
}
else if(j >= 0)
while(j >= 0 && size_res-- > 0)
{
carry = ( (t = ( (n2[j--] - '0') + carry )) >> 1);
*(--res) = (t & 1) + '0';
}
if(carry == 1 && size_res > 0) *(--res) = '1';
return res;
}
сложение в 2-ой сс (С++)
помогите с программой!:confused: