/* "由函數所構成的陣列" (function dispatch table) 範例. */ #include #include #include "sitio.h" int row=1, col=1; void move_left(void) { --col; if (col <= 0) col += 70; } void move_down(void) { ++row; if (row > 20) row -= 20; } void move_up(void) { --row; if (row <= 0) row += 20; } void move_right(void) { ++col; if (col > 70) col -= 70; } int main(void) { int ch; char * p; char const valid_keys[] = "hjkl"; void (* dispatch[])(void) = { move_left, move_down, move_up, move_right }; beginsitio(); clearscr(); gotorc(23, 1); printf("use hjkl to move the cursor, any other key to quit"); do { gotorc(row, col); ch = getkey(); if (ch == EOF) break; p = strchr(valid_keys, ch); if (p) (*dispatch[p - valid_keys])(); } while (p); clearscr(); gotorc(24, 1); endsitio(); return 0; }