ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
µÚÈýµç×ÓÊé ·µ»Ø±¾ÊéĿ¼ ¼ÓÈëÊéÇ© ÎÒµÄÊé¼Ü ÎÒµÄÊéÇ© TXTÈ«±¾ÏÂÔØ ¡ºÊղص½ÎÒµÄä¯ÀÀÆ÷¡»

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ13²¿·Ö

¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡


the¡¡answers¡¡returned¡¡by¡¡the¡¡method¡¡with¡¡targeted¡¡responses¡£¡¡The¡¡If¡¡statement¡¡is¡¡used¡¡to¡¡check¡¡¡¡

whether¡¡the¡¡value¡¡of¡¡the¡¡variable¡¡total¡¡is¡¡equal¡¡to¡¡¡¡3¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡When¡¡writing¡¡test¡¡code£»¡¡the¡¡way¡¡the¡¡Add£¨£©¡¡method¡¡is¡¡used¡¡must¡¡be¡¡the¡¡same¡¡way¡¡the¡¡¡¡

console¡¡or¡¡Windows¡¡application¡¡uses¡¡the¡¡method¡£¡¡Otherwise£»¡¡it¡¡would¡¡be¡¡like¡¡testing¡¡a¡¡winter¡¡¡¡

tire¡¡in¡¡the¡¡middle¡¡of¡¡the¡¡Sahara¡ªfun¡¡to¡¡do¡¡but¡¡irrelevant¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Another¡¡question¡¡related¡¡to¡¡testing¡¡has¡¡to¡¡do¡¡with¡¡the¡¡timing¡¡of¡¡tests¡£¡¡Do¡¡you¡¡create¡¡the¡¡¡¡

tests¡¡before¡¡or¡¡after¡¡implementing¡¡the¡¡Add£¨£©¡¡method£¿¡¡To¡¡get¡¡a¡¡clear¡¡understanding¡¡of¡¡the¡¡problem£»¡¡¡¡

imagine¡¡the¡¡development¡¡of¡¡a¡¡tire¡£¡¡Do¡¡you¡¡define¡¡the¡¡tests¡¡for¡¡the¡¡tire¡¡before¡¡or¡¡after¡¡the¡¡tire¡¡has¡¡¡¡

been¡¡developed£¿¡¡Most¡¡likely£»¡¡the¡¡answer¡¡is¡¡before£»¡¡during£»¡¡and¡¡after¡¡development¡£¡¡This¡¡is¡¡an¡¡¡¡

important¡¡consideration¡¡when¡¡developing¡¡software¡£¡¡Tests¡¡are¡¡written¡¡before£»¡¡during£»¡¡and¡¡after¡¡¡¡

implementation£»¡¡as¡¡follows£º¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡58¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

36¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡You¡¡develop¡¡tests¡¡before¡¡implementing¡¡the¡¡Add£¨£©¡¡method¡¡to¡¡get¡¡an¡¡idea¡¡of¡¡what¡¡name

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡spaces£»¡¡classes£»¡¡and¡¡methods¡¡you¡¡will¡¡be¡¡defining¡£¡¡The¡¡definition¡¡of¡¡the¡¡different¡¡items¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡gives¡¡the¡¡developer¡¡an¡¡idea¡¡of¡¡how¡¡the¡¡items¡¡will¡¡be¡¡used¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡You¡¡develop¡¡tests¡¡during¡¡the¡¡implementation¡¡of¡¡the¡¡Add£¨£©¡¡method¡¡to¡¡verify¡¡that¡¡your¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡source¡¡code¡¡implementation¡¡is¡¡on¡¡the¡¡right¡¡track¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡You¡¡develop¡¡tests¡¡after¡¡the¡¡implementation¡¡of¡¡the¡¡Add£¨£©¡¡method¡¡as¡¡an¡¡exhaustive¡¡measure¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡make¡¡sure¡¡you¡¯ve¡¡dotted¡¡the¡¡i¡¯s¡¡and¡¡crossed¡¡the¡¡t¡¯s¡¡in¡¡the¡¡implementation¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Adding¡¡a¡¡Test¡¡Project¡¡to¡¡Your¡¡Solution¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡writing¡¡test¡¡routines£»¡¡you¡¡will¡¡need¡¡to¡¡organize¡¡the¡¡source¡¡code£»¡¡and¡¡that¡¡means¡¡figuring¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡out¡¡to¡¡which¡¡project¡¡the¡¡tests¡¡are¡¡added¡£¡¡For¡¡the¡¡calculator¡¡application£»¡¡you¡¡could¡¡place¡¡the¡¡test¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡routines¡¡within¡¡the¡¡Calculator¡¡class¡¡library¡£¡¡However£»¡¡doing¡¡that¡¡is¡¡not¡¡the¡¡proper¡¡approach¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡due¡¡to¡¡distribution¡¡of¡¡the¡¡class¡¡library¡¡and¡¡correct¡¡testing¡¡context¡£¡¡Remember¡¡that¡¡the¡¡test¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡routines¡¡must¡¡be¡¡identical¡¡to¡¡how¡¡the¡¡code¡¡will¡¡be¡¡used¡£¡¡Thus£»¡¡the¡¡appropriate¡¡place¡¡for¡¡the¡¡test¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡routines¡¡is¡¡in¡¡their¡¡own¡¡application¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡ideal¡¡approach¡¡is¡¡to¡¡create¡¡another¡¡application¡¡that¡¡represents¡¡the¡¡tests¡£¡¡Figure¡¡2¡­5¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡illustrated¡¡how¡¡a¡¡Windows¡¡and¡¡console¡¡application¡¡could¡¡use¡¡the¡¡Calculator¡¡class¡¡library¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­9¡¡adds¡¡the¡¡testing¡¡console¡¡application¡¡that¡¡also¡¡uses¡¡the¡¡class¡¡library¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­9¡£¡¡Adding¡¡the¡¡testing¡¡console¡¡application¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡testing¡¡console¡¡application¡¡is¡¡like¡¡the¡¡console¡¡application¡¡created¡¡in¡¡Chapter¡¡1£»¡¡and¡¡it¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡references¡¡the¡¡Calculator¡¡class¡¡library¡£¡¡Both¡¡projects¡¡should¡¡be¡¡part¡¡of¡¡the¡¡Calculator¡¡solution¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Go¡¡ahead¡¡and¡¡add¡¡the¡¡TestCalculator¡¡project¡¡to¡¡the¡¡Calculator¡¡solution¡£¡¡Remember¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡add¡¡a¡¡reference¡¡to¡¡the¡¡Calculator¡¡class¡¡library¡¡£¨right¡­click¡¡TestCalculator¡¡and¡¡choose¡¡Add¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Reference¡¡¡¡Projects¡¡¡¡Calculator£©¡£¡¡Remember¡¡to¡¡set¡¡TestCalculator¡¡as¡¡the¡¡startup¡¡project¡¡for¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡debugging¡¡purposes¡£¡¡Figure¡¡2¡­10¡¡shows¡¡the¡¡TestCalculator¡¡and¡¡Calculator¡¡projects¡¡in¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Solution¡¡Explorer¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡59¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡37¡¡



Figure¡¡2¡­10¡£¡¡Solution¡¡Explorer¡¡showing¡¡the¡¡testing¡¡console¡¡application¡¡and¡¡Calculator¡¡¡¡

class¡¡library¡¡



Testing¡¡Simple¡¡Addition¡¡



Add¡¡the¡¡boldfaced¡¡code¡¡to¡¡Module1¡£vb¡¡in¡¡the¡¡testing¡¡console¡¡project¡¡to¡¡verify¡¡the¡¡addition¡¡of¡¡1¡¡¡¡

and¡¡2¡¡£¨the¡¡¡¡operator¡¡means¡¡is¡¡not¡¡equal¡¡to£©£º¡¡



Imports¡¡Calculator¡¡



Module¡¡Module1¡¡

¡¡¡¡¡¡¡¡Public¡¡Sub¡¡TestSimpleAddition£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡result¡¡As¡¡Integer¡¡=¡¡Operations¡£Add£¨1£»¡¡2£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨total¡¡¡¡3£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Oops¡¡1¡¡and¡¡2¡¡does¡¡not¡¡equal¡¡3¡¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡Sub¡¡Main£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestSimpleAddition£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£ReadKey£¨£©¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡



End¡¡Module¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡60¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

38¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Press¡¡Ctrl£«F5¡¡to¡¡test¡¡the¡¡calculation¡£¡¡When¡¡executed£»¡¡the¡¡testing¡¡console¡¡application¡¡calls¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡test¡¡method¡¡TestSimpleAddition£¨£©£»¡¡which¡¡calls¡¡and¡¡verifies¡¡the¡¡functionality¡¡of¡¡the¡¡Calculator¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡¡library¡£¡¡If¡¡everything¡¡works£»¡¡the¡¡message¡¡will¡¡not¡¡appear¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡Recall¡¡that¡¡the¡¡Main£¨£©¡¡method¡¡is¡¡where¡¡a¡¡project¡¡begins¡¡its¡¡operation¡£¡¡To¡¡make¡¡an¡¡application¡¡do¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡something£»¡¡you¡¡must¡¡add¡¡code¡¡to¡¡its¡¡Main£¨£©¡¡method¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡To¡¡see¡¡that¡¡the¡¡test¡¡also¡¡fails£»¡¡change¡¡the¡¡Add£¨£©¡¡method¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Shared¡¡Function¡¡Add£¨ByVal¡¡number1¡¡As¡¡Integer£»¡¡ByVal¡¡number2¡¡As¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Integer£©¡¡As¡¡Integer¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡number1¡¡*¡¡number2¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡rerun¡¡the¡¡program£»¡¡and¡¡you¡¯ll¡¡see¡¡the¡¡failure¡¡message¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡THE¡¡DEVELOPMENT¡¡CYCLE¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡So¡¡far¡¡in¡¡this¡¡chapter£»¡¡we¡¯ve¡¡dealt¡¡with¡¡three¡¡pieces¡¡of¡¡code£º¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡¡¡The¡¡code¡¡segment¡¡that¡¡implements¡¡the¡¡Add£¨£©¡¡method¡¡performs¡¡the¡¡calculation¡¡operation¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡¡¡The¡¡code¡¡that¡¡represents¡¡a¡¡caller£»¡¡which¡¡could¡¡be¡¡either¡¡the¡¡Windows¡¡application¡¡or¡¡the¡¡console¡¡appli

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cation£»¡¡is¡¡considered¡¡production¡¡code¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡¡¡The¡¡code¡¡that¡¡contains¡¡the¡¡production¡¡code¡¡with¡¡some¡¡verification¡¡routines¡¡represents¡¡testing¡¡code¡£¡¡The¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡testing¡¡code¡¡is¡¡important¡¡because£»¡¡if¡¡the¡¡ponent¡¡implementation¡¡is¡¡altered£»¡¡you¡¡need¡¡to¡¡rerun¡¡only¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡testing¡¡code¡¡to¡¡make¡¡sure¡¡everything¡¡works¡¡as¡¡it¡¡should¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡These¡¡three¡¡pieces¡¡of¡¡code¡¡have¡¡demonstrated¡¡a¡¡plete¡¡development¡¡cycle¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Testing¡¡the¡¡Addition¡¡of¡¡Two¡¡Very¡¡Large¡¡Numbers¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡code¡¡and¡¡projects¡¡have¡¡been¡¡organized£»¡¡but¡¡we¡¯re¡¡missing¡¡some¡¡tests¡£¡¡The¡¡current¡¡test¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡adds¡¡two¡¡simple¡¡numbers¡£¡¡Another¡¡test¡¡could¡¡be¡¡to¡¡add¡¡two¡¡really¡¡big¡¡numbers£»¡¡such¡¡as¡¡2¡¡billion¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡2¡¡billion¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡code¡¡to¡¡test¡¡the¡¡addition¡¡of¡¡two¡¡really¡¡big¡¡numbers¡¡is¡¡shown¡¡in¡¡Figure¡¡2¡­11¡£¡¡Add¡¡this¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Module1¡£vb¡¡in¡¡the¡¡TestCalculator¡¡project¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡61¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡A¡¡PT¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡39¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Addition¡¡result¡¡is¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡stored¡¡in¡¡a¡¡variable¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡¡billion¡¡added¡¡to¡¡2¡¡billion¡¡



¡¡¡¡Public¡¡Sub¡¡TestReallyBigNumbers£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡total¡¡As¡¡Integer¡¡=¡¡Operations¡£Add£¨2000000000£»¡¡2000000000£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡If¡¡£¨total¡¡¡¡4000000000£©¡¡Then¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨¡¨Error¡¡found£¨¡¨¡¡&¡¡total¡¡&¡¡_¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¨£©¡¡should¡¡have¡¡been¡¡£¨4000000000£©¡¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡

¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Verification¡¡of¡¡addition¡¡is¡¡4¡¡billion¡¡¡¡¡¡¡¡If¡¡there¡¡is¡¡an¡¡error£»¡¡a¡¡string¡¡is¡¡built¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡concatenation¡¡techniques¡¡



Figure¡¡2¡­11¡£¡¡¡¡Testing¡¡the¡¡addition¡¡of¡¡two¡¡large¡¡numbers¡¡



¡¡¡¡¡¡¡¡¡¡¡¡The¡¡test¡¡to¡¡add¡¡two¡¡very¡¡large¡¡numbers¡¡is¡¡identical¡¡to¡¡the¡¡test¡¡to¡¡add¡¡two¡¡simple¡¡numbers£»¡¡¡¡

except¡¡for¡¡the¡¡numbers¡¡themselves¡£¡¡The¡¡error¡¡message¡¡is¡¡handled¡¡a¡¡bit¡¡differently¡¡than¡¡in¡¡the¡¡¡¡

previous¡¡code£»¡¡in¡¡that¡¡it¡¡is¡¡built¡¡using¡¡concatenation¡¡techniques¡£¡¡In¡¡the¡¡example£»¡¡a¡¡string¡¡is¡¡¡¡

concatenated¡¡with¡¡an¡¡integer¡¡with¡¡a¡¡string¡£¡¡Visual¡¡Basic¡¡will¡¡automatically¡¡convert¡¡the¡¡integer¡¡¡¡

into¡¡a¡¡string¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Often£»¡¡you¡¡will¡¡write¡¡tests¡¡where¡¡the¡¡only¡¡real¡¡difference¡¡is¡¡the¡¡data¡¡itself£»¡¡not¡¡the¡¡ramifica

tions¡¡of¡¡the¡¡data¡£¡¡Do¡¡you¡¡think¡¡of¡¡adding¡¡two¡¡very¡¡large¡¡numbers¡¡£¨such¡¡as¡¡2¡¡billion¡¡plus¡¡2¡¡billion£©¡¡¡¡

and¡¡two¡¡smaller¡¡numbers¡¡£¨such¡¡as¡¡2¡¡plus¡¡2£©¡¡as¡¡different£¿¡¡No£»¡¡because¡¡to¡¡humans£»¡¡the¡¡major¡¡¡¡

difference¡¡between¡¡2¡¡billion¡¡and¡¡2¡¡is¡¡a¡¡bunch¡¡of¡¡zeros£»¡¡the¡¡result¡¡is¡¡either¡¡4¡¡billion¡¡or¡¡4£»¡¡which¡¡¡¡

seems¡¡very¡¡trivial¡£¡¡Yet¡¡with¡¡respect¡¡to¡¡a¡¡puter£»¡¡4¡¡billion¡¡is¡¡very¡¡different¡¡from¡¡4£»¡¡as¡¡you¡¯ll¡¡¡¡

soon¡¡see¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Before¡¡you¡¡run¡¡the¡¡test£»¡¡you¡¡need¡¡to¡¡add¡¡a¡¡call¡¡to¡¡it¡¡in¡¡the¡¡Main£¨£©¡¡method£º¡¡



¡¡¡¡¡¡¡¡Sub¡¡Main£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestSimpleAddition£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡TestReallyBigNumbers£¨£©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£ReadKey£¨£©¡¡

¡¡¡¡¡¡¡¡End¡¡Sub¡¡



¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡run¡¡the¡¡very¡¡large¡¡number¡¡test¡£¡¡You¡¡will¡¡see¡¡the¡¡output¡¡shown¡¡in¡¡Figure¡¡2¡­12¡£¡¡

¡¡¡¡¡¡¡¡¡¡What¡¡happened£¿¡¡The¡¡problem¡¡has¡¡to¡¡do¡¡with¡¡the¡¡addition¡¡of¡¡very¡¡large¡¡numbers¡¡that¡¡cause¡¡¡¡

an¡¡overflow£»¡¡which¡¡is¡¡a¡¡puter¡¡construct¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡62¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

40¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡N¡¡U¡¡M¡¡B¡¡E¡¡R¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡V¡¡A¡¡L¡¡U¡¡E¡¡¡¡¡¡T¡¡Y¡¡P¡¡E¡¡S¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡2¡­12¡£¡¡¡¡Visual¡¡Basic¡¡Express¡¡highlighting¡¡the¡¡overflow¡¡error¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Understanding¡¡Problems¡¡with¡¡Numeric¡¡Numbers¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡What¡¡you¡¡understand¡¡as¡¡numbers¡¡and¡¡what¡¡the¡¡puter¡¡understands¡¡as¡¡numbers¡¡are¡¡two¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡pletely¡¡different¡¡things¡£¡¡As¡¡a¡¡child£»¡¡you¡¡learn¡¡to¡¡count¡¡starting¡¡at¡¡1¡¡and¡¡ending¡¡at¡¡100£»¡¡which¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡thought¡¡was¡¡a¡¡gargantuan¡¡number¡£¡¡As¡¡you¡¡grew¡¡up£»¡¡you¡¡learned¡¡about¡¡zero¡¡and¡¡numbers¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡less¡¡than¡¡zero¡£¡¡Later£»¡¡you¡¡advanced¡¡to¡¡studying¡¡fractions¡¡and¡¡decimal¡¡values¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Throughout¡¡all¡¡of¡¡this¡¡learning¡¡about¡¡numbers£»¡¡you¡¡considered¡¡the¡¡number¡¡1¡¡and¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡number¡¡1¡£5¡¡to¡¡be¡¡the¡¡same¡¡kind¡¡of¡¡thing¡ªthat¡¡is£»¡¡numbers¡£¡¡Yet¡¡to¡¡the¡¡puter£»¡¡they¡¡are¡¡not¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡same¡¡type¡¡of¡¡values¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡reason¡¡numbers¡¡are¡¡different¡¡to¡¡puters¡¡has¡¡to¡¡do¡¡with¡¡puter¡¡efficiency¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡how¡¡the¡¡puter¡¡stores¡¡numbers¡£¡¡For¡¡those¡¡who¡¡have¡¡learned¡¡the¡¡decimal¡¡system¡¡with¡¡place

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡holders£»¡¡you¡¡start¡¡counting¡¡at¡¡0£»¡¡continue¡¡to¡¡9£»¡¡and¡¡then¡¡the¡¡next¡¡number¡¡is¡¡a¡¡decimal¡¡increment¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡called¡¡10¡£¡¡The¡¡decimal¡¡system¡¡is¡¡originally¡¡accredited¡¡to¡¡the¡¡Babylonians£»¡¡but¡¡they¡¡used¡¡a¡¡decimal¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡system¡¡that¡¡was¡¡sexagesimal¡¡£¨60¡¡unique¡¡identifiers¡¡versus¡¡our¡¡decimal¡¡10¡¡unique¡¡identifiers£©¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡puter¡¡has¡¡a¡¡similar¡¡counting¡¡scheme£»¡¡except¡¡it¡¡is¡¡binary¡¡with¡¡only¡¡two¡¡unique¡¡identi

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡fiers£º¡¡1¡¡and¡¡0¡£¡¡puters¡¡have¡¡two¡¡unique¡¡identifiers¡¡because¡¡they¡¡signify¡¡two¡¡unique¡¡states£º¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡on¡¡and¡¡off¡£¡¡At¡¡the¡¡heart¡¡of¡¡a¡¡puter¡¡is¡¡a¡¡transistor¡¡that¡¡has¡¡the¡¡ability¡¡to¡¡distinguish¡¡between¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡on¡¡and¡¡off£»¡¡there¡¡are¡¡no¡¡¡°sort¡¡of¡±¡¡on¡¡and¡¡¡°sort¡¡of¡±¡¡off¡¡states
·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨0£© ²È£¨0£©
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾­Ñé½±Àø£¬ÈÏÕæдԭ´´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾­Ñé½±ÀøŶ£¡