Skip to content

Instantly share code, notes, and snippets.

@boxp
Last active December 18, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxp/5802904 to your computer and use it in GitHub Desktop.
Save boxp/5802904 to your computer and use it in GitHub Desktop.
Cで作ったなんちゃってclojure関数たち
// 文字列版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++;
  }
}
@boxp
Copy link
Author

boxp commented Jun 18, 2013

使い方

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment