7.1 »Øµ÷º¯Êý
»Øµ÷º¯Êý¾ÍÊÇÒ»¸öͨ¹ýº¯ÊýÖ¸Õëµ÷Óõĺ¯Êý¡£Èç¹ûÄã°Ñº¯ÊýµÄÖ¸Õ루µØÖ·£©×÷Ϊ²ÎÊý´«µÝ¸øÁíÒ»¸öº¯Êý£¬µ±Õâ¸öÖ¸Õë±»ÓÃÀ´µ÷ÓÃÆäËùÖ¸ÏòµÄº¯Êýʱ£¬ÎÒÃǾÍ˵ÕâÊǻص÷º¯Êý¡£»Øµ÷º¯Êý²»ÊÇÓɸú¯ÊýµÄʵÏÖ·½Ö±½Óµ÷Ó㬶øÊÇÔÚÌض¨µÄʼþ»òÌõ¼þ·¢ÉúʱÓÉÁíÍâµÄÒ»·½µ÷Óõģ¬ÓÃÓÚ¶Ô¸Ãʼþ»òÌõ¼þ½øÐÐÏìÓ¦¡£
7.1.1 »úÖÆ
¢Å¶¨ÒåÒ»¸ö»Øµ÷º¯Êý£»
¢ÆÌṩº¯ÊýʵÏÖµÄÒ»·½ÔÚ³õʼ»¯µÄʱºò£¬½«»Øµ÷º¯ÊýµÄº¯ÊýÖ¸Õë×¢²á¸øµ÷ÓÃÕߣ»
¢Çµ±Ìض¨µÄʼþ»òÌõ¼þ·¢ÉúµÄʱºò£¬µ÷ÓÃÕßʹÓú¯ÊýÖ¸Õëµ÷Óûص÷º¯Êý¶Ôʼþ½øÐд¦Àí¡£
7.1.2 ÒâÒå
ÒòΪ¿ÉÒ԰ѵ÷ÓÃÕßÓë±»µ÷ÓÃÕß·Ö¿ª£¬ËùÒÔµ÷ÓÃÕß²»¹ØÐÄËÊDZ»µ÷ÓÃÕß¡£ËüÖ»ÐèÖªµÀ´æÔÚÒ»¸ö¾ßÓÐÌض¨ÔÐͺÍÏÞÖÆÌõ¼þµÄ±»µ÷Óú¯Êý¡£¼ò¶øÑÔÖ®£¬»Øµ÷º¯Êý¾ÍÊÇÔÊÐíÓû§°ÑÐèÒªµ÷Óõĺ¯ÊýµÄÖ¸Õë×÷Ϊ²ÎÊý´«µÝ¸øÒ»¸öº¯Êý£¬ÒÔ±ã¸Ãº¯ÊýÔÚ´¦ÀíÏàËÆʼþµÄʱºò¿ÉÒÔÁé»îµÄʹÓò»Í¬µÄ·½·¨¡£
ÏëÖªµÀ»Øµ÷º¯ÊýÔÚʵ¼ÊÖÐÓÐʲô×÷Óã¿ÏȼÙÉèÓÐÕâÑùÒ»ÖÖÇé¿ö£ºÎÒÃÇÒª±àдһ¸ö¿â£¬ËüÌṩÁËijЩÅÅÐòËã·¨µÄʵÏÖ£¨ÈçðÅÝÅÅÐò¡¢¿ìËÙÅÅÐò¡¢shellÅÅÐò¡¢shakeÅÅÐòµÈµÈ£©£¬ÎªÁËÄÜÈÿâ¸ü¼ÓͨÓ㬲»ÏëÔÚº¯ÊýÖÐǶÈëÅÅÐòÂß¼£¬¶øÈÃʹÓÃÕßÀ´ÊµÏÖÏàÓ¦µÄÂß¼£»»òÕߣ¬ÄÜÈÿâ¿ÉÓÃÓÚ¶àÖÖÊý¾ÝÀàÐÍ£¨int¡¢float¡¢string£©£¬´Ëʱ£¬¸ÃÔõô°ìÄØ£¿¿ÉÒÔʹÓú¯ÊýÖ¸Õ룬²¢½øÐлص÷¡£
»Øµ÷¿ÉÓÃÓÚ֪ͨ»úÖÆ¡£ÀýÈ磬ÓÐʱҪÔÚA³ÌÐòÖÐÉèÖÃÒ»¸ö¼ÆʱÆ÷£¬Ã¿µ½Ò»¶¨Ê±¼ä£¬A³ÌÐò»áµÃµ½ÏàÓ¦µÄ֪ͨ£¬µ«Í¨Öª»úÖƵÄʵÏÖÕ߶ÔA³ÌÐòÒ»ÎÞËùÖª¡£ÄÇô£¬¾ÍÐèÒ»¸ö¾ßÓÐÌض¨ÔÐ͵ĺ¯ÊýÖ¸Õë½øÐлص÷£¬Í¨ÖªA³ÌÐòʼþÒѾ·¢Éú¡£Êµ¼ÊÉÏ£¬APIʹÓÃÒ»¸ö»Øµ÷º¯ÊýSetTimer()À´Í¨Öª¼ÆʱÆ÷¡£Èç¹ûûÓÐÌṩ»Øµ÷º¯Êý£¬Ëü»¹»á°ÑÒ»¸öÏûÏ¢·¢Íù³ÌÐòµÄÏûÏ¢¶ÓÁС£
ÁíÒ»¸öʹÓûص÷»úÖƵÄAPIº¯ÊýÊÇEnumWindow£¨£©£¬Ëüö¾ÙÆÁÄ»ÉÏËùÓеĶ¥²ã´°¿Ú£¬Ã¿¸ö´°¿Ú¶¼¿ÉÒÔͨ¹ýËüµ÷ÓÃÁíÒ»¸ö³ÌÐòÌṩµÄº¯Êý£¬²¢´«µÝ´°¿ÚµÄ´¦Àí³ÌÐò¡£ÀýÈ磺Èç¹û±»µ÷ÓÃÕß·µ»ØÒ»¸öÖµ£¬¾Í¼ÌÐø½øÐеü´ú£»·ñÔò£¬Í˳ö¡£EnumWindow()²¢²»¹ØÐı»µ÷ÓÃÕßÔں䦣¬Ò²²»¹ØÐı»µ÷ÓÃÕßÓÃËü´«µÝµÄ´¦Àí³ÌÐò×öÁËʲô£¬ËüÖ»¹ØÐÄ·µ»ØÖµ£¬ÒòΪ»ùÓÚ·µ»ØÖµ£¬Ëü½«¼ÌÐøÖ´ÐлòÍ˳ö¡£
²»¹ÜÔõô˵£¬»Øµ÷º¯ÊýÊǼ̳Ð×ÔCÓïÑԵġ£ÔÚC++ÖУ¬Ó¦Ö»ÔÚÓëC´úÂ뽨Á¢½Ó¿Ú»òÓëÒÑÓеĻص÷½Ó¿Ú´ò½»µÀʱ£¬²ÅʹÓûص÷º¯Êý¡£³ýÁËÉÏÊöÇé¿ö£¬ÔÚC++ÖÐӦʹÓÃÐéÄâ·½·¨»ò·Âº¯Êý£¨functor£©£¬¶ø²»Êǻص÷º¯Êý¡£
7.1.3 ˵Ã÷
Äãµ½Ò»¸öÉ̵êÂò¶«Î÷£¬¸ÕºÃÄãÒªµÄ¶«Î÷ûÓлõ£¬ÓÚÊÇÄãÔÚµêÔ±ÄÇÀïÁôÏÂÁËÄãµÄµç»°£¬¹ýÁ˼¸ÌìµêÀïÓлõÁË£¬µêÔ±¾Í´òÁËÄãµÄµç»°£¬È»ºóÄã½Óµ½µç»°ºó¾Íµ½µêÀïȥȡÁË»õ¡£ÔÚÕâ¸öÀý×ÓÀÄãµÄµç»°ºÅÂë¾Í½Ð»Øµ÷º¯Êý£¬Äã°Ñµç»°Áô¸øµêÔ±¾Í½ÐµÇ¼Ç»Øµ÷º¯Êý£¬µêÀïºóÀ´ÓлõÁ˽Ð×ö´¥·¢Á˻ص÷¹ØÁªµÄʼþ£¬µêÔ±¸øÄã´òµç»°½Ð×öµ÷Óûص÷º¯Êý£¬Äãµ½µêÀïȥȡ»õ½Ð×öÏìÓ¦»Øµ÷ʼþ¡£
https://www.cnblogs.com/ioleon13/archive/2010/03/02/1676621.html
7.2 Óú¯ÊýÖ¸Õë×÷Ϊº¯ÊýµÄ·µ»ØÖµ
ÔÚÖ¸Õ뺯ÊýÀïÃ棬ÓÐÕâÑùÒ»ÀຯÊý£¬ËüÃÇÒ²·µ»ØÖ¸ÕëÐÍÊý¾Ý£¨µØÖ·£©£¬µ«ÊÇÕâ¸öÖ¸Õë²»ÊÇÖ¸Ïòint¡¢charÖ®ÀàµÄ»ù±¾ÀàÐÍ£¬¶øÊÇÖ¸Ïòº¯Êý¡£¶ÔÓÚ³õѧÕߣ¬±ð˵д³öÕâÑùµÄº¯ÊýÉùÃ÷£¬¾ÍÊÇ¿´µ½ÕâÑùµÄд·¨Ò²ÊÇһͷÎíË®¡£±ÈÈç,ÏÂÃæµÄÓï¾ä£º
int (*ff(int))(int *, int);
ÎÒÃÇÓÃÉÏÃæ½éÉܵķ½·¨·ÖÎöһϣ¬ffÊ×ÏÈÓëºóÃæµÄ¡°()¡±½áºÏ£¬¼´£º
int (*(ff(int)))(int *, int); // ÓÃÀ¨ºÅ½«ff(int)ÔÙÀ¨ÆðÀ´
Ò²¾ÍÒâζ×Å£¬ffÊÇÒ»¸öº¯Êý¡£
½Ó×ÅÓëÇ°ÃæµÄ¡°*¡±½áºÏ£¬ËµÃ÷ffº¯ÊýµÄ·µ»ØÖµÊÇÒ»¸öÖ¸Õ롣ȻºóÔÙÓëºóÃæµÄ¡°()¡±½áºÏ£¬Ò²¾ÍÊÇ˵£¬¸ÃÖ¸ÕëÖ¸ÏòµÄÊÇÒ»¸öº¯Êý¡£
ÕâÖÖд·¨È·ÊµÈÃÈ˷dz£ÄѶ®£¬ÒÔÖÁÓÚһЩ³õѧÕß²úÉúÎó½â£¬ÈÏΪд³ö±ðÈË¿´²»¶®µÄ´úÂë²ÅÄÜÏÔʾ×Ô¼ºË®Æ½¸ß¡£¶øÊÂʵÉÏÇ¡ºÃÏà·´£¬ÄÜ·ñд³öͨË×Ò׶®µÄ´úÂëÊǺâÁ¿³ÌÐòÔ±ÊÇ·ñÓÅÐãµÄ±ê×¼¡£Ò»°ãÀ´Ëµ£¬ÓÃtypedef¹Ø¼ü×Ö»áʹ¸ÃÉùÃ÷¸ü¼òµ¥Ò׶®¡£ÔÚÇ°ÃæÎÒÃÇÒѾ¼û¹ý£º
int (*PF)(int *, int);
Ò²¾ÍÊÇ˵£¬PFÊÇÒ»¸öº¯ÊýÖ¸Õë¡°±äÁ¿¡±¡£µ±Ê¹ÓÃtypedefÉùÃ÷ºó£¬ÔòPF¾Í³ÉΪÁËÒ»¸öº¯ÊýÖ¸Õë¡°ÀàÐÍ¡±£¬¼´£º
typedef int (*PF)(int *, int);
ÕâÑù¾Í¶¨ÒåÁË·µ»ØÖµµÄÀàÐÍ¡£È»ºó£¬ÔÙÓÃPF×÷Ϊ·µ»ØÖµÀ´ÉùÃ÷º¯Êý:
PF ff(int);
ÏÂÃ潫ÒÔ³ÌÐòÇåµ¥1ΪÀý£¬ËµÃ÷Óú¯ÊýÖ¸Õë×÷Ϊº¯ÊýµÄ·µ»ØÖµµÄÓ÷¨¡£µ±³ÌÐò½ÓÊÕÓû§ÊäÈëʱ£¬Èç¹ûÓû§ÊäÈëd£¬ÔòÇóÊý×éµÄ×î´óÖµ£¬Èç¹ûÊäÈëx£¬ÔòÇóÊý×éµÄ×îСֵ£¬Èç¹ûÊäÈëp£¬ÔòÇóÊý×éµÄƽ¾ùÖµ¡£
#include<stdio.h> #include<assert.h>
double UnKnown(double *dbData, int iSize); // δ֪Ëã·¨ double GetAverage(double *dbData, int iSize); // Çóƽ¾ùÖµ double GetMax(double *dbData, int iSize); // Çó×î´óÖµ double GetMin(double *dbData, int iSize); // Çó×îСֵ
typedef double (*PF)(double *dbData, int iSize); //¶¨Ò庯ÊýÖ¸ÕëÀàÐÍ PF GetOperation(char c) ;
int main(void) { double dbData[]={3.1415926, 1.4142, -0.5,999, -313, 365}; int iSize = sizeof(dbData)/sizeof(dbData[0]); char c; printf("Please input the Operation :\n"); c = getchar(); printf("result is %lf\n",GetOperation(c)(dbData,iSize));// ͨ¹ýº¯ÊýÖ¸Õëµ÷Óú¯Êý }
PF GetOperation(char c) // ¸ù¾Ý×Ö·ûµÃµ½²Ù×÷ÀàÐÍ£¬·µ»Øº¯ÊýÖ¸Õë { switch(c) { case 'd': return GetMax; case 'x': return GetMin; case 'p': return GetAverage; default: return UnKnown; } } double GetMin(double *dbData, int iSize) // Çó×îСֵ { double dbMin; int i; assert(iSize > 0); /* void assert( int expression ); ¡¡¡¡assertµÄ×÷ÓÃÊÇÏÖ¼ÆËã±í´ïʽ expression £¬Èç¹ûÆäֵΪ¼Ù£¨¼´Îª0£©£¬ ÄÇôËüÏÈÏòstderr´òÓ¡Ò»Ìõ³ö´íÐÅÏ¢£¬È»ºóͨ¹ýµ÷Óà abort À´ÖÕÖ¹³ÌÐòÔËÐС£ ´Ëʱ³ÌÐò¾Í²»»á¼ÌÐøÍùÏÂÖ´ÐÐ */ dbMin = dbData[0]; for(i = 1; i < iSize; i++) { if(dbMin > dbData[i]) { dbMin = dbData[i]; } } return dbMin; }
double GetMax(double *dbData, int iSize) // Çó×î´óÖµ { double dbMax; int i; assert(iSize > 0); dbMax = dbData[0]; for(i = 1; i < iSize; i++) { if(dbMax < dbData[i]) { dbMax = dbData[i]; } } return dbMax; }
double GetAverage(double *dbData, int iSize) // Çóƽ¾ùÖµ { double dbSum = 0; int i; assert(iSize > 0); for(i = 0; i < iSize; i++) { dbSum += dbData[i]; } return dbSum / iSize; }
double UnKnown(double *dbData, int iSize) // δ֪Ëã·¨ { return 0; } |
ÉÏÊö³ÌÐòÖÐÇ°Ãæ4¸öº¯Êý·Ö±ðʵÏÖÇó×î´óÖµ¡¢×îСֵ¡¢Æ½¾ùÖµºÍδ֪Ëã·¨£¬È»ºóʵÏÖÁËGetOperationº¯Êý¡£Õâ¸öº¯Êý¸ù¾Ý×Ö·ûµÄ·µ»ØֵʵÏÖÉÏÃæ4¸öº¯Êý¡£ËüÊÇÒÔº¯ÊýÖ¸ÕëµÄÐÎʽ·µ»ØµÄ£¬´ÓºóÃæµÄmainº¯ÊýµÄGetOperation(c)(dbData, iSize)¿ÉÒÔ¿´³ö£¬Í¨¹ýÕâ¸öÖ¸Õë¿ÉÒÔµ÷Óú¯Êý¡£