forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
1b5e876
commit df9f4a3
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test multiply_strings.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
static void reverse(char *s, int len) | ||
{ | ||
int low = 0; | ||
int high = len - 1; | ||
while (low < high) { | ||
char c = s[low]; | ||
s[low] = s[high]; | ||
s[high] = c; | ||
low++; | ||
high--; | ||
} | ||
} | ||
|
||
static char* multiply(char* num1, char* num2) { | ||
if (*num1 == '\0') { | ||
return num1; | ||
} | ||
if (*num2 == '\0') { | ||
return num2; | ||
} | ||
|
||
int i, j; | ||
char *result = malloc(110 + 110); | ||
memset(result, '0', 220); | ||
int len1 = strlen(num1); | ||
int len2 = strlen(num2); | ||
reverse(num1, len1); | ||
reverse(num2, len2); | ||
for (i = 0; i < len1; i++) { | ||
int carry = 0; | ||
for (j = 0; j < len2; j++) { | ||
carry += (num1[i] - '0') * (num2[j] - '0') + (result[i + j] - '0'); | ||
result[i + j] = carry % 10 + '0'; | ||
carry /= 10; | ||
} | ||
if (carry != 0) { | ||
result[len2 + i] = carry + '0'; | ||
} | ||
} | ||
int len = 220; | ||
while (--len >= 0) { | ||
if (result[len] > '0') { | ||
result[++len] = '\0'; | ||
break; | ||
} | ||
} | ||
if (len == -1) { | ||
len = 1; | ||
result[len] = '\0'; | ||
} | ||
reverse(result, len); | ||
return result; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 3) { | ||
fprintf(stderr, "Usage: ./test m1 m2\n"); | ||
exit(-1); | ||
} | ||
printf("%s\n", multiply(argv[1], argv[2])); | ||
return 0; | ||
} |