ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:230.22KB ,
资源ID:2453736      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-2453736.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(c++程序设计原理与实践第三章课后答案.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

c++程序设计原理与实践第三章课后答案.docx

1、c+程序设计原理与实践第三章课后答案“std_lib_facilities.h”这个头文件是c+程序设计原理与实践一直用到的头文件,要将此头文件放在你的文件目录中,下面我给出这个头文件:/ This is a standard library support code to the chapters of the book/ Programming - Principles and Practice Using C+ by Bjarne Stroustrup/#ifndef STD_LIB_FACILITIES_GUARD#define STD_LIB_FACILITIES_GUARD 1#i

2、nclude using namespace std;/-/ The call to keep_window_open() is needed on some Windows machines to prevent/ them from closing the window before you have a chance to read the output. inline void keep_window_open() cin.get();/-#endif / STD_LIB_FACILITIES_GUARD将上述代码拷贝到记事本中,把后缀.txt改为.h,放在你的根目录即可引用。第3章

3、对象,类型和值第6 题 #includestd_lib_facilities.hint main()int a;int b;int c;int t;coutabc;if(ab)/如果a大于b,将a,b交换.t=a;a=b;b=t;if(ac)t=a;a=c;c=t;if(bc)t=b;b=c;c=t;couta,b,cendl;下面是vc6.0的运行结果:第7题#include #includeusing namespace std;/-int main() cout first secondthird; / 读入三个字符串。if(firstsecond)space=first;first=s

4、econd;second=space;if(firstthird)space=first;first=third;third=space;if(secondthird)space=second;second=third;third=space;coutfirst,second,thirdendl;return 0;/-Vc6.0运行结果如下:第8题#includestd_lib_facilities.hint main()couta;if(a%2=0) cout您输入的数:a是一个偶数n;else cout您输入的数:a是一个奇数n;return 0;Vc6.0运行结果如下:第9题#inclu

5、de#includeusing namespace std;int main()string str;coutstr;if(str=zero) cout对应的阿拉伯数字是:0n;else if(str=one) cout对应的阿拉伯数字是:1n; else if(str=two) cout对应的阿拉伯数字是:2n; else if(str=three) cout对应的阿拉伯数字是:3n; else if(str=four) cout对应的阿拉伯数字是:4n; else cout ch; / note that skips whitespace (space, newline, tab, etc

6、.) switch (ch) case ;: / for print case q: / for quit case (: case ): case +: case -: case *: case /: case%: return Token(ch); / let each character represent itself case .: case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:case8: case 9: cin.putback(ch); / put digit back into the input

7、stream double val; cin val; / read a floating-point number return Token(s,val); / let 8 represent a number default: perror(Bad token); /-Token_stream ts; / provides get() and putback() /-double expression(); / declaration so that primary() can call expression()/-/ deal with numbers and parenthesesdo

8、uble primary() Token t = ts.get(); switch (t.kind) case (: / handle ( expression ) double d = expression(); t = ts.get(); if (t.kind != ) perror() expected); return d; case s: / 按照惯例用8代替数字类型,但本程序需要8参与运算,所以这里用s表示数字类型。 return t.value; / return the numbers value default: perror(primary expected); /-/ d

9、eal with *, /, and %double term() double left = primary(); Token t = ts.get(); / get the next token from token stream while(true) switch (t.kind) case *: left *= primary(); t = ts.get(); break; case /: double d = primary(); if (d = 0) perror(divide by zero); left /= d; t = ts.get(); break; case%: in

10、t d = primary(); if (d = 0) perror(divide by zero); left =int(left) % d; /注意取余时这里进行了强制性转换,可能出现数据截断。 t = ts.get(); break; default: ts.putback(t); / put t back into the token stream return left; /-/ deal with + and -double expression() double left = term(); / read and evaluate a Term Token t = ts.get(

11、); / get the next token from token stream while(true) switch(t.kind) case +: left += term(); / evaluate Term and add t = ts.get(); break; case -: left -= term(); / evaluate Term and subtract t = ts.get(); break; default: ts.putback(t); / put t back into the token stream return left; / finally: no mo

12、re + or -: return the answer /-int main()/8被定义为数字类型,当与数字8有关的运算无法运行,本程序中将数字的类型定义为s。try cout欢迎使用简易计算器( *_* ):n; cout本计算程序能实现+,-,*,/,%以及括弧运算n; cout请您输入一个数学表达式(本程序精确到双精度),以;结束,以q退出n; while (cin) Token t = ts.get(); double val; if (t.kind = q) break; / q for quit if (t.kind = ;) / ; for print now. cout =

13、 val n; else ts.putback(t); val = expression(); keep_window_open();catch (exception& e) cerr error: e.what() n; keep_window_open(); return 1;catch (.) cerr Oops: unknown exception!n; keep_window_open(); return 2;return 0;/-Vc6.0环境下的运行结果如图:第11题#includestd_lib_facilities.hint main()coutABCDEF;int AWS=

14、1,BWS=1,CWS=1,DWS=1,EFWS=1,N=10;/定义初始位数和进位位数。while(A=N)/当A=10时判断为两位数。AWS+=1;N=N*10;/两位数的进位为100.N=10;/一位数的进位仍旧赋值为10.while(B=N)BWS+=1;N=N*10;N=10;while(C=N)CWS+=1;N=N*10;N=10;while(D=N)DWS+=1;N=N*10;N=10;while(F*2+E=N)EFWS+=1;N=N*10;int X=AWS;/将x通过比较赋值为,所有位数最高的那位。if(XBWS)X=BWS;if(XCWS)X=CWS;if(XDWS)X=

15、DWS;if(XEFWS)X=EFWS;int ZWS=X+1;int a=ZWS-AWS;cout0)cout ;a-=1;if(A=1)coutA1)coutA pennies.n;cout0)cout ;a-=1;if(B=1)coutB1)coutB nickels.n;cout0)cout ;a-=1;if(C=1)coutC1)coutC dimes.n;cout0)cout ;a-=1;if(D=1)coutD1)coutD quarters.n;cout0)cout ;a-=1;if(F*2+E=1)coutF*2+E1)coutF*2+E half dollars.n;cout合计值:$:double(A+5*B+10*C+25*D)/100+double(F*2+E)/2endl;coutZWS AWS BWS CWS DWS EFWS n;/输出各数的位数-检错用。keep_window_open();Vc6.0下运行结果如下:

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

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