C程序 二进制字符串求和
给定两个二进制字符串,我们必须以二进制形式返回其总和。
方法: 我们将从两个字符串的最后一个开始,按照二进制加法进行加法,如果我们得到任何携带,我们将把它加到下一个数字。
输入:
11 + 11
输出:
110
// C Program to Add 2 Binary Strings
// and Print their Binary Sum
#include <stdio.h>
#include <string.h>
// Function to add two Binary Strings
void sum(char b1[], char b2[], int l1, int l2)
{
int carry = 0, temp, num1, num2, i;
char result[100];
result[l1 + 1] = '\0';
// This loop will add Both Strings till
// second have characters in it.
while (l2 > 0) {
num1 = b1[l1 - 1] - '0';
num2 = b2[l2 - 1] - '0';
temp = num1 + num2 + carry;
if (temp >= 2) {
carry = 1;
temp = temp % 2;
}
result[l1] = temp + '0';
l1--;
l2--;
}
// This loop will add leftover
// characters of first Strings.
while (l1 - 1 >= 0) {
temp = b1[l1 - 1] + carry - '0';
if (temp >= 2) {
carry = 1;
temp = temp % 2;
}
result[l1] = temp + '0';
l1--;
}
// Add last carry to result string
if (carry) {
result[0] = '1';
}
else {
// if there is no carry then we will shift
// each character by one place in left side.
for (i = 0; i < strlen(result) - 1; i++) {
result[i] = result[i + 1];
}
result[strlen(result) - 1] = '\0';
}
// printing result
printf("%s + %s = %s\n", b1, b2, result);
}
// Driver code
int main()
{
char b1[100] = "11", b2[100] = "11";
int l1, l2;
printf("Enter binary number 1: ");
printf("%s \n", b1);
printf("Enter binary number 2: ");
printf("%s \n", b2);
l1 = strlen(b1);
l2 = strlen(b2);
// calling function to add strings
if (l1 > l2) {
sum(b1, b2, l1, l2);
}
else {
sum(b2, b1, l2, l1);
}
return 0;
}
输出
Enter binary number 1: 11
Enter binary number 2: 11
11 + 11 = 110
使用while循环
#include <stdio.h>
int main() {
long binary1=10000, binary2=10000;
int i = 0, remainder = 0, sum[20];
while (binary1 != 0 || binary2 != 0)
{
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0)
sum[i++] = remainder;
--i;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%d", sum[i--]);
return 0;
}
输出
Sum of two binary numbers: 100000