C++面向对象程序设计双语教程(第3版)-参考答案【ch02】Basic Failities - Shifting from C to C++ Programs.docx
《C++面向对象程序设计双语教程(第3版)-参考答案【ch02】Basic Failities - Shifting from C to C++ Programs.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计双语教程(第3版)-参考答案【ch02】Basic Failities - Shifting from C to C++ Programs.docx(12页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、ShjftingChapter 2 Bas ic Faci I ities from C to C+ Programs1. Answer the fol lowing questions:1-5 bdcad6. ait:2. Read the fol lowing program and comp Iete册血11& vi6如丽. _ . . . _ _ _ _ . J _ * , * * : , .:. y. :.:;.: ;?.,*. ;*,;- *, ,;V.- !.*: : :using namespace std;int& put(int n); /put value into th
2、e array. -.* , . * * ,.;mt get(int n); /obtain a value from the array, , . * (. .* * .*. .int vals101;intend;二: . : * . * *_*.* * . J a : / .1 voidmamO(put(0) =10; /put value into the array .: . * * *_ * * ( . *, , : . . . /.- - - , : .put =20; put =30;cout get(O)eiidl;cout get(l)endl; 涔泰西琮萌冬0山;1mmm
3、;,mput(12) = l; /out of bound 8kget(12)vvendl; . .略。3.Wr ite out the output of the fol lowing programs. (1)int i当后蕊港;: : O:二:. : :二.;:二mt&r-1; . . : . . . a a .: . . . . . _ . , . . . . . . : .、. : . :口=9;三二三三:三.= A 工 : : : :丁1 = 10二二二?.:;coUtvvK之;iWVeiidl; :;:; . . . , . . .cout&r 0:#有两个实根xl =(-b +
4、 math, sqrt(discriminant) / (2*a)x2 = (-b - math, sqrt(discriminant) / (2*a) return 2, xl, x2elif discriminant 二二 0:#只有一个实根xl = -b / (2*a)return 1, xl, Noneelse:#没有实根return 0, None, None#测试a = 2b = -5c = -3root_count, xl, x2 = solve_quadratic_equation(a, b, c)print (、根的数量: root_count)if root_count 0
5、:print (、根 1: xl)if root count 1:print (、根 2: x2)以上函数使用判别式确定根的情况,返回根的数量以及根的数值(如果有的话)。若根 数大于3则会打印根的值。14. Write a program that pI ays the game “guess the number? as fol lows. Your programchooses the number to be guessed by seIecting an integer randomly in the range 1 to 1000. The program then d i spI
6、ays the fol lowing:“I have a number between 1 and 1000. Can you guess my number? Please type your number :99The pIayer then types a guess. The program responds with one of the fol lowing after every guess:)z )z 12 3 /( z( z(xBingo! You guessed the number.Too h i gh. Try aga in.Too 1ow. Try aga in.An
7、d the program counts the number of guesses that the pIayer makes.(1) I f the number i s 5 or fewer, pr i nt “You know the secret?11(2) If the pIayer guesses the number i n 10 tr i es, then pr i nt “You got Iucky!(3) If the pIayer makes more than 10 guesses, then pr i nt “You shouId be abIe to do bet
8、ter.当玩家猜测的数字小于选择的数字时,程序会回答:“太小了,请重新猜测J当玩家猜测的数字大于选择的数字时,程序会回答:“太大了,请重新猜测J当玩家猜测的数字等于选择的数字时,程序会回答:“恭喜,你猜对了!”并显示猜测了 多少次才猜对。以下是一个示例的Python代码:pythonimport randomdef guess_number():secret number = random, randint(1, 1000)guess_count = 0 print (我有一个数字在1到1000之间。你能猜出我的电话号码吗? whileTrue:guesscount +二 1elif gues
9、s secret_number: else:guess = int (input (、请输入您的号码: if guess secret_number:print(、太小了,请重新猜测。print(太大了,请重新猜测。print (恭喜,你猜对了你猜了 guess_count, 次。breakguess_number ()这段代码使用random, randint。,1000)来在1到1000之间随机选择一个整数作为 要猜测的数字。然后,通过不断循环,玩家可以输入他们的猜测,程序会根据猜测的大小给 出相应的回答,直到猜对为止。最后会显示玩家猜了多少次才猜对。(2):K:,:;.: : * ;-,
10、:/:,.: ,;?.:士二:二:?: :,-, ,、W, :;:讪京受5,苗三;6襄十二 . J / a. .*.*. *. * : : 二讨三Q 痣沱求耍河阂:土:密.袋:W 孑淑二诵。画历证M前反装;:陵疑泰腔;然小犯施bi镖%奖6簸苑海熊.泰 ;:靖UU::球然:涣学可也:江可 工:十二:,:己;:.二,:.七:三旌5就面面0密谈逐泊定滂黎懑诙哥. * . e * * * * * * * , * * * * * 丁 . W . . .* . . * .:, 名m沙smgmySpMe:;a;为三营片x . , . , /. * . r . . ? 您您鸿叫里崂峭独亡诳密, 子访oKcac
11、V9fyenE;1 .1. . . e * * * * , * * _ , . * * * * * 4 . .,. . . . .、 . . . , / * * * * . *, , . . , * * - , .:少;. 9.V /.*. ? * , J * * 2 mySpace:a=5yourSpace:b=98. 3#include . . . . . . .,J.using namespace std;void Tcst(int&. int); , , . ; . voidmamQ. . a 4. . j. . . . . ,. ., . , ;.:., .;intx= 12;了.
12、. . . . . . . ,.;mty= 14; 一:: .:;: ;:, :.壬.: / -:Test(x,y); ?;.; ; .?,:f ./-:. . : After the first call of Te钝 the variables equal K x y chdl; . 二工20; : ,.:二.一. :.;. . J.二二 y=22;: . . . - * , * * . . , 1 .,X 2 . . * , . . . . . , , . , . . . :coutnAficr the second call of Test, the variables equal n
13、 x y endl; .a、 . . ; void Test(int& a, intb),; . :;.; 丁,; ;e., .J ,. , . . . . i. . . . . . -* .、 , ” . . .,.” ::: a=3;二二二二, : :/.:. , . . . ., . . , 1 . :a+= 2;./.:; -.a*5; :二.二二.二; : : I.:, .::.:;,boutIn fimetion Test, tfie variable equal W 在 v b W endl;略。4. Wr i te a funct i on that swaps two i n
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ch02 C+面向对象程序设计双语教程第3版-参考答案 【ch02】Basic Failities Shifting from to C+ Programs
链接地址:https://www.taowenge.com/p-97893056.html
限制150内