Skip to content

Instantly share code, notes, and snippets.

@ukitazume
Created December 9, 2011 11:26
Show Gist options
  • Save ukitazume/1451176 to your computer and use it in GitHub Desktop.
Save ukitazume/1451176 to your computer and use it in GitHub Desktop.
#include <stdio.h>
typedef struct Nameval Nameval;
struct Nameval {
char *name;
int value;
};
int lookup(char *word, Nameval st[], int sts) {
int low, hight, mid, cmp;
low = 0;
hight = sts + 1;
while (low <= hight) {
mid = (low + hight) / 2;
cmp = strcmp(word, st[mid].name);
if (cmp < 0)
hight = mid - 1;
else if (cmp > 0)
low = mid + 1;
else
return st[mid].value;
}
return 0;
}
int main (int argc, const char * argv[])
{
Nameval stock[] = {
"Backup", 1,
"Desktop", 2,
"Documents", 3,
"Downloads", 4,
"Dropbox", 5,
"Library", 6,
"Movies", 7,
"Music", 8,
"OF", 9,
"Pankaku", 10,
"Pictures", 11,
"Public", 12,
"Sites", 13,
"bin", 14,
"doc", 15,
"git", 16,
"macports", 17,
"parse", 18,
"pic", 19,
"share", 20,
"tmp", 21,
};
char *target = "git";
int result = lookup(target, stock, 20);
printf("%d", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment