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