Two issues to be faced with table-driven methods:
1. How do I access a table? The options are direct access, index access, and ladder access.
2. What is stored in the table? If you want to get the data, put the data in the table, and if you want to get an action, put the function pointer in the table.
Example 1: Given a score rating, to ensure that the score range within 0-100, the 90-100-grade a,80-89 for b,70-79 to c,60-69 for the d,0-59 E.
The function prototype is: Char getgrade (int score);
General Practice
1 CharGetgrade (intscore)2 {3 if(Score >= -)return 'A';4 if(Score >= the)return 'B';5 if(Score >= -)return 'C';6 if(Score >= -)return 'D';7 return 'E';8}
Using table-driven methods
1 structMark2 {3 intLower//fractional interval Lower bound4 intGrade//score corresponding level5 };6 7 Const structMarktable[] =8 {9{ -,'A'},Ten{ the,'B'}, One{ -,'C'}, A{ -,'D'}, -{0,'E'}, - }; the - intGetIndex (intscore) - { - intIDX =0; + while(Score <table[idx].lower) -idx++; + returnidx; A } at - CharGetgrade (intscore) - { - intIDX =GetIndex (score); - returnMarktable[idx].grade; -}
Look at the table above the linear lookup, if the table data is more, you can consider using binary search, but only if the table is ordered.
Example 2: A system provides multiple authentication methods for verifying user identity validity, such as static password, fingerprint, dynamic token, LDAP, radius, etc. Now, in order to improve the security of the system, it is necessary to extend the two-factor authentication on the basis of the original authentication, that is, the choice of two different combinations of authentication methods, when and only if both authentication methods are verified to be valid.
Description: Authentication type authtype is type int, from low to high to represent static password, fingerprint, dynamic token, LDAP, RADIUS authentication method, such as 4 for dynamic token authentication, 9 for LDAP and static password authentication.
The input data format is:
General Certification: username password
Two-factor authentication: Username Password1 password2, where Password1 and password2 order do not require.
A variety of certified function prototypes are listed below, specific certification process slightly.
1 intAuthbypwd (Const Char*user,Const Char*pass);2 intAuthbyfingerprint (Const Char*user,Const Char*pass);3 intAuthbytoken (Const Char*user,Const Char*pass);4 intAUTHBYLDAP (Const Char*user,Const Char*pass);5 intAuthbyradius (Const Char*user,Const Char*pass);
Two-factor authentication using table-driven methods
1typedefint(*authfunc) (Const Char*,Const Char*);2 3 intOffset (unsigned n)4 {5 intoffset;6 7 for(offset =0; N N >>=1, offset++);8 returnoffset;9 }Ten One intMain () A { - Const StaticAuthfunc authfunc[] = { - NULL, the Authbypwd, - Authbyfingerprint, - Authbytoken, - Authbyldap, + Authbyradius, - }; + intAuthType, ret, idx[2] = {0}; A Charuser[ +], pass1[ +], pass2[ +]; at - while(~SCANF ("%d", &authtype)) { - if(AuthType & (AuthType-1)) { -scanf"%s%s%s", user, Pass1, pass2); -idx[0] = Offset (AuthType &-authtype); -AuthType &= AuthType-1; inidx[1] = Offset (AuthType &-authtype); -ret = (authfunc[idx[0]] (user, pass1) && authfunc[idx[1]] (user, pass2)) to|| (authfunc[idx[0]] (user, pass2) && authfunc[idx[1]] (user, pass1)); +}Else { -scanf"%s%s", user, pass1); theidx[0] = Offset (AuthType &-authtype); *RET = authfunc[idx[0]] (user, pass1); $ }Panax Notoginsengprintf"Auth%s\n"Ret?"Success":"fail"); - } the return 0; +}
Example of table-driven method