补充第三种,虽然我还是不很明白。。。=,=!
(3)树型结构
int main() { int i=1; switch(i) { case 1001:printf("%s","no1");break; case 22:printf("%s","no2");break; case 388:printf("%s","no3");break; case 777:printf("%s","no4");break; case 48:printf("%s","no5");break; case 998:printf("%s","no6");break; case 148:printf("%s","no6");break; case 598:printf("%s","no6");break; default:printf("%s","default"); } return 0; }
每个case的常量表达式没有规律
int main() { 001413A0 push ebp 001413A1 mov ebp,esp 001413A3 sub esp,0D0h 001413A9 push ebx 001413AA push esi 001413AB push edi 001413AC lea edi,[ebp-0D0h] 001413B2 mov ecx,34h 001413B7 mov eax,0CCCCCCCCh 001413BC rep stos dword ptr es:[edi] int i=148; 001413BE mov dword ptr [i],94h switch(i) 001413C5 mov eax,dword ptr [i] 001413C8 mov dword ptr [ebp-0D0h],eax 001413CE cmp dword ptr [ebp-0D0h],250h 001413D8 jg main+92h (141432h) 001413DA cmp dword ptr [ebp-0D0h],250h 001413E4 je main+1A1h (141541h) 001413EA cmp dword ptr [ebp-0D0h],94h 001413F4 jg main+81h (141421h) 001413F6 cmp dword ptr [ebp-0D0h],94h 00141400 je main+183h (141523h) 00141406 cmp dword ptr [ebp-0D0h],16h 0014140D je main+0E4h (141484h) 0014140F cmp dword ptr [ebp-0D0h],30h 00141416 je main+147h (1414E7h) 0014141C jmp main+1BFh (14155Fh) 00141421 cmp dword ptr [ebp-0D0h],184h 0014142B je main+105h (1414A5h) 0014142D jmp main+1BFh (14155Fh) 00141432 cmp dword ptr [ebp-0D0h],309h 0014143C je main+126h (1414C6h) 00141442 cmp dword ptr [ebp-0D0h],3E6h 0014144C je main+165h (141505h) 00141452 cmp dword ptr [ebp-0D0h],3E9h 0014145C je main+0C3h (141463h) 0014145E jmp main+1BFh (14155Fh) { case 1001:printf("%s","no1");break; 00141463 mov esi,esp 00141465 push offset string "no1" (145768h) 0014146A push offset string "%s" (145764h) 0014146F call dword ptr [__imp__printf (1482BCh)] 00141475 add esp,8 00141478 cmp esi,esp 0014147A call @ILT+310(__RTC_CheckEsp) (14113Bh) 0014147F jmp main+1DBh (14157Bh) case 22:printf("%s","no2");break; 00141484 mov esi,esp 00141486 push offset string "no2" (145760h) 0014148B push offset string "%s" (145764h) 00141490 call dword ptr [__imp__printf (1482BCh)] 00141496 add esp,8 00141499 cmp esi,esp 0014149B call @ILT+310(__RTC_CheckEsp) (14113Bh) 001414A0 jmp main+1DBh (14157Bh) case 388:printf("%s","no3");break; 001414A5 mov esi,esp 001414A7 push offset string "no3" (14575Ch) 001414AC push offset string "%s" (145764h) 001414B1 call dword ptr [__imp__printf (1482BCh)] 001414B7 add esp,8 001414BA cmp esi,esp 001414BC call @ILT+310(__RTC_CheckEsp) (14113Bh) 001414C1 jmp main+1DBh (14157Bh) case 777:printf("%s","no4");break; 001414C6 mov esi,esp 001414C8 push offset string "no4" (145758h) 001414CD push offset string "%s" (145764h) 001414D2 call dword ptr [__imp__printf (1482BCh)] 001414D8 add esp,8 001414DB cmp esi,esp 001414DD call @ILT+310(__RTC_CheckEsp) (14113Bh) 001414E2 jmp main+1DBh (14157Bh) case 48:printf("%s","no5");break; 001414E7 mov esi,esp 001414E9 push offset string "no5" (145754h) 001414EE push offset string "%s" (145764h) 001414F3 call dword ptr [__imp__printf (1482BCh)] 001414F9 add esp,8 001414FC cmp esi,esp 001414FE call @ILT+310(__RTC_CheckEsp) (14113Bh) 00141503 jmp main+1DBh (14157Bh) case 998:printf("%s","no6");break; 00141505 mov esi,esp 00141507 push offset string "no6" (145750h) 0014150C push offset string "%s" (145764h) 00141511 call dword ptr [__imp__printf (1482BCh)] 00141517 add esp,8 0014151A cmp esi,esp 0014151C call @ILT+310(__RTC_CheckEsp) (14113Bh) 00141521 jmp main+1DBh (14157Bh) case 148:printf("%s","no7");break; 00141523 mov esi,esp 00141525 push offset string "no7" (14574Ch) 0014152A push offset string "%s" (145764h) 0014152F call dword ptr [__imp__printf (1482BCh)] 00141535 add esp,8 00141538 cmp esi,esp 0014153A call @ILT+310(__RTC_CheckEsp) (14113Bh) 0014153F jmp main+1DBh (14157Bh) case 592:printf("%s","no8");break; 00141541 mov esi,esp 00141543 push offset string "no8" (145748h) 00141548 push offset string "%s" (145764h) 0014154D call dword ptr [__imp__printf (1482BCh)] 00141553 add esp,8 00141556 cmp esi,esp 00141558 call @ILT+310(__RTC_CheckEsp) (14113Bh) 0014155D jmp main+1DBh (14157Bh) default:printf("%s","default"); 0014155F mov esi,esp 00141561 push offset string "default" (14573Ch) 00141566 push offset string "%s" (145764h) 0014156B call dword ptr [__imp__printf (1482BCh)] 00141571 add esp,8 00141574 cmp esi,esp 00141576 call @ILT+310(__RTC_CheckEsp) (14113Bh) } return 0; 0014157B xor eax,eax } 0014157D pop edi 0014157E pop esi 0014157F pop ebx 00141580 add esp,0D0h 00141586 cmp ebp,esp 00141588 call @ILT+310(__RTC_CheckEsp) (14113Bh) 0014158D mov esp,ebp 0014158F pop ebp 00141590 ret
这是二叉树
从001413C5到0014145E是实现过程(我是看花了,没学过数据结构)
树的图片:没做出来,只有草图
程序按照如下顺序遍历搜索这颗二叉树:
1.搜索根节点
2.对左子树结点进行先序遍历搜索
3.对右子树结点进行先序遍历搜索
二叉树搜索是一个递归的过程,因为从一棵二叉树搜索中查找一个元素时,其时间复杂度大致为O(log2n),查找的效率非常高,可见使用二叉树查找比较的机制实现switch是另一种高效的方法。