ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ22²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡whenever¡¡any¡¡parsing¡¡routines¡¡are¡¡used£»¡¡German¡¡from¡¡Germany¡¡is¡¡used¡¡as¡¡the¡¡basis¡¡for¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡95¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡PU¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡S¡¡¡¡73¡¡
formatting¡¡rules¡£¡¡Changing¡¡the¡¡culture¡¡information¡¡does¡¡not¡¡affect¡¡the¡¡formatting¡¡rules¡¡of¡¡the¡¡¡¡
programming¡¡language¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡It¡¡is¡¡also¡¡possible¡¡to¡¡parse¡¡dates¡¡and¡¡times¡¡using¡¡the¡¡Parse£¨£©¡¡and¡¡TryParse£¨£©¡¡routines£»¡¡as¡¡¡¡
demonstrated¡¡by¡¡the¡¡following¡¡examples£º¡¡
Public¡¡Sub¡¡TestGermanParseDate£¨£©¡¡
¡¡¡¡¡¡¡¡Dim¡¡datetime¡¡As¡¡Date¡¡=¡¡Date¡£Parse£¨¡¨May¡¡10£»¡¡2008¡¨£©¡¡
¡¡¡¡¡¡¡¡If¡¡5¡¡=¡¡datetime¡£Month¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¡¡¨correct¡¨£©¡¡
¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡Thread¡£CurrentThread¡£CurrentCulture¡¡=¡¡new¡¡CultureInfo£¨¡¨de¡DE¡¨£©¡¡
¡¡¡¡¡¡¡¡datetime¡¡=¡¡Date¡£Parse£¨¡¨10¡¡Mai£»¡¡2008¡¨£©¡¡
¡¡¡¡¡¡¡¡If¡¡5¡¡=¡¡datetime¡£Month¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¡¡¨correct¡¨£©¡¡
¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡
End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡Notice¡¡how¡¡the¡¡first¡¡Date¡£Parse£¨£©¡¡processed¡¡English¡Canadian¡¡formatted¡¡text¡¡and¡¡knew¡¡¡¡
that¡¡the¡¡identifier¡¡May¡¡equaled¡¡the¡¡fifth¡¡month¡¡of¡¡the¡¡year¡£¡¡For¡¡the¡¡second¡¡Date¡£Parse£¨£©¡¡method¡¡¡¡
call£»¡¡the¡¡culture¡¡was¡¡changed¡¡to¡¡German£»¡¡and¡¡it¡¡was¡¡possible¡¡to¡¡process¡¡10¡¡Mai£»¡¡2008¡£¡¡In¡¡both¡¡¡¡
cases£»¡¡processing¡¡the¡¡buffer¡¡posed¡¡no¡¡major¡¡problems£»¡¡as¡¡long¡¡as¡¡you¡¡knew¡¡that¡¡the¡¡buffer¡¡was¡¡¡¡
a¡¡German¡¡or¡¡English¡Canadian¡¡date¡£¡¡Where¡¡things¡¡can¡¡go¡¡awry¡¡is¡¡when¡¡you¡¡have¡¡a¡¡German¡¡¡¡
date¡¡and¡¡an¡¡English¡¡culture¡£¡¡
¡¡¡¡¡¡¡¡¡¡Converting¡¡a¡¡data¡¡type¡¡to¡¡a¡¡buffer¡¡is¡¡relatively¡¡easy¡¡because¡¡the¡¡ToString£¨£©¡¡methods¡¡have¡¡¡¡
been¡¡implemented¡¡to¡¡generate¡¡the¡¡desired¡¡output¡£¡¡Consider¡¡the¡¡following¡¡example£»¡¡which¡¡¡¡
generates¡¡a¡¡buffer¡¡from¡¡an¡¡integer¡¡value£º¡¡
Public¡¡Sub¡¡TestGenerateString£¨£©¡¡¡¡
¡¡¡¡¡¡¡¡Dim¡¡iValue¡¡As¡¡Integer¡¡=¡¡123¡¡
¡¡¡¡¡¡¡¡Dim¡¡buffer¡¡As¡¡String¡¡=¡¡iValue¡£ToString£¨£©¡¡
¡¡¡¡¡¡¡¡If¡¡buffer¡¡=¡¡¡¨123¡¨¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¡¡¨correct¡¨£©¡¡
¡¡¡¡¡¡¡¡End¡¡If¡¡
End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡example£»¡¡the¡¡value¡¡123¡¡has¡¡been¡¡assigned¡¡to¡¡¡¡iValue£»¡¡which¡¡is¡¡of¡¡type¡¡Integer£»¡¡and¡¡¡¡
then¡¡its¡¡ToString£¨£©¡¡method¡¡is¡¡called£»¡¡which¡¡generates¡¡a¡¡buffer¡¡that¡¡contains¡¡¡¡¡¨123¡¨¡£¡¡The¡¡same¡¡¡¡
thing¡¡can¡¡be¡¡done¡¡to¡¡a¡¡Double¡¡value£»¡¡as¡¡in¡¡this¡¡example£º¡¡
Dim¡¡number¡¡As¡¡Double¡¡=¡¡123¡£5678¡¡
Dim¡¡buffer¡¡As¡¡String¡¡=¡¡number¡£ToString£¨¡¨0¡£00¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡Here£»¡¡the¡¡number¡¡123¡£5678¡¡is¡¡converted¡¡to¡¡a¡¡buffer¡¡using¡¡the¡¡ToString£¨£©¡¡method£»¡¡but¡¡¡¡
ToString£¨£©¡¡has¡¡a¡¡parameter£»¡¡which¡¡is¡¡a¡¡formatting¡¡instruction¡¡that¡¡indicates¡¡how¡¡the¡¡double¡¡¡¡
number¡¡should¡¡be¡¡generated¡¡as¡¡a¡¡buffer¡£¡¡The¡¡desired¡¡result¡¡is¡¡a¡¡buffer¡¡with¡¡a¡¡maximum¡¡of¡¡two¡¡¡¡
digits¡¡after¡¡the¡¡decimal¡¡point¡£¡¡Because¡¡the¡¡third¡¡digit¡¡after¡¡the¡¡decimal¡¡is¡¡a¡¡7£»¡¡the¡¡value¡¡is¡¡¡¡
rounded¡¡up£»¡¡resulting¡¡in¡¡the¡¡buffer¡¡123¡£57¡£¡¡
¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡see¡¡an¡¡example¡¡where¡¡the¡¡culture¡¡information¡¡also¡¡applies¡¡to¡¡generating¡¡a¡¡buffer¡£¡¡¡¡
Here£»¡¡a¡¡Double¡¡value¡¡is¡¡generated¡¡in¡¡the¡¡format¡¡of¡¡the¡¡culture£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡96¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
74¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡P¡¡U¡¡L¡¡AT¡¡IO¡¡N¡¡S¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡TestGenerateGermanNumber£¨£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡number¡¡As¡¡Double¡¡=¡¡123¡£5678¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Thread¡£CurrentThread¡£CurrentCulture¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡new¡¡CultureInfo£¨¡¨de¡DE¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡buffer¡¡As¡¡String¡¡=¡¡number¡£ToString£¨¡¨0¡£00¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡buffer¡¡=¡¡¡¨123£»57¡¨¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¡¡¨correct¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡in¡¡the¡¡previous¡¡examples£»¡¡the¡¡CurrentCulture¡¡property¡¡is¡¡assigned¡¡the¡¡desired¡¡culture¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Then¡¡when¡¡the¡¡Double¡¡variable¡¡number¡¡has¡¡its¡¡ToString£¨£©¡¡method¡¡called£»¡¡the¡¡buffer¡¡¡¡¡¨123£»57¡¨¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡generated¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Important¡¡Stuff¡¡to¡¡Remember¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡chapter£»¡¡you¡¡learned¡¡about¡¡strings¡¡and¡¡writing¡¡code¡£¡¡Here¡¡are¡¡the¡¡keys¡¡points¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡remember£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Writing¡¡tests¡¡is¡¡an¡¡important¡¡part¡¡of¡¡your¡¡development¡¡practice¡£¡¡A¡¡test¡¡is¡¡not¡¡just¡¡a¡¡mech
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡anism¡¡to¡¡catch¡¡errors£»¡¡but¡¡also¡¡a¡¡mechanism¡¡used¡¡to¡¡understand¡¡the¡¡dynamics¡¡of¡¡your¡¡code¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡The¡¡String¡¡type¡¡is¡¡a¡¡special¡¡reference¡¡type¡¡that¡¡has¡¡many¡¡methods¡¡and¡¡properties¡£¡¡You¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡are¡¡advised¡¡to¡¡look¡¡at¡¡the¡¡MSDN¡¡documentation¡¡to¡¡see¡¡what¡¡a¡¡string¡¡can¡¡do¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡IntelliSense¡¡and¡¡the¡¡MSDN¡¡documentation¡¡are¡¡your¡¡best¡¡bets¡¡when¡¡you¡¡want¡¡to¡¡find¡¡out¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡about¡¡specific¡¡methods£»¡¡properties£»¡¡or¡¡types¡£¡¡Books¡¡and¡¡web¡¡sites¡¡such¡¡as¡¡Code¡¡Project¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡are¡¡good¡¡resources¡¡to¡¡help¡¡you¡¡understand¡¡concepts¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡All¡¡variables¡¡and¡¡types¡¡are¡¡based¡¡on¡¡the¡¡object¡¡type¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡writing¡¡code£»¡¡you¡¡need¡¡to¡¡define¡¡responsibilities¡¡and¡¡contexts¡£¡¡Don¡¯t¡¡fix¡¡bugs¡¡or¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡write¡¡code¡¡using¡¡knee¡jerk¡¡reactions¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡All¡¡strings¡¡are¡¡based¡¡on¡¡Unicode¡£¡¡Each¡¡Unicode¡¡character¡¡is¡¡16¡¡bits¡¡wide¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡When¡¡translating¡¡buffers£»¡¡you¡¡need¡¡to¡¡deal¡¡with¡¡the¡¡translation¡¡of¡¡text¡¡and¡¡the¡¡translation¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡numbers¡¡and¡¡dates¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡¡¡includes¡¡sophisticated¡¡technology¡¡to¡¡help¡¡you¡¡translate¡¡numbers¡¡and¡¡dates¡¡using¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bination¡¡of¡¡language¡¡and¡¡culture¡¡information¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡97¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡3¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡ST¡¡R¡¡I¡¡N¡¡G¡¡¡¡¡¡M¡¡A¡¡N¡¡I¡¡PU¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡S¡¡¡¡75¡¡
Some¡¡Things¡¡for¡¡You¡¡to¡¡Do¡¡
The¡¡following¡¡are¡¡some¡¡exercises¡¡that¡¡relate¡¡to¡¡what¡¡you¡¯ve¡¡learned¡¡in¡¡this¡¡chapter¡£¡¡
¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡Finish¡¡the¡¡application¡¡to¡¡translate¡¡from¡¡one¡¡language¡¡to¡¡another¡¡language£»¡¡allowing¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡user¡¡to¡¡choose¡¡which¡¡direction¡¡the¡¡translation¡¡takes¡£¡¡
¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡Extend¡¡the¡¡¡¡LanguageTranslator¡¡ponent¡¡to¡¡be¡¡able¡¡to¡¡translate¡¡the¡¡words¡¡¡¡au¡¡revoir¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡auf¡¡wiedersehen¡¡to¡¡good¡¡bye¡£¡¡
¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡You¡¡can¡¡bine¡¡strings¡¡by¡¡using¡¡the¡¡plus¡¡sign£»¡¡but¡¡doing¡¡many¡¡additions¡¡will¡¡slow¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡down¡¡your¡¡code¡£¡¡Use¡¡the¡¡StringBuilder¡¡class¡¡to¡¡concatenate¡¡two¡¡buffers¡¡together¡£¡¡Hint£º¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡want¡¡to¡¡convert¡¡the¡¡code¡¡String¡¡c¡¡=¡¡a¡¡£«¡¡b£»¡¡and¡¡make¡¡a¡¡and¡¡b¡¡use¡¡the¡¡StringBuilder¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡£¡¡The¡¡result¡¡of¡¡the¡¡StringBuilder¡¡is¡¡assigned¡¡to¡¡the¡¡variable¡¡c¡£¡¡
¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡Create¡¡a¡¡test¡¡that¡¡demonstrates¡¡what¡¡happens¡¡when¡¡a¡¡number¡¡value¡¡is¡¡added¡¡to¡¡a¡¡string¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡value¡£¡¡Write¡¡the¡¡appropriate¡¡tests¡¡to¡¡verify¡¡your¡¡conclusion¡£¡¡
¡¡¡¡¡¡¡¡¡¡5¡£¡¡¡¡Extend¡¡the¡¡¡¡LanguageTranslator¡¡ponent¡¡to¡¡include¡¡methods¡¡to¡¡translate¡¡English¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡numbers¡¡into¡¡German¡¡numbers¡£¡¡
¡¡¡¡¡¡¡¡¡¡6¡£¡¡¡¡Extend¡¡the¡¡LanguageTranslator¡¡ponent¡¡to¡¡include¡¡methods¡¡to¡¡translate¡¡dates¡¡from¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡American¡¡or¡¡Canadian¡¡dates¡¡into¡¡German¡¡dates¡£¡¡Note¡¡that¡¡the¡¡added¡¡wrinkle¡¡is¡¡that¡¡you¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡could¡¡input¡¡an¡¡American¡¡or¡¡Canadian¡¡date¡£¡¡
¡¡¡¡¡¡¡¡¡¡7¡£¡¡¡¡Implement¡¡the¡¡Windows¡¡application¡¡that¡¡calls¡¡the¡¡¡¡LanguageTranslator¡¡ponent¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡98¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡99¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
C¡¡¡¡H¡¡¡¡A¡¡¡¡P¡¡¡¡T¡¡¡¡E¡¡¡¡R¡¡¡¡¡¡¡¡¡¡4¡¡
¡ö¡¡¡ö¡¡¡ö¡¡
Learning¡¡About¡¡Data¡¡Structures£»¡¡¡¡
¡¡Decisions£»¡¡and¡¡Loops¡¡
When¡¡you¡¡are¡¡creating¡¡applications£»¡¡the¡¡source¡¡code¡¡will¡¡need¡¡to¡¡make¡¡decisions£»¡¡such¡¡as¡¡¡¡
should¡¡you¡¡open¡¡the¡¡file¡¡or¡¡save¡¡the¡¡file£¿¡¡And¡¡if¡¡you¡¡open¡¡the¡¡file£»¡¡what¡¡kind¡¡of¡¡iterative¡¡code¡¡is¡¡¡¡
going¡¡to¡¡read¡¡the¡¡contents¡¡of¡¡the¡¡file£¿¡¡These¡¡sorts¡¡of¡¡questions¡¡are¡¡answered¡¡by¡¡employing¡¡data¡¡¡¡
structures£»¡¡decisions£»¡¡and¡¡loops¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡easiest¡¡way¡¡to¡¡demonstrate¡¡how¡¡to¡¡make¡¡a¡¡decision¡¡is¡¡to¡¡write¡¡a¡¡miniature¡¡artificial¡¡¡¡
intelligence¡¡£¨AI£©¡¡system¡£¡¡The¡¡AI¡¡system¡¡will¡¡be¡¡extremely¡¡primitive£»¡¡but¡¡AI¡¡is¡¡interesting¡¡because¡¡¡¡
it¡¡makes¡¡extensive¡¡use¡¡of¡¡decision¡¡and¡¡loop¡¡constructs¡£¡¡The¡¡AI¡¡system¡¡iterates¡¡and¡¡makes¡¡deci
sions¡¡based¡¡on¡¡data¡¡defined¡¡in¡¡a¡¡neat¡¡and¡¡orderly¡¡custom¡¡data¡¡structure¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡Using¡¡the¡¡example¡¡of¡¡creating¡¡an¡¡algorithm£»¡¡the¡¡following¡¡topics¡¡will¡¡be¡¡covered¡¡in¡¡this¡¡¡¡
chapter£º¡¡
¡¡¡¡¡¡¡¡o¡¡¡¡Data¡¡structures£»¡¡including¡¡custom¡¡types¡¡
¡¡¡¡¡¡¡¡o¡¡¡¡The¡¡restrictions¡¡of¡¡value¡¡types¡¡
¡¡¡¡¡¡¡¡o¡¡¡¡Algorithm¡¡design¡¡
¡¡¡¡¡¡¡¡o¡¡¡¡Class¡¡constructors£»¡¡which¡¡allow¡¡you¡¡to¡¡initialize¡¡an¡¡object¡¡
¡¡¡¡¡¡¡¡o¡¡¡¡The¡¡For¡¡loop£»¡¡which¡¡allows¡¡you¡¡to¡¡iterate¡¡over¡¡sets¡¡of¡¡data¡¡
¡¡¡¡¡¡¡¡o¡¡¡¡The¡¡If¡¡statement£»¡¡which¡¡allows¡¡you¡¡to¡¡execute¡¡specific¡¡code¡¡based¡¡on¡¡logic¡¡
Understanding¡¡the¡¡Depth¡First¡¡Search¡¡Algorithm¡¡
AI¡¡involves¡¡searching¡¡for¡¡data£»¡¡and¡¡a¡¡core¡¡algorithm¡¡of¡¡AI¡¡is¡¡searching¡£¡¡The¡¡search¡¡algorithm¡¡¡¡
that¡¡we¡¡will¡¡develop¡¡for¡¡this¡¡chapter¡¯s¡¡example¡¡is¡¡a¡¡depth¡first¡¡search¡¡system¡£¡¡AI¡¡has¡¡other¡¡types¡¡¡¡
of¡¡searches£»¡¡such¡¡as¡¡an¡¡A*¡¡or¡¡a¡¡breadth¡first¡¡search£»¡¡but¡¡they¡¡are¡¡all¡¡based¡¡on¡¡the¡¡same¡¡idea¡¡as¡¡¡¡
the¡¡depth¡search¡¡algorithm¡£¡¡This¡¡idea¡¡is¡¡searching¡¡for¡¡information¡¡that¡¡is¡¡arranged¡¡in¡¡a¡¡tree¡¡¡¡
structure¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡77¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡100¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
78¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡D¡¡AT¡¡A¡¡¡¡S¡¡TR¡¡U¡¡CT¡¡U¡¡R¡¡E¡¡S£»¡¡¡¡¡¡DE¡¡CI¡¡SI¡¡ON¡¡S£»¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡L¡¡O¡¡OP¡¡S¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡An¡¡algorithm¡¡is¡¡a¡¡logical¡¡set¡¡of¡¡finite£»¡¡repeatable¡¡steps¡¡for¡¡pleting¡¡a¡¡task¡£¡¡The¡¡term¡¡is¡¡usually¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡applied¡¡in¡¡relation¡¡to¡¡formal¡¡problems¡¡such¡¡as¡¡searching£»¡¡but¡¡most£»¡¡if¡¡not¡¡all¡¡puter¡¡programs£»¡¡use¡¡algo
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡rithms¡¡of¡¡one¡¡sort¡¡or¡¡another¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Before¡¡we¡¡write¡¡any¡¡code£»¡¡you¡¡need¡¡to¡¡understand¡¡what¡¡the¡¡depth¡first¡¡search¡¡algorithm¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡does¡¡and¡¡why¡¡you¡¡would¡¡use¡¡it¡£¡¡The¡¡problem¡¡to¡¡solve¡¡is¡¡how¡¡to¡¡get¡¡from¡¡point¡¡A¡¡to¡¡point¡¡B¡¡in¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡most¡¡efficient¡¡manner¡£¡¡This¡¡problem¡¡can¡¡be¡¡stated¡¡generally¡¡as£»¡¡¡°how¡¡to¡¡solve¡¡task¡¡A¡¡when¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡have¡¡X¡¡options¡£¡±¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imagine¡¡you¡¡are¡¡about¡¡to¡¡drive¡¡to¡¡work¡¡and¡¡you¡¡are¡¡at¡¡the¡¡front¡¡door¡¡of¡¡your¡¡house¡£¡¡You¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡don¡¯t¡¡know¡¡where¡¡your¡¡keys¡¡are£»¡¡and¡¡thus¡¡you¡¡begin¡¡a¡¡search¡¡for¡¡the¡¡keys¡¡in¡¡the¡¡house¡£¡¡Of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡course£»¡¡you¡¡try¡¡to¡¡remember£»¡¡but¡¡your¡¡memory¡¡is¡¡not¡¡working¡¡that¡¡early¡¡in¡¡the¡¡morning¡£¡¡You¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡try¡¡to¡¡retrace¡¡your¡¡steps£»¡¡and¡¡think¡¡of¡¡logical¡¡places¡¡where¡¡you¡¡could¡¡have¡¡placed¡¡your¡¡keys¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡you¡¡retrace¡¡your¡¡steps£»¡¡you¡¡follow¡¡your¡¡memory¡¯s¡¡logic¡£¡¡Simply¡¡put£»¡¡your¡¡search¡¡algo
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡rithm¡¡is¡¡based¡¡on¡¡your¡¡memory¡¯s¡¡suggestion¡¡of¡¡where¡¡the¡¡keys¡¡might¡¡be¡£¡¡The¡¡data¡¡structure¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡you¡¡are¡¡navigating¡¡is¡¡the¡¡rooms¡¡of¡¡your¡¡house¡£¡¡Your¡¡brain¡based¡¡search¡¡algorithm¡¡could¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡create¡¡a¡¡search¡¡pattern¡¡like¡¡that¡¡shown¡¡in¡¡Figure¡¡4¡1¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡4¡1¡£¡¡A¡¡possible¡¡search¡¡order¡¡for¡¡your¡¡keys¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡Figure¡¡4¡1£»¡¡you¡¡found¡¡the¡¡keys¡¡in¡¡the¡¡hall£»¡¡but¡¡yet¡¡your¡¡search¡¡algorithm¡¡led¡¡you¡¡astray¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡for¡¡a¡¡while£»¡¡since¡¡you¡¡searched¡¡the¡¡hall¡¡last¡£¡¡The¡¡cynic¡¡could¡¡say¡¡that¡¡you¡¡kept¡¡walking¡¡around¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡keys¡¡without¡¡realizing¡¡that¡¡they¡¡were¡¡so¡¡close¡¡to¡¡you¡£¡¡But¡¡this¡¡is¡¡the¡¡crux¡¡of¡¡the¡¡problem£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡because¡¡you¡¡didn¡¯t¡¡know¡¡you¡¡developed¡¡a¡¡search¡¡algorithm¡¡that¡¡would¡¡lead¡¡you¡¡astray¡¡this¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡time¡£¡¡And¡¡the¡¡same¡¡algorithm¡¡might¡¡not¡¡l
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡