Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f5b3995

Browse files
Binary calculation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent d7b492a commit f5b3995

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

066_plus_one/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test plus_one.c

066_plus_one/plus_one.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
/**
6+
** Return an array of size *returnSize.
7+
** Note: The returned array must be malloced, assume caller calls free().
8+
**/
9+
static int* plusOne(int* digits, int digitsSize, int* returnSize)
10+
{
11+
int i, j, len = 0, carry = 1;
12+
int *result = malloc((digitsSize + 1) * sizeof(int));
13+
for (i = digitsSize - 1; i >= 0 || carry; i--) {
14+
int n = digits[i] + carry;
15+
result[len++] = n % 10;
16+
carry = n / 10;
17+
}
18+
19+
for (i = 0, j = len - 1; i < j; i++, j--) {
20+
int tmp = result[i];
21+
result[i] = result[j];
22+
result[j] = tmp;
23+
}
24+
25+
*returnSize = len;
26+
return result;
27+
}
28+
29+
int main(int argc, char **argv)
30+
{
31+
if (argc != 2) {
32+
fprintf(stderr, "Usage: ./test num\n");
33+
exit(-1);
34+
}
35+
36+
int i, count = strlen(argv[1]);
37+
int *digits = malloc(count * sizeof(int));
38+
for (i = 0; i < count; i++) {
39+
digits[i] = argv[1][i] - '0';
40+
}
41+
42+
int len = 0;
43+
int *res = plusOne(digits, count, &len);
44+
for (i = 0; i < len; i++) {
45+
printf("%c", res[i] + '0');
46+
}
47+
printf("\n");
48+
return 0;
49+
}

067_add_binary/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test add_binary.c

067_add_binary/add_binary.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
static char* addBinary(char* a, char* b)
6+
{
7+
int len1 = strlen(a);
8+
int len2 = strlen(b);
9+
int len = len1 > len2 ? len1 + 1 : len2 + 1;
10+
char *result = malloc(len + 1);
11+
result[len] = '\0';
12+
result[len - 1] = '\0';
13+
14+
int i, j, carry = 0;
15+
len = 0;
16+
for (i = len1 - 1, j = len2 - 1; carry || i >= 0 || j >= 0; i--, j--) {
17+
int na = i >= 0 ? a[i] - '0' : 0;
18+
int nb = j >= 0 ? b[j] - '0' : 0;
19+
result[len++] = (na ^ nb ^ carry) + '0';
20+
carry = carry + na + nb >= 2 ? 1 : 0;
21+
}
22+
23+
for (i = 0, j = len - 1; i < j; i++, j--) {
24+
char c = result[i];
25+
result[i] = result[j];
26+
result[j] = c;
27+
}
28+
29+
return result;
30+
}
31+
32+
int main(int argc, char **argv)
33+
{
34+
if (argc != 3) {
35+
fprintf(stderr, "Usage: ./test s1 s2\n");
36+
exit(-1);
37+
}
38+
printf("%s\n", addBinary(argv[1], argv[2]));
39+
return 0;
40+
}

0 commit comments

Comments
 (0)