欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

    面向对象的C++程序设计 第六版 课后习题答案第五章.docx

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

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

    面向对象的C++程序设计 第六版 课后习题答案第五章.docx

    1、面向对象的C+程序设计 第六版 课后习题答案第五章Chapter 5Functions for All Subtasks1. Solutions to Selected Programming ProjectsDetailed solutions to the first 6 projects are presented here. The rest are essentially the same problems, except for what is being converted. Notes about the remaining problems are included.One

    2、of the more important things in programming is planning, even for the simplest program. If the planning is thorough, the coding will be easy, and the only errors likely to be encountered are syntax errors, usually caused by either typing errors, a boundary condition problem (frequently, an off by on

    3、e error), or (we hope not) lack of knowledge of the language details.1 Convert TimeTask: Convert 24 hour time notation to 12 hour AM/PM notation.General comments: The student should note that:a) The convert function has boundary cases that require careful attention.b) ALL of this commentary and plan

    4、ning should be done PRIOR to beginning to write the program. Once this is done the program is almost written. The sooner coding begins, the longer the program will take to do correctly.c) When testing for equality, as inif (12 = hours)put the constant first. The compiler will catch errors such as if

    5、 (12= hours) which are hard to see otherwise. I made many errors of this type while coding this problem./file: ch5prog1.cc/Task: Convert 24 hour time notation to 12 hour AM/PM /notation. /Input: 24 hour time/Output: corresponding 12 hour time, with AM/PM indication/Required: 3 functions: input, conv

    6、ersion, and output./ keep AM/PM information in a char variable/ allow repeat at users option/Notes: conversion function will have a char reference/ parameter to return whether the time is AM/PM. Other/ parameters are required.#include using namespace std;void input( int& hours24, int& minutes);/Prec

    7、ondition: input( hours, minutes ) is called with /arguments capable of being assigned./Postcondition:/ user is prompted for time in 24 hour format:/ HH:MM, where 0 = HH 24, 0 = MM 60./ hours is set to HH, minutes is set to MM./KNOWN BUG: NO CHECKING IS DONE ON INPUT FORMAT. Omitting/the “:” (colon)

    8、from the input format “eats” one character /from the minutes data, and silently gives erroneous /results.void convert( int& hours, char& AMPM );/Precondition: 0 = hours 12, / Note: definitely in the afternoon/ hours is replaced by hours - 12,/ AMPM is set to P/ else if 12 = hours / boundary afternoo

    9、n hour/ AMPM is set to P, / hours is not changed./ else if 0 = hours / boundary morning hour/ hours = hours + 12;/ AMPM = A;/ else / (hours 12)/ AMPM is set to A; / hours is unchangedvoid output( int hours, int minutes, char AMPM );/Precondition:/ 0 hours =12, 0 = minutes 60,/ AMPM = P or AMPM = A/P

    10、ostconditions:/ time is written in the format/ HH:MM AM or HH:MM PMint main()int hours, minutes;char AMPM, ans;doinput( hours, minutes );convert ( hours, AMPM );output( hours, minutes, AMPM );cout Enter Y or y to continue, anything else quits. ans; while ( Y= ans | y = ans );return 0;void input( int

    11、& hours24, int& minutes)char colon;cout Enter 24 hour time in the format HH:MM hours24 colon minutes;/Precondition: 0 = hours = 12,/ hours is replaced by hours - 12,/ AMPM is set to P/ else / (hours 12) / definitely in the afternoon hours = hours - 12; AMPM = P;else if (12 = hours) / boundary aftern

    12、oon hour AMPM = P; / but hours is not changed.else if (0 = hours) / boundary morning hour hours = hours + 12; AMPM = A;else / (hours 12) / definitely morning hour AMPM = A; / hours is unchangedvoid output( int hours, int minutes, char AMPM )cout Time in 12 hour format: endl hours : minutes AMPM M en

    13、dl;A typical run follows:20:33:03:/AW$ a.outEnter 24 hour time in the format HH:MM0:30Time in 12 hour format:12:30 AMEnter Y or y to continue, anything else quits.yEnter 24 hour time in the format HH:MM2:15Time in 12 hour format:2:15 AMEnter Y or y to continue, anything else quits.yEnter 24 hour tim

    14、e in the format HH:MMEnter 24 hour time in the format HH:MM11:30Time in 12 hour format:11:30 AMEnter Y or y to continue, anything else quits.yEnter 24 hour time in the format HH:MM12:30Time in 12 hour format:12:30 PMEnter Y or y to continue, anything else quits.yEnter 24 hour time in the format HH:M

    15、M23:59Time in 12 hour format:11:59 PMEnter Y or y to continue, anything else quits.n20:33:59:/AW$2. Time/ Waiting time/ Problem 2, Savitch, Programming and Problem Solving with C+ 5th/ file ch5.2.cpp/ Program input: current time and a waiting time / each time is number of hours and a number of minut

    16、es./ Program output is is the time the waiting period completes./ Use 24 hour time. Allow user repeat./ Notes: The 24 hour boundary, i.e., when the time wraps to the/ next day is important here./ / Known Bugs: If the completion time would be in a day later / than the next day after the start, this p

    17、rogram gives incorrect / results. / #include void input( int& hours24, int& minutes) using std:cout; using std:cin; using std:endl; char colon; cout Enter 24 hour time in the format HH:MM hours24 colon minutes;void output( int hours, int minutes) using std:cout; using std:cin; using std:endl; cout T

    18、ime in 24 hour format:n hours : minutes endl; int main() using std:cout; using std:cin; using std:endl; int timeHours, timeMinutes, waitHours, waitMinutes, finishHours, finishMinutes; cout Compute completion time from current time and waiting periodn; char ans = y; while (y = ans | Y = ans) cout Cur

    19、rent time:n; input(timeHours, timeMinutes); cout = 24) finishHours %= 24; cout Completion time is in the day following the start timen; finishMinutes%= 60; cout Completion ; output(finishHours, finishMinutes); cout ans; return 0;/*Typical runCompute completion time from current time and waiting peri

    20、odCurrent time:Enter 24 hour time in the format HH:MM12:30Waiting time:Enter 24 hour time in the format HH:MM15:40Completion time is in the day following the start timeCompletion Time in 24 hour format:4:10Enter Y or y to continue, any other haltsyCurrent time:Enter 24 hour time in the format HH:MM8

    21、:30Waiting time:Enter 24 hour time in the format HH:MM15:10Completion Time in 24 hour format:23:40Enter Y or y to continue, any other haltsnPress any key to continue */3. Project 3 Modify project 2 to use 12 hour time.We provide suggestions on how to proceed in solving this problem.This problem is d

    22、ifferent from #2 only in the details of managing 12 hour time. The wait time interval could be any number of hours and minutes, so the output should provide the number of days that elapse from the start time until completion.You may want an input routine that verifies that you have entered legitimat

    23、e 12 hour time data, i.e. hours betwee 1 and 12, minutes between 0 and 59, and includes either an A for AM or a P for PM.Write code to convert the 12 hour start time to 24 hour time and use the code from #3 to computer the finish time.Decide on how to handle finish times that fall in some later day,

    24、 then convert 24 hour time to 12 hour time and output that using code from #2.4. StatisticsCompute average and standard deviation of 4 entries.General remarks are in the code file which I present here:/ file ch5prob4.cc#include using namespace std;/*Task: Write a function that computes average (I wi

    25、ll call this the arithmetic mean or simply the mean) and standard deviation of four scores. The average or mean, avg, is computed as avg = ( s1 + s2 + s3 + s4 ) / 4The standard deviation is computed as where a = avg. Note that some statisticians may wish to use 3 instead of 4. We will use 4.Input: s

    26、cores s1 s2 s3 s4Output: standard deviation and mean.Required: The function is to have 6 parameters. This function calls two others that compute the mean and the std deviation. A driver with a loop should be written to test the function at the users option.*/function declaration (or prototype)/When

    27、used, the math library must be linked to the /executable.#include / for sqrtusing namespace std;void average (double s1, double s2, double s3, double s4, double& avg) avg = ( s1 + s2 + s3 + s4 ) / 4;/ Preconditions: average function must have been called on/the data, and the value of the average pas

    28、sed into the /parameter a/Postconditions: Standard deviation is passed back in /the variable stdDevvoid sD (double s1, double s2, double s3, double s4, double a, double& stdDev) stdDev = sqrt( (s1 - a)*(s1 - a) + (s2 - a)*(s2 - a) + (s3 - a)*(s3 - a) + (s4 - a)*(s4 - a) )/4 ;void statistics( double s1, double s2, double s3, double s4, double& avg, double& stdDev );/Preconditions: this function is called with any set of /values. Very large or very small numbers are subject to /errors in the c


    注意事项

    本文(面向对象的C++程序设计 第六版 课后习题答案第五章.docx)为本站会员主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(点击联系客服),我们立即给予删除!

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




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 冰点文库 网站版权所有

    经营许可证编号:鄂ICP备19020893号-2


    收起
    展开