sttsのソースコードMemoブログ

色々やってみた結果のMemo

C言語でバイナリサーチ

C言語でバイナリサーチをするサンプルです。

#include 
#include 
#include 

static int cmp(const void *p1, const void *p2) {
	char *c1;
	char *c2;

	c1 = *(char **)p1;
	c2 = *(char **)p2;
	//printf("cmp %s - %s\n", c1, c2);
	return strcmp(c1, c2);
}
int main (int argc, char *argv[]) {

	int number = 0;
	char *list[4096];
	char buf[4096];
	FILE *fp = NULL;
	char **ret;
	int i;

	fp = fopen(argv[1], "r");
	while ( fgets(buf, sizeof(buf), fp) != NULL) {
		list[number] = strdup(buf);
		number++;
	}
	fclose(fp);
	
	qsort(list, number, sizeof(char *), cmp);

	if (argc == 3) {
		ret = bsearch(&(argv[2]), list, number, sizeof(char *), cmp);
		if (ret != NULL) {
			printf("find! %s\n", *ret);
			exit(1);
		}
	}
		
	for (i=0; i<number; i++) {
		printf("%s", list[i]);
	}
}

以下のように実行します。

$ ./a.out ソート対象の入力ファイル 検索対象の文字列

もっと高級言語を使いたい。。。

zshrcのメモ

バックアップもかねてzshrc

# Enable compsys completion.
autoload -U compinit
compinit
zstyle ':completion:*:default' menu select=1 # Menu like emacs.
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}  # color completion.

bindkey -e # keybind is emacs.

# Prompt
setopt prompt_subst
PROMPT='%{%(?.$fg[green].$fg[red])%}[%n@%m]%# %{$reset_color%}' # left prompt
RPROMPT='[%~]'
autoload -U colors
colors

# History
HISTFILE=$HOME/.zsh-history
HISTSIZE=10000
SAVEHIST=10000
setopt extended_history # save command time.
setopt share_history    # share history in terms.
setopt append_history   # append
function history-all { history -E 1 }

# Beep
setopt no_beep

# cd
setopt auto_pushd
setopt pushd_ignore_dups

# PATH
# For homebrew
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
export MANPATH=/usr/local/man:$MANPATH