欢迎来到天天文库
浏览记录
ID:39670931
大小:744.00 KB
页数:103页
时间:2019-07-08
《VisualC%2B%2B2005入门经典习题答案》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、Chapter21.WriteanISO/ANSIC++programthataskstheusertoenteranumberandthenprintsitout,usinganintegerasalocalvariable.//Soln2_1.cpp#includeusingstd::cin;usingstd::cout;usingstd::endl;intmain(){intnumber;cout<<"Enteranumber:";cin>>number;cout<<"Thankyou.Yournumberwas"<2、umber;cout<3、.//Soln2_2.cpp#includeusingstd::cin;usingstd::cout;usingstd::endl;intmain(){intnumber;cout<<"Enteranumber:";cin>>number;cout<<"Thankyou.Theremainderafterdividingyournumberby8is"<>3)<<3);cout<4、inordertoshowtheprecedenceandassociativity:1+2+3+416*4/2*3a>b?a:c>d?e:fa&b&&c&dParenthesized:(((1+2)+3)+4)(((16*4)/2)*3)((a>b)?a:((c>d)?e:f))((a&b)&&(c&d))4.Createaprogramthatwillcalculatetheaspectratioofyourcomputerscreen,giventhewidthandheightinpixels,usingthefollowingstate5、ments:intwidth=1280;intheight=1024;doubleaspect=width/height;//Soln2_4.cpp#includeusingstd::cout;usingstd::endl;intmain(){intwidth=1280;intheight=1024;doubleaspect=width/height;cout<<"Theaspectratiois"<6、lyouget?Isitsatisfactory—andifnot,howcouldyoumodifythecode,withoutaddinganymorevariables?Theoutputis:Theaspectratiois1Thisisbecausethedivisionofthewidthbytheheightisanintegeroperationwithanintegerresult.Theintegerresultisthenpromotedtotypedoublebeforebeingstoredinaspect.Youca7、ngetamoreaccurateresultbychangingthestatementto:doubleaspect=static_cast(width)/height;Castingwidthtotypedoubleforcesthedivideoperationtobecarriedoutwithvaluesoftypedoublesothevalueofheightwillbepromotedtotypedoublebeforethedivision.Ofcourse,youcouldalsomaketheprogram8、moregeneralbyreadingthewidthandheightfromthecommandline.Youjustneedt
2、umber;cout<3、.//Soln2_2.cpp#includeusingstd::cin;usingstd::cout;usingstd::endl;intmain(){intnumber;cout<<"Enteranumber:";cin>>number;cout<<"Thankyou.Theremainderafterdividingyournumberby8is"<>3)<<3);cout<4、inordertoshowtheprecedenceandassociativity:1+2+3+416*4/2*3a>b?a:c>d?e:fa&b&&c&dParenthesized:(((1+2)+3)+4)(((16*4)/2)*3)((a>b)?a:((c>d)?e:f))((a&b)&&(c&d))4.Createaprogramthatwillcalculatetheaspectratioofyourcomputerscreen,giventhewidthandheightinpixels,usingthefollowingstate5、ments:intwidth=1280;intheight=1024;doubleaspect=width/height;//Soln2_4.cpp#includeusingstd::cout;usingstd::endl;intmain(){intwidth=1280;intheight=1024;doubleaspect=width/height;cout<<"Theaspectratiois"<6、lyouget?Isitsatisfactory—andifnot,howcouldyoumodifythecode,withoutaddinganymorevariables?Theoutputis:Theaspectratiois1Thisisbecausethedivisionofthewidthbytheheightisanintegeroperationwithanintegerresult.Theintegerresultisthenpromotedtotypedoublebeforebeingstoredinaspect.Youca7、ngetamoreaccurateresultbychangingthestatementto:doubleaspect=static_cast(width)/height;Castingwidthtotypedoubleforcesthedivideoperationtobecarriedoutwithvaluesoftypedoublesothevalueofheightwillbepromotedtotypedoublebeforethedivision.Ofcourse,youcouldalsomaketheprogram8、moregeneralbyreadingthewidthandheightfromthecommandline.Youjustneedt
3、.//Soln2_2.cpp#includeusingstd::cin;usingstd::cout;usingstd::endl;intmain(){intnumber;cout<<"Enteranumber:";cin>>number;cout<<"Thankyou.Theremainderafterdividingyournumberby8is"<>3)<<3);cout<4、inordertoshowtheprecedenceandassociativity:1+2+3+416*4/2*3a>b?a:c>d?e:fa&b&&c&dParenthesized:(((1+2)+3)+4)(((16*4)/2)*3)((a>b)?a:((c>d)?e:f))((a&b)&&(c&d))4.Createaprogramthatwillcalculatetheaspectratioofyourcomputerscreen,giventhewidthandheightinpixels,usingthefollowingstate5、ments:intwidth=1280;intheight=1024;doubleaspect=width/height;//Soln2_4.cpp#includeusingstd::cout;usingstd::endl;intmain(){intwidth=1280;intheight=1024;doubleaspect=width/height;cout<<"Theaspectratiois"<6、lyouget?Isitsatisfactory—andifnot,howcouldyoumodifythecode,withoutaddinganymorevariables?Theoutputis:Theaspectratiois1Thisisbecausethedivisionofthewidthbytheheightisanintegeroperationwithanintegerresult.Theintegerresultisthenpromotedtotypedoublebeforebeingstoredinaspect.Youca7、ngetamoreaccurateresultbychangingthestatementto:doubleaspect=static_cast(width)/height;Castingwidthtotypedoubleforcesthedivideoperationtobecarriedoutwithvaluesoftypedoublesothevalueofheightwillbepromotedtotypedoublebeforethedivision.Ofcourse,youcouldalsomaketheprogram8、moregeneralbyreadingthewidthandheightfromthecommandline.Youjustneedt
4、inordertoshowtheprecedenceandassociativity:1+2+3+416*4/2*3a>b?a:c>d?e:fa&b&&c&dParenthesized:(((1+2)+3)+4)(((16*4)/2)*3)((a>b)?a:((c>d)?e:f))((a&b)&&(c&d))4.Createaprogramthatwillcalculatetheaspectratioofyourcomputerscreen,giventhewidthandheightinpixels,usingthefollowingstate
5、ments:intwidth=1280;intheight=1024;doubleaspect=width/height;//Soln2_4.cpp#includeusingstd::cout;usingstd::endl;intmain(){intwidth=1280;intheight=1024;doubleaspect=width/height;cout<<"Theaspectratiois"<6、lyouget?Isitsatisfactory—andifnot,howcouldyoumodifythecode,withoutaddinganymorevariables?Theoutputis:Theaspectratiois1Thisisbecausethedivisionofthewidthbytheheightisanintegeroperationwithanintegerresult.Theintegerresultisthenpromotedtotypedoublebeforebeingstoredinaspect.Youca7、ngetamoreaccurateresultbychangingthestatementto:doubleaspect=static_cast(width)/height;Castingwidthtotypedoubleforcesthedivideoperationtobecarriedoutwithvaluesoftypedoublesothevalueofheightwillbepromotedtotypedoublebeforethedivision.Ofcourse,youcouldalsomaketheprogram8、moregeneralbyreadingthewidthandheightfromthecommandline.Youjustneedt
6、lyouget?Isitsatisfactory—andifnot,howcouldyoumodifythecode,withoutaddinganymorevariables?Theoutputis:Theaspectratiois1Thisisbecausethedivisionofthewidthbytheheightisanintegeroperationwithanintegerresult.Theintegerresultisthenpromotedtotypedoublebeforebeingstoredinaspect.Youca
7、ngetamoreaccurateresultbychangingthestatementto:doubleaspect=static_cast(width)/height;Castingwidthtotypedoubleforcesthedivideoperationtobecarriedoutwithvaluesoftypedoublesothevalueofheightwillbepromotedtotypedoublebeforethedivision.Ofcourse,youcouldalsomaketheprogram
8、moregeneralbyreadingthewidthandheightfromthecommandline.Youjustneedt
此文档下载收益归作者所有