轉自:https://blog.csdn.net/morixinguan/article/details/51799668 作者:Engineer-Bruce_Yang
就像下面的這個表
之前寫過上面這個標題的一篇文章,講的是以位移的方式去遍歷表中的數據,效率非常高,但是,如果要實現一個亂序的流水燈或者跑馬燈的話,思考一個這樣的算法是不可取的,很費時間,也很費腦力,於是,今天就說一說查表法,如果在程序中運用查表法,不論多么復雜的程序,只要符合一張表,那都可以實現,非常簡單,體力活而已,接下來看看下面這個程序,對上面這個進行操作吧。
#include <stdio.h> #include <windows.h> //這里的行可以自由寫,這樣就不受限制,想做出什么樣的效果都可以。 int array[][10] = { 0x03,0x00,0x00,0x00,0x00,//第一列 0x00,0x02,0x00,0x00,0x00, 0x0C,0x00,0x00,0x00,0x00,//第二列 0x00,0x04,0x00,0x00,0x00, 0x10,0x00,0x00,0x00,0x00,//第三列 0x00,0x08,0x00,0x00,0x00, 0x60,0x00,0x00,0x00,0x00,//第四列 0x00,0x10,0x00,0x00,0x00, 0x80,0x01,0x00,0x00,0x00,//第五列 0x00,0x20,0x00,0x00,0x00, 0xAA,0x55,0x00,0x00,0xC0,//end 0x00,0x00,0x00,0x00,0x00, }; void to_Q112_cmd_designator_LED(int *array) { int i; for(i = 0; i < 10; i++) { printf(" %3d ", *(array+i)); } printf("\n"); } void delay_500ms(void) { Sleep(500); } int main(void) { int i,j; int tick; int count = 0; while(array[count][0] != 0xAA || array[count][1] != 0x55)//如果當數組第count行第0列等於0xAA,或者第count行第1列等於0x55時,那么就退出,否則就循環執行遍歷數據 { to_Q112_cmd_designator_LED((int *)(&array[0][0]+count*10) );//以首元素每次向后偏移10個字節 delay_500ms(); count++; } return 0; }
運行結果:
3 0 0 0 0 0 2 0 0 0
12 0 0 0 0 0 4 0 0 0
16 0 0 0 0 0 8 0 0 0
96 0 0 0 0 0 16 0 0 0
128 1 0 0 0 0 32 0 0 0