欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    C++ Primer Plus中文第五版编程练习答案-非扫描.pdf

    • 资源ID:70019412       资源大小:315.11KB        全文页数:65页
    • 资源格式: PDF        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    C++ Primer Plus中文第五版编程练习答案-非扫描.pdf

    Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 1 of 65 September 2,2004Chapter 2/?pe2-2.cpp#include?int?main(void)?using?namespace?std;?cout?furlongs;?double?feet;?feet?=?220?*?furlongs;?cout?furlongs?furlongs?=?feet?feetn;?return?0;/?pe2-3.cpp#include?using?namespace?std;void?mice();void?run();int?main()?mice();?mice();?run();?run();?return?0;void?mice()?cout?Three?blind?micen;void?run()?cout?See?how?they?runn;/?pe2-4.cpp#include?double?C_to_F(double);int?main()?using?namespace?std;?cout?C;?double?F;?F?=?C_to_F(C);C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描1/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 2 of 65 September 2,2004?cout?C?degrees?Celsius?=?F?degrees?Fahrenheitn;?return?0;double?C_to_F(double?temp)?return?1.8?*?temp?+?32.0;Chapter 3/?pe3-1.cpp#include?const?int?Inch_Per_Foot?=?12;int?main(void)?using?namespace?std;/?Note:?some?environments?dont?support?the?backspace?character?cout?ht_inch;?int?ht_feet?=?ht_inch?/?Inch_Per_Foot;?int?rm_inch?=?ht_inch?%?Inch_Per_Foot;?cout?Your?height?is?ht_feet?feet,?;?cout?rm_inch?inch(es).n;?return?0;/?pe3-3.cpp#include?const?double?MINS_PER_DEG?=?60.0;const?double?SECS_PER_MIN?=?60.0;int?main()?using?namespace?std;?int?degrees;?int?minutes;?int?seconds;?double?latitude;?cout?Enter?a?latitude?in?degrees,?minutes,?and?seconds:n;?cout?degrees;?cout?minutes;?cout?seconds;?latitude?=?degrees?+?(minutes?+?seconds?/?SECS_PER_MIN)/MINS_PER_DEG;?cout?degrees?degrees,?minutes?minutes,?seconds?seconds?=?latitude?degreesn;?return?0;?C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描2/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 3 of 65 September 2,2004/?pe3-5.cpp#include?int?main(void)?using?namespace?std;?cout?miles;?cout?gallons;?cout?Your?car?got?miles?/?gallons;?cout?miles?per?gallon.n;?return?0;/?pe3-6.cpp#include?const?double?KM100_TO_MILES?=?62.14;const?double?LITERS_PER_GALLON?=?3.875;int?main?(?void?)?using?namespace?std;?double?euro_rating;?double?us_rating;?cout?euro_rating;?/?divide?by?LITER_PER_GALLON?to?get?gallons?per?100-km?/?divide?by?KM100_TO_MILES?to?get?gallons?per?mile?/?invert?result?to?get?miles?per?gallon?us_rating?=?(LITERS_PER_GALLON?*?KM100_TO_MILES)?/?euro_rating;?cout?euro_rating?liters?per?100?km?is?;?cout?us_rating?miles?per?gallon.n;?return?0;Chapter 4/?pe4-2.cpp?-?storing?strings?in?string?objects#include?#include?int?main()?using?namespace?std;?string?name;?string?dessert;?cout?Enter?your?name:n;?getline(cin,?name);?/?reads?through?newline?cout?Enter?your?favorite?dessert:n;?getline(cin,?dessert);?cout?I?have?some?delicious?dessert;?cout?for?you,?name?.n;?return?0;?C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描3/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 4 of 65 September 2,2004/?pe4-3.cpp?-?storing?strings?in?char?arrays#include?#include?const?int?SIZE?=?20;int?main()?using?namespace?std;?char?firstNameSIZE;?char?lastNameSIZE;?char?fullName2*SIZE?+?1;?cout?firstName;?cout?lastName;?strncpy(fullName,lastName,SIZE);?strcat(fullName,?,?);?strncat(fullName,?firstName,?SIZE);?fullNameSIZE?-?1?=?0;?cout?Heres?the?information?in?a?single?string:?fullName?endl;?return?0;?/?pe4-5.cpp/?a?candybar?structurestruct?CandyBar?char?brand40;?double?weight;?int?calories;#include?int?main()?using?namespace?std;?/introduces?namespace?std?CandyBar?snack?=?Mocha?Munch,?2.3,?350?;?cout?Brand?name:?snack.brand?endl;?cout?Weight:?snack.weight?endl;?cout?Calories:?snack.calories?endl;?return?0;/?pe4-7.ccp#include?const?int?Slen?=?70;struct?pizza?char?nameSlen;?float?diameter;?float?weight;int?main(void)C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描4/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 5 of 65 September 2,2004?using?namespace?std;?pizza?pie;?cout?What?is?the?name?of?the?pizza?company?;?cin.getline(pie.name,?Slen);?cout?pie.diameter;?cout?pie.weight;?cout?Company:?pie.name?n;?cout?Diameter:?pie.diameter?inchesn;?cout?Weight:?pie.weight?ouncesn;?return?0;Chapter 5/?pe5-2.cpp#include?int?main(void)?using?namespace?std;?double?sum?=?0.0;?double?in;?cout?in;?while?(in?!=?0)?sum?+=?in;?cout?Running?total?=?sum?n;?cout?in;?cout?Bye!n;?return?0;/?pe5-4.cpp/?book?sales#include?const?int?MONTHS?=?12;const?char?*?monthsMONTHS?=?January,?February,?March,?April,?May,?June,?July,?August,?September,?October,?November,?December;int?main()?using?namespace?std;?/introduces?namespace?std?int?salesMONTHS;?int?month;?cout?Enter?the?monthly?sales?for?C+?for?Fools:n;?for?(month?=?0;?month?MONTHS;?month+)?cout?Sales?for?monthsmonth?salesmonth;?C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描5/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 6 of 65 September 2,2004?double?total?=?0.0;?for?(month?=?0;?month?MONTHS;?month+)?total?+=?salesmonth;?cout?Total?sales:?total?endl;?return?0;/?pe5-6.cpp#include?struct?car?char?name20;?int?year;int?main(void)?using?namespace?std;?int?n;?cout?n;?while(cin.get()?!=?n)?/?get?rid?of?rest?of?line?;?car?*?pc?=?new?car?n;?int?i;?for?(i?=?0;?i?n;?i+)?cout?Car?#?(i?+?1)?:n;?cout?Please?enter?the?make:?;?cin.getline(pci.name,20);?cout?pci.year;?while(cin.get()?!=?n)?/?get?rid?of?rest?of?line?;?cout?Here?is?your?collection:n;?for?(i?=?0;?i?n;?i+)?cout?pci.year?pci.name?n;?delete?pc;?return?0;/?pe5-7.cpp?-?count?words?using?C-style?string#include?#include?/?prototype?for?strcmp()const?int?STR_LIM?=?50;int?main()?using?namespace?std;?char?wordSTR_LIM;?int?count?=?0;?cout?word?&?strcmp(done,?word)?+count;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描6/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 7 of 65 September 2,2004?cout?You?entered?a?total?of?count?words.n;?return?0;?/?pe5-9.cpp/nested?loops#include?int?main()?using?namespace?std;?/introduces?namespace?std?int?rows;?int?row;?int?col;?int?periods;?cout?rows;?for?(row?=?1;?row?=?rows;?row+)?periods?=?rows?-?row;?for?(col?=?1;?col?=?periods;?col+)?cout?.;?/?col?already?has?correct?value?for?next?loop?for?(?;?col?=?rows;?col+)?cout?*;?cout?endl;?return?0;Chapter 6/?pe6-1.cpp#include?#include?int?main(?)?using?namespace?std;?/introduces?namespace?std?char?ch;?cin.get(ch);?while(ch?!=?)?if?(!isdigit(ch)?if?(isupper(ch)?ch?=?tolower(ch);?else?if?(islower(ch)?ch?=?toupper(ch);?cout?ch;?cin.get(ch);?return?0;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描7/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 8 of 65 September 2,2004/?pe6-3.cpp#include?int?main(void)?using?namespace?std;?cout?Please?enter?one?of?the?following?choices:n;?cout?c)?carnivore?p)?pianistn?ch;?while?(ch?!=?c?&?ch?!=?p?&?ch?!=?t?&?ch?!=?g)?cout?ch;?switch?(ch)?case?c?:?cout?A?cat?is?a?carnivore.n;?break;?case?p?:?cout?Radu?Lupu?is?a?pianist.n;?break;?case?t?:?cout?A?maple?is?a?tree.n;?break;?case?g?:?cout?Golf?is?a?game.n;?break;?default?:?cout?The?program?shouldnt?get?here!n;?return?0;/?pe6-5.cpp/?Neutronia?taxation#include?const?double?LEV1?=?5000;const?double?LEV2?=?15000;const?double?LEV3?=?35000;const?double?RATE1?=?0.10;const?double?RATE2?=?0.15;const?double?RATE3?=?0.20;int?main(?)?using?namespace?std;?double?income;?double?tax;?cout?income;?if?(income?=?LEV1)?tax?=?0;?else?if?(income?=?LEV2)?tax?=?(income?-?LEV1)?*?RATE1;?else?if?(income?=?LEV3)?tax?=?RATE1?*?(LEV2?-?LEV1)?+?RATE2?*?(income?-?LEV2);?else?tax?=?RATE1?*?(LEV2?-?LEV1)?+?RATE2?*?(LEV3?-?LEV2)?+?RATE3?*?(income?-?LEV3);?cout?You?owe?Neutronia?tax?tvarps?in?taxes.n;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描8/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 9 of 65 September 2,2004?return?0;/?pe6-7.cpp#include?#include?int?main()?using?namespace?std;?string?word;?char?ch;?int?vowel?=?0;?int?consonant?=?0;?int?other?=?0;?cout?word;?while?(?word?!=?q)?ch?=?tolower(word0);?if?(isalpha(ch)?if?(ch?=?a?|?ch?=?e?|?ch?=?i?|?ch?=?o?|?ch?=?u)?vowel+;?else?consonant+;?else?other+;?cin?word;?cout?vowel?words?beginning?with?vowelsn;?cout?consonant?words?beginning?with?consonantsn;?cout?other?othersn;?return?0;?/?pe6-8.cpp?-?counting?characters#include?#include?/?file?I/O?suppport#include?/?support?for?exit()const?int?SIZE?=?60;int?main()?using?namespace?std;?char?filenameSIZE;?char?ch;?ifstream?inFile;?/?object?for?handling?file?input?cout?Enter?name?of?data?file:?;?cin.getline(filename,?SIZE);?inFile.open(filename);?/?associate?inFile?with?a?file?if?(!inFile.is_open()?/?failed?to?open?file?cout?Could?not?open?the?file?filename?endl;?cout?ch;?/?get?first?value?while?(inFile.good()?/?while?input?good?and?not?at?EOF?count+;?/?one?more?item?read?inFile?ch;?/?get?next?value?cout?count?characters?in?filename?endl;?inFile.close();?/?finished?with?the?file?return?0;Chapter 7/pe7-1.cpp?-?harmonic?mean#include?double?h_mean(double?x,?double?y);int?main(void)?using?namespace?std;?double?x,y;?cout?x?y?&?x?*?y?!=?0)?cout?harmonic?mean?of?x?and?y?=?h_mean(x,y)?x?&?x?!=?0)?cin?y;?if?(y?=?0)?break;?.*/?cout?Byen;?return?0;double?h_mean(double?x,?double?y)?return?2.0?*?x?*?y?/?(x?+?y);/?pe7-3.cpp#include?struct?box?char?maker40;?float?height;?float?width;?float?length;?float?volume;void?showbox(box?b);C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描10/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 11 of 65 September 2,2004void?setbox(box?*?pb);int?main(void)?box?carton?=?Bingo?Boxer,?2,?3,?5;?/?no?volume?provided?setbox(&carton);?showbox(carton);?return?0;void?showbox(box?b)?using?namespace?std;?cout?Box?maker:?b.maker?nheight:?b.height?nlwidth:?b.width?nlength:?b.length?nvolume:?b.volume?volume?=?pb-height?*?pb-width?*?pb-length;/?pe7-4.cpp?-probability of?winning#include?long?double?probability(unsigned?numbers,?unsigned?picks);int?main()?using?namespace?std;?double?total,?choices;?double?mtotal;?double?probability1,?probability2;?cout?total?choices)?&?choices?=?total)?cout?mtotal)?break;?cout?The?chances?of?getting?all?choices?picks?is?one?in?(probability1?=?probability(total,?choices)?)?.n;?cout?The?chances?of?getting?the?megaspot?is?one?in?(probability2?=?probability(mtotal,?1)?)?.n;?cout?You?have?one?chance?in?;?cout?probability1?*?probability2;?/?compute?the?probability?cout?of?winning.n;?cout?Next?set?of?numbers?(q?to?quit):?;?cout?0;?n-,?p-)?result?=?result?*?n?/?p?;?return?result;/?pe7-6.cpp#include?int?Fill_array(double?ar,?int?size);void?Show_array(const?double?ar,?int?size);void?Reverse_array(double?ar,?int?size);const?int?LIMIT?=?10;int?main(?)?using?namespace?std;?double?valuesLIMIT;?int?entries?=?Fill_array(values,?LIMIT);?cout?Array?values:n;?Show_array(values,?entries);?cout?Array?reversed:n;?Reverse_array(values,?entries);?Show_array(values,?entries);?cout?All?but?end?values?reversed:n;?Reverse_array(values?+?1,?entries?-?2);?Show_array(values,?entries);?return?0;int?Fill_array(double?ar,?int?size)?using?namespace?std;?int?n;?cout?Enter?up?to?size?values?(q?to?quit):n;?for?(n?=?0;?n?arn;?if?(!cin)?break;?return?n;void?Show_array(const?double?ar,?int?size)?using?namespace?std;?int?n;?for?(n?=?0;?n?size;?n+)?cout?arn;?if?(n?%?8?=?7)?cout?endl;?else?cout?;?if?(n?%?8?!=?0)?cout?endl;void?Reverse_array(double?ar,?int?size)?int?i,?j;?double?temp;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描12/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 13 of 65 September 2,2004?for?(i?=?0,?j?=?size?-?1;?i?j;?i+,?j-)?temp?=?ari;?ari?=?arj;?arj?=?temp;?/pe7-9.cpp#include?double?calculate(double?x,?double?y,?double?(*pf)(double,?double);double?add(double?x,?double?y);double?sub(double?x,?double?y);double?mean(double?x,?double?y);int?main(void)?using?namespace?std;?double?(*pf3)(double,double)?=?add,?sub,?mean;?char?*?op3?=?sum,?difference,?mean;?double?a,?b;?cout?a?b)?/?using?function?names?cout?calculate(a,?b,?add)?=?sumn;?cout?calculate(a,?b,?mean)?=?meann;?/?using?pointers?for?(i?=?0;?i?3;?i+)?cout?calculate(a,?b,?pfi)?=?opi?n;?cout?Done!n;?return?0;double?calculate(double?x,?double?y,?double?(*pf)(double,?double)?return?(*pf)(x,?y);double?add(double?x,?double?y)?return?x?+?y;double?sub(double?x,?double?y)?return?x?-?y;double?mean(double?x,?double?y)?return?(x?+?y)?/?2.0;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描13/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 14 of 65 September 2,2004Chapter 8/?pe8-1.cpp#include?void?silly(const?char?*?s,?int?n?=?0);int?main(void)?using?namespace?std;?char?*?p1?=?Why?me?n;?silly(p1);?for?(int?i?=?0;?i?3;?i+)?cout?i?=?in;?silly(p1,?i);?cout?Donen;?return?0;void?silly(const?char?*?s,?int?n)?using?namespace?std;?static?int?uses?=?0;?int?lim?=?+uses;?if?(n?=?0)?lim?=?1;?for?(int?i?=?0;?i?lim;?i+)?cout?s;/?pe8-4.cpp#include?#include?/?for?strlen(),?strcpy()using?namespace?std;struct?stringy?char?*?str;?/?points?to?a?string?int?ct;?/?length?of?string?(not?counting?0)?;void?show(const?char?*str,?int?cnt?=?1);void?show(const?stringy?&?bny,?int?cnt?=?1);void?set(stringy?&?bny,?const?char?*?str);int?main(void)?stringy?beany;?char?testing?=?Reality?isnt?what?it?used?to?be.;?set(beany,?testing);?/?first?argument?is?a?reference,?/?allocates?space?to?hold?copy?of?testing,?/?sets?str?member?of?beany?to?point?to?the?/?new?block,?copies?testing?to?new?block,?/?and?sets?ct?member?of?beany?show(beany);?/?prints?member?string?once?show(beany,?2);?/?prints?member?string?twice?testing0?=?D;?testing1?=?u;?show(testing);?/?prints?testing?string?onceC+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描14/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 15 of 65 September 2,2004?show(testing,?3);?/?prints?testing?string?thrice?show(Done!);?return?0;void?show(const?char?*str,?int?cnt)?while(cnt-?0)?cout?str?0)?cout?bny.str?endl;?void?set(stringy?&?bny,?const?char?*?str)?bny.ct?=?strlen(str);?bny.str?=?new?charbny.ct+1;?strcpy(bny.str,?str);/?pe8-5.cpp#include?template?T?max5(T?ar)?int?n;?T?max?=?ar0;?for?(n?=?1;?n?max)?max?=?arn;?return?max;const?int?LIMIT?=?5;int?main(?)?using?namespace?std;?double?ardLIMIT?=?-3.4,?8.1,?-76.4,?34.4,?2.4;?int?ariLIMIT?=?2,?3,?8,?1,?9;?double?md;?int?mi;?md?=?max5(ard);?mi?=?max5(ari);?cout?md?=?md?endl;?cout?mi?=?mi?endl;?return?0;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描15/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 16 of 65 September 2,2004Chapter 9PE 9-1 /?pe9-golf.h?-?for?pe9-1.cppconst?int?Len?=?40;struct?golf?char?fullnameLen;?int?handicap;/?non-interactive?version/?function?sets?golf?structure?to?provided?name,?handicap/?using?values?passed?as?arguments?to?the?functionvoid?setgolf(golf?&?g,?const?char?*?name,?int?hc);/?interactive?version/?function?solicits?name?and?handicap?from?user/?and?sets?the?members?of?g?to?the?values?entered/?returns?1?if?name?is?entered,?0?if?name?is?empty?stringint?setgolf(golf?&?g);/?function?resets?handicap?to?new?valuevoid?handicap(golf?&?g,?int?hc);/?function?displays?contents?of?golf?structurevoid?showgolf(const?golf?&?g);/?pe9-golf.cpp?-?for?pe9-1.cpp#include?#include?pe9-golf.h#include?/?function?solicits?name?and?handicap?from?user/?returns?1?if?name?is?entered,?0?if?name?is?empty?stringint?setgolf(golf?&?g)std:cout?Please?enter?golfers?full?name:?;std:cin.getline(g.fullname,?Len);?if?(g.fullname0?=?0)?return?0;?/?premature?terminationstd:cout?Please?e

    注意事项

    本文(C++ Primer Plus中文第五版编程练习答案-非扫描.pdf)为本站会员(qwe****56)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开