Last active
December 18, 2015 15:18
-
-
Save boxp/5802904 to your computer and use it in GitHub Desktop.
Cで作ったなんちゃってclojure関数たち
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
// 文字列版take | |
void take_char(int n, char* string, char* substring) { | |
int i; | |
for (i = 0; i < n; i++) { | |
substring[i] = string[i]; | |
} | |
substring[i+1] = '\0'; | |
} | |
// 整数版reduce | |
int reduce_int(char operator,const int num[],const int count,const int result){ | |
switch (operator) { | |
case '+': | |
if (count == 0) { | |
return result + num[count]; | |
} else { | |
reduce_int('+',num,(count-1),(result + num[count])); | |
} | |
break; | |
case '*': | |
if (count == 0) { | |
return result * num[count]; | |
} else { | |
reduce_int('*',num,(count-1),(result * num[count])); | |
} | |
break; | |
} | |
} | |
// 文字列版length | |
int length_char(char* str) { | |
int i = 0; | |
while (str[i] != '\0') { | |
i++; | |
} | |
return (i+1); | |
} | |
// 文字列版drop | |
void drop_char(const int count, char* str) { | |
int i = 0; | |
while (str[i] != '\0') { | |
str[i] = str[i+count]; | |
i++; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使い方
char str[20];
take_char(5,"akameco",str);
print("%s\n",str); // akame
int fascination[3] = {100,200,400};
printf("%d\n",reduce_int('+',fascination,3,0)); // 700
printf("%d\n",reduce_int('*',fascination,3,0)); // 8000000