Last active
December 14, 2015 21:18
-
-
Save sooop/5149932 to your computer and use it in GitHub Desktop.
[C] 큰 자리수의 덧셈
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
void str_rev(char *s); | |
void ltrim(char *s, char c); | |
void rtrim(char *s, char c); | |
void ljust(char *dest, char *source, int length, char c); | |
void rjust(char *dest, char *source, int length, char c); | |
char * stradd(char *s1, char *s2); | |
int main(void) | |
{ | |
char *s1 = "1234543547356756874546562523453457456785678567858456256464567"; | |
char *s2 = "2432565356756756785678354634563456567567856788788563456345612"; | |
char *s = stradd(s1,s2); | |
printf("%s\n", s); | |
free(s); | |
return 0; | |
} | |
void rtrim(char *s, char c) | |
{ | |
str_rev(s); | |
ltrim(s,c); | |
str_rev(s); | |
} | |
void rjust(char *dest, char *source, int length, char c){ | |
if(length==0 || length == NULL){length = strlen(dest);} | |
int i=0, l=strlen(source); | |
for(i=0;i<length;i++) | |
{ | |
if(i<length-l) {dest[i] = c; } | |
else {dest[i] = source[i-(length-l)]; } | |
} | |
} | |
void str_rev(char *s) | |
{ | |
char temp; | |
int i, len = strlen(s); | |
for(i=0;i<len/2;i++) | |
{ | |
temp = *(s+i); | |
*(s+i) = *(s+len-1-i); | |
*(s+len-i-1) = temp; | |
} | |
} | |
void ltrim(char *s, char c) | |
{ | |
int i=0,l=strlen(s); | |
while(i<l && s[i]==c) | |
{ | |
i++; | |
} | |
if(i!=0) { | |
int j=0; | |
while(j<l-i) | |
{ | |
s[j] = s[j+i]; | |
j++; | |
} | |
s[l-i]=0; | |
} | |
} | |
void ljust(char *dest, char *source, int length, char c){ | |
if(length == 0 || length == NULL) {length = strlen(dest); } | |
int i; | |
for(i=0;i<length;i++) | |
{ | |
if(i<strlen(source)) {dest[i] = source[i]; } | |
else {dest[i] = c; } | |
} | |
} | |
char * stradd(char *s1, char *s2){ | |
int maxLen = (strlen(s1)>=strlen(s2) ? strlen(s1) : strlen(s2)) + 1; | |
char *a1 = (char*)calloc(sizeof(char), maxLen+1); | |
char *a2 = (char*)calloc(sizeof(char), maxLen+1); | |
char *result = (char*)calloc(sizeof(char), maxLen+1); | |
rjust(a1, s1, maxLen, '0'); | |
rjust(a2, s2, maxLen, '0'); | |
str_rev(a1); | |
str_rev(a2); | |
int i; | |
char v=0, temp=0; | |
for(i=0;i<maxLen;i++) | |
{ | |
v = (a1[i]-48) + (a2[i]-48) + temp; | |
if(v>9){temp=1; v = v % 10;} | |
else{temp=0;} | |
result[i] = v+48; | |
} | |
result[maxLen] = 0; | |
str_rev(result); | |
ltrim(result,'0'); | |
free(a1); free(a2); | |
return result; | |
} |
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
NSString *mutipliyStrings(NSString *str1, NSString *str2); | |
NSString *stringWithReversedArray(NSArray *reversedArray); | |
NSString *ltrim(NSString *str, unichar ch); | |
NSString *rjust(NSString *str, NSUInteger length, unichar ch); | |
NSString *addStrings(NSString *str1, NSString *str2); | |
NSString *factorial(NSString *str); | |
NSString *rjust(NSString *str, NSUInteger length, unichar ch) | |
{ | |
unichar *prefix = (unichar *)malloc((length - [str length]) * sizeof(unichar)); | |
NSUInteger i=0; | |
while(i<(length - [str length])) { | |
*(prefix+i) = ch; | |
i++; | |
} | |
*(prefix+i) = 0; | |
NSString *result = [NSString stringWithFormat:@"%S%@", prefix, str]; | |
free(prefix); | |
return result; | |
} | |
NSString *ltrim(NSString *str, unichar ch) | |
{ | |
NSUInteger i = 0; | |
while(i < [str length] && [str characterAtIndex:i] == ch ) i++; | |
return [str substringFromIndex:i]; | |
} | |
NSString *stringWithReversedArray(NSArray *reversedArray) | |
{ | |
NSUInteger i; | |
unichar *string = (unichar *)calloc([reversedArray count] , sizeof(unichar)); | |
for(i=0;i<[reversedArray count];i++) { | |
*(string+i) = (unichar)([[reversedArray objectAtIndex:([reversedArray count]-i-1)] charValue] + 48); | |
} | |
// printf("%s\n", string); | |
NSString *result = [NSString stringWithCharacters:string length:[reversedArray count]]; | |
free(string); | |
return result; | |
} | |
NSString *addStrings(NSString *str1, NSString *str2) | |
{ | |
NSUInteger len = ([str1 length] > [str2 length]) ? [str1 length]+1 : [str2 length]+1; | |
NSString *v1 = rjust(str1, len, '0'); | |
NSString *v2 = rjust(str2, len, '0'); | |
NSMutableArray *reversed = [NSMutableArray array]; | |
int i; | |
int a,b,k=0,temp;; | |
for(i=len;i>0;i--) { | |
a = [v1 characterAtIndex:i-1] - 48; | |
b = [v2 characterAtIndex:i-1] - 48; | |
temp = a+b+k; | |
if(temp > 9) { | |
k = 1; | |
temp = temp - 10; | |
}else{ | |
k=0; | |
} | |
[reversed addObject:@(temp)]; | |
} | |
return ltrim(stringWithReversedArray(reversed), '0'); | |
} | |
NSString *mutipliyStrings(NSString *str1, NSString *str2) | |
{ | |
NSMutableArray *rows = [NSMutableArray array]; | |
NSMutableArray *reversedRow = [NSMutableArray array]; | |
NSUInteger rowIndex, colIndex; | |
int a,b,c,temp=0; | |
for(rowIndex=[str1 length];rowIndex > 0;rowIndex--) { | |
[reversedRow removeAllObjects]; | |
int k; | |
for(k=0;k<[str1 length]-rowIndex;k++) { | |
[reversedRow addObject:@(0)]; | |
} | |
a = [str1 characterAtIndex:rowIndex-1] - 48; | |
for(colIndex=[str2 length];colIndex>0;colIndex--) { | |
b = [str2 characterAtIndex:colIndex-1] - 48; | |
c = a*b+temp; | |
if(c > 9) { | |
temp = (c / 10); | |
c = c - temp*10; | |
} else { | |
temp = 0; | |
} | |
[reversedRow addObject:@(c)]; | |
} | |
if(temp) { | |
if(temp > 9) { | |
[reversedRow addObject:@(floor(temp % 10))]; | |
[reversedRow addObject:@(floor(temp/10))]; | |
} | |
else { | |
[reversedRow addObject:@(temp)]; | |
} | |
} | |
// NSLog(@"%@", reversedRow); | |
[rows addObject:ltrim(stringWithReversedArray(reversedRow),'0')]; | |
} | |
NSString *allSum = @"0"; | |
for(NSString *row in rows) { | |
// NSLog(@"%@",row); | |
allSum = addStrings(row, allSum); | |
} | |
[rows removeAllObjects]; | |
return allSum; | |
} | |
NSString *factorial(NSString *str) | |
{ | |
int k = [str intValue]; | |
NSString *f = [@"1" retain]; | |
while(k>1) { | |
NSString *kstring = [NSString stringWithFormat:@"%d", k]; | |
NSLog(@"%@ X %@",f, kstring); | |
f = mutipliyStrings(f, kstring); | |
k--; | |
} | |
return f; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
@autoreleasepool { | |
// NSLog(@"%@", mutipliyStrings(@"361829371852383189442705860000", @"85")); | |
NSLog(@"%@", factorial(@"100")); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
calloc
의 사용법을 잘 못 알고 있었다.과 같이 써야 하며, 이 때 할당되는 메모리 크기는 두 인자의 곱이다.