ビッグマックセット
C言語
#include <stdio.h> #include <string.h> #include <stdlib.h> void tr(char *src, char *cmp, char *str) { char *tmp, *index; int p_tmp, p_src; int len_before, len_after; index = strstr(src, cmp); if (!index) return; len_before = index - src; len_after = strlen(src) - len_before - strlen(cmp); tmp = malloc(len_before + strlen(str) + len_after + 1); p_tmp = p_src = 0; strncpy(tmp + p_tmp, src + p_src, len_before); p_tmp += len_before, p_src += len_before; strncpy(tmp + p_tmp, str, strlen(str)); p_tmp += strlen(str), p_src += strlen(cmp); strncpy(tmp + p_tmp, src + p_src, len_after + 1); strncpy(src, tmp, len_before + strlen(str) + len_after + 1); free(tmp); /* ぎゃーす */ } int main(void) { int i; char context[10240] = "ビッグマックセット"; for (i=0;i<100;i++) { printf("--- step %d ---\n", i+1); tr(context, "ビッグマックセット", "(ビッグマックセット + ポテトM + ドリンクM)"); printf("%s\n", context); } return 0; }
結果
--- step 1 --- (ビッグマックセット + ポテトM + ドリンクM) --- step 2 --- ((ビッグマックセット + ポテトM + ドリンクM) + ポテトM + ドリンクM) --- step 3 --- (((ビッグマックセット + ポテトM + ドリンクM) + ポテトM + ドリンクM) + ポテトM + ドリンクM) 以下略
系統分け
・ビッグマックセットは常識的に考えてビッグマック+ドリンク+ポテトだよ派 (クーポンの内容はビッグマックセットの定義じゃないよ派) ・ビッグマックセットはビッグマックセット+ドリンク+ポテトだよ派 (クーポンの内容がビッグマックセットの定義だよ派) ・ビッグマックセットの定義自体が次々に書き換わるよ派 (自己書き換え派)
ここでは真ん中の説を採用しています
反省
置換はもっとスマートに書けるんじゃね
めもりりーくこわい