C++面向对象程序设计双语教程(第3版)习题及答案汇总刘嘉敏 第1--8章Introduction --- Templates.docx

上传人:太** 文档编号:97922758 上传时间:2024-07-08 格式:DOCX 页数:25 大小:64.02KB
返回 下载 相关 举报
C++面向对象程序设计双语教程(第3版)习题及答案汇总刘嘉敏 第1--8章Introduction --- Templates.docx_第1页
第1页 / 共25页
C++面向对象程序设计双语教程(第3版)习题及答案汇总刘嘉敏 第1--8章Introduction --- Templates.docx_第2页
第2页 / 共25页
点击查看更多>>
资源描述

《C++面向对象程序设计双语教程(第3版)习题及答案汇总刘嘉敏 第1--8章Introduction --- Templates.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计双语教程(第3版)习题及答案汇总刘嘉敏 第1--8章Introduction --- Templates.docx(25页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Chapter 1 Introduct i on1. What i s planing the performance of a task or an event?一个任务或事件的执行计划是一份详细的文件,描述了完成该任务或事件所需的步骤、 资源和时间安排。它通常包括以下要素:1 .目标和目的:明确任务或事件的最终目标和目的。2 .任务和子任务一个任务或事件的执行计划是指一系列有条理的步骤,旨在达到特定目标 或完成特定任务。具体的执行计划可以因任务性质、复杂度和要求的不同而有所不同,但通 常包括以下几个关键要素:1 .目标设定:明确任务或事件的最终目标,并确保目标具体、可行和可衡量。2 .分析

2、与策划:评估任务的各种要求、限制和可能的风险,并制定相应的策略和计划来实 现目标。3 .:列出任务和各个子任务的详细描述和要求。每个任务应具有明确的起止时间和负责人。4 .资源分配:确定完成任务或事件所需的各种资源,如人力、物资、设备和技术等。5 .时间安排:制定任务子任务的时间表,包括开始时间、截止时间和关键里程碑。6 .风险评估:识别可能出现的风险和问题,并制定相应的风险应对措施和备选方案。7 .沟通和协调:明确团队成员之间的沟通和定时间表:确定任务执行的时间范围,包括起 始日期、终止日期和关键里程碑。8 .资源分配:确定需要的各种资源,如人力、物力、财力和技术,以及如何合理地配置和 利用

3、这些资源。9 .组织与协调方式,以确保信息流畅和工作高效。7.监控和评估:建立监控机制,以便及时跟踪任务的执行进度,并对完成情况进行评估和 反馈。通过制定任务或事件的执行计划,可以提高工作效率、减少风险,并确保任务或事件按时、 按质完成。调:确保任务执行过程中的各项工作有条不紊地进行,包括分工、协作和沟通等。 6.监测与评估:持续跟踪任务的进展情况,及时发现和解决问题,并根据情况进行调整和 改进。7.风险管理:识别潜在的风险和挑战,并制定相应的措施来降低风险,并确保任务顺利进 行。8.评价与总结:在任务或事件完成后,进行全面评估和总结,发掘经验教训,并为未来的 类似任务提供参考。2. What

4、 i s a step-by-step procedure for solving a prob Iem i n a finite amount of time?在有限的时间内逐步解决一个问题的步骤可以根据具体情况而定。以下是一般的解决问 题的步骤:1 .确定问题的范围和定义:明确问题是什么、为什么它是个问题,以及解决问题的目标是 什么。2 .收集必要的信息:收集与问题相关的信息和数据,这可以包括已知条件、约束和其他关 键信息。3 .分析问题:仔细分析问题的不同方面,确定可能的因素和因果关系。使用逻辑推理和相 关的工具或模型来深入理解问题。return None#初始化最大值和最小值为数组的第

5、一个元素 max_val = arr0min val = arr0#遍历数组,更新最大值和最小值for num in arr:if num maxval:max_val = numif num = 0:sum_positive +二 num else:sum_negative += numreturn sumpositive, sum_negative#测试函数numbers =1, -2, 3, -4, 5, 6, -7, 8, 9, -10 positivesum, negativesum = sum of numbers(numbers) print (、数字之和 0=0) : posi

6、tive_sum)print(、数字之和(= 0:sum_positive += numelse:sum_negative += num return sum_positive, sum_negative#测试函数numbers = -4, 6, 2, -8, 10, -3, 5, 7, -1, 9positive_sum, negative_sum = sum_of_numbers(numbers)print (、大于等于0的数字之和: positive_sum)print (、小于 0 的数字之和: negative_sum) 、运行结果:大于等于0的数字之和:39 小于0的数字之和:-1

7、6这个函数会遍历输入的10个整数,将于等于0的数字累加到、suni_positive变量中, 将小于0的数字累加到sum_negative变量中。最后,函数返回这两个和。11. Write a funct i on to ca I cu I ate the calorie of I unch. The user i nputs three I i nes. Each I i ne has a character (M i s for meat, V i s for vegetabIe, D i s for dessert) and a calorie vaIuc. Theprogram out

8、puts the totaI calorie vaIue of Iunch.pythondef calculate_calories():total_calories = 0for _ in range(3):line = input (请输入字符和卡路里值(M/V/D卡路里):line =line, split ()character 二 line0calories = int(linel)total calories += caloriesprint (、午餐的总卡路里值: total_calories)calculate_calories () 、请注意,这个例子假设用户将会依次输入三行

9、信息,并且每一行都包含一个字符和一个 卡路里值,中间用空格隔开。函数会依次读取每一行的信息,并将卡路里值累加到总卡路里 值中,最后输出总卡路里值。12. According to the input string, count the number of digit and a Iphabet and output them.pythondef count_digits_and_letters(input_str):digitcount = 0letter_count = 0 for char in input_str: if char, isdigit ():digit_count 十二 1

10、elif char. isalphaO :letter_count +二 1return digit_count, letter_count#测试代码input_string = Hello World 123digit_count, letter_count count digitsand letters(inputstring)print (、数字的数量: digit_count)print (、字母的数量: letter_count) 、 、这段代码会输出以下结果:数字的数量:3 字母的数量:10 、请注意,这个代码假设输入字符串只包含数字和字母,并且只计算ASCH字符。如果输 入字符串

11、中包含其他字符,例如标点符号、空格或特殊字符,它们将被忽略。13. Write a funct i on to so Ive roots of ax2 +bx+c=0 equat i on. Hint: for d i fferent a, b and c, the equat i on may have 0, 1 or 2 roots. You may def i ne a funct i on as foI Iows:i nt so Iv i ng( doubIe a, doubIe b, doubIe c, doubIc& x1, doubIe& x2) where parameters

12、 x1 and x2 are the roots. The return i ng vaIue of the funct i on i s the number of roots.以下是一个求解一元二次方程的根的函数:python import mathdef solve quadratic equation (a, b, c):#计算判别式 discriminant = b*2 - 4*a*c#判断根的数量 if discriminant 0:#有两个实根xl =(-b + math, sqrt(discriminant) / (2*a)x2 = (-b - math, sqrt(discr

13、iminant) / (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:print (、根 1: xl)if root count 1:print (、根 2: x2)以上函数使用判别式确定

14、根的情况,返回根的数量以及根的数值(如果有的话)。若根 数大于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 ays the fol lowing:“I have a number between 1 and 1000. Can

15、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.And the program counts the number of guesses that the pIayer m

16、akes.(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 print “You got Iucky!(3) If the pIayer makes more than 10 guesses, then pr i nt “You shouId be abIe to do better.当玩家猜测的数字小于选择的数字时,程序会回答:“太小了,请重新猜测。”当玩家猜测的数字大于选择的数字时,程序会回答:

17、“太大了,请重新猜测。”当玩家猜测的数字等于选择的数字时,程序会回答:“恭喜,你猜对了! ”并显示猜测 了多少次才猜对。以下是一个示例的Python代码:pythonimport randomdef guess_number():secret number = random, randint(1, 1000)guess_count = 0 print (我有一个数字在1到1000之间。你能猜出我的电话号码吗? whileTrue:guesscount +二 1elif guess secret_number: else:guess = int (input (、请输入您的号码:if guess

18、 are used to access cI ass members.(7) Header files prov i de the comp Iete def i n i t i on of a cI ass * s impIementat i on deta i Is.(8) Each cI ass def i n i t i on i s norma I Iy pIaced i n a header fIe.(9) The default access mode for members of a cI ass i s pr i vate.(10) The IabeIs pub I i c

19、or pr i vate can be repeated i n a cI ass def i n i t i on.(11) Member funct i ons I abeIed pub I i c can on Iy be accessed from outs i de the cI ass.(12) Access to members of a struct can be def i ned as pub I i c or pr i vate.(13) Li ke any funct ion, constructors can return a vaIue.(14) Construct

20、ors cannot be over Ioaded.(15) A constructor i s a member fumct ion with the same names as the c I ass.(16) A cI ass has on Iy one constructor.(17) There can on Iy be one default constructor per cI ass.(18) The constructor can conta i n defau11 arguments i n the parameter I i st.(19) A destructor i

21、s cal led when an object i s removed ftom memory.(20) Like a constructor, a destructor can take arguments.(21) A destructor may return a vaIue.(22) The destructor can be over Ioaded.(23) Constructors and destructors are cal led automat i caI Iy.(24) The programmer must always provide a constructor a

22、nd destructor for a c I ass.(25) A set funct i on shouId perform the vaI i d i ty exami nat i on.(26) Informat i on hiding i s a process by wh i ch a cI asss data imp Iementat i on i s h i dden from the user of the cI ass.(1)假(F)o在某些情况下,对象可以通过特定的方法去了解其他对象的实现,例如通 过反射或者访问控制的方式。(2)假(F)。该类通常被称为用户定义的类型,而

23、不是程序员定义的类型。(3)真(T)。在声明类之外定义的成员函数不会拥有类作用域,因此无法直接访问类的 私有成员。(4)假(F)o与类名称相同并前面带有的成员函数被称为析构函数,而不是构函数。(5)假(F)o成员函数可以根据不同的参数类型和数量进行重载。重载允许在同一个类 中定义多个具有相同名称但不同参数的成员函数。(6)假(F)o操作符用于访问类成员,而-用于访问指向类对象的指针的成员。(7)假(F)o头文件只包含类的声明,而不是完整的实现细节。实现细节通常在源文件 中定义。(8)真(T)o每个类的定义通常都放在一个头文件中,以便其他文件可以引用该类。(9)真(T)o一个类的成员的默认访问模

24、式是私有的,这意味着它们只能在类的内部访 问。(10)假(F)o公共的或私有的标签在类定义中只能使用一次。在类定义中,可以通过 使用多个公共或私有部分来分组相关的成员,但这些部分不能重复出现。(11)假(F)。标记为“公共”的成员函数可以从类的外部和内部访问。(12)真(T)。对结构体成员的访问可以定义为公共的(默认)或私有的,取决于所 定义的访问修饰符。(13)假(F)。构造函数没有返回值,它们的主要目的是初始化对象的实例。(14)假(F)。构造函数可以被重载,允许多个构造函数以不同的参数进行定义。(15)真(T)。构造函数与类具有相同的名称,用于创建和初始化类的对象。(16)假(F): 一

25、个类可以有多个构造函数。一个类可以根据不同的参数组合定义多个 构造函数。(17)假(F):每个类可以有多个构造函数,没有限制只有一个默认构造函数。(18)真(T):构造函数可以在参数列表包含默认参数,这样在调用构造函数时,如果 没有提供相应的参数,将使用默认值。(19)真(T):析构函数在对象被删除从内存中释放时被调用,用于清理对象所占用的 资源。(20)真(T):与构造函数一样,析构函数也可以接受参数。这是为了在析构对象时执 行特定的操作,或者根据不同情况处理不同的清理任务。(21)假(F)。析构函数没有返回类型,不能返回值。(22)假(F)。析构函数不可以重载,因为它只有一个在对象销毁时被

26、自动调用的版本。(23)真(T)。构造函数在创建对象时被自动调用,析构函数在对象销毁时被自动调用。(24)假(F)。在某些情况下,一个类可以没有显式定义的构造函数和析构函数,编译 器会提供默认的构造函数和析构函数。(25)真(T)。集合函数应进行有效性检查以确保输入的数据是合法和有效的,以避免 潜在的错误和异常。(26)假(F)。信息隐藏是指在数据中隐藏一些额外信息或元数据的过程,而不是隐 藏整个类的数据。例如,在数字图像中隐藏水印或嵌入隐藏消息。(2) F i nd the syntax errors i n the definitions of the fol lowing cI asse

27、s.(3) class AA public: .void AA(int, int);int sum0;private:int x = 0;.:inty; : . (4) class BB public:BB(int, int); printO;.二 I:;. .private: intx,y;(5) class CC .:public:cco;条湍系发砒;游辞又涕溪舞,. . . .:三三三,;3二:;”二,::二:,:.::二::;::prfvM 臣ulwl:;谖:廉mtx,y;二;二::;.;二二二三三r .寸,: 丁二:-;.:,:;.? :二::.;.;二:.二二二.: : :;:,.

28、, : .: , . i. , , i f , e and assume that the following statements are in a user program:Tointpomtl;二0证口0检。0;20); :,【;17 T- z(How many members does cI ass Po i nt have?7 72 3 4 z( z( z(How many pr i vate members does cI ass Po i nt have?How many constructors does cI ass Po i nt have?Wr ite the defin

29、ition of default constructor of cI ass Po i nt so that the pr i vate member var iabIes are imitial ized to be 0. Write the definition of the constructor with arguments x and y so that the pr i vate member var i abIes are initial i zed to be the vaIues of xc and yy.(6) Write the definition of member

30、function move with arguments newX and newY so that x i s reset to be newX and y i s reset to be newY.(7) Wr ite the definition of member funct i on pr i nt that outputs the vaIues of x andy.(8) Write the main funct i on to test cI ass Point.略。4. Write the def i n i t i on of a Word cI ass that imp I

31、ements the funct i ons of add i ng a mean i ng, gett i ng a mean i ng, gett i ng the number of word mean i ngs and outputt i ng a I I i nformat i on of a word.pythonclass Word:def _init_(self):self, meanings= #用字典存储各个单词的含义def addmeaning(self, word, meaning):if word in self.meanings:self, meaningswor

32、d. append(meaning)else:self, meaningsword二meaningdef getmeaning(self, word):if word in self, meanings:return self, meaningsword4 .生成解决方案:利用先前的分析结果,思考可能的解决方案。根据问题的性质,可以采 用创造性思维、团队讨论或其他方法来生成解决方案。5 .评估解决方案:评估每个解决方案的优劣和可行性。考虑其影响、风险、成本、时间以 及相关的因素。筛选出最佳的解决方案。6 .制定行动计划:为选择的解决方案制定行动计划。确定所需资源、时间表和责任分配, 并确保计划

33、的可执行性。7 .实施解决方案:按照行动计划开始执行解决方案。确保每个步骤按计划进行,并做好记 录,以便跟踪进度和发现潜在的问题。8 .监控和评估结果:在实施过程中持续监控和评估解决方案的效果。根据实际情况进行调 整,并进行必要的反馈和纠正措施。9 .总结和总结经验:在解决问题后,总结整个过程,包括解决方案的有效性和所学到的经 验教训。将这些经验教训应用到将来的类似问题中。3. What i s OOP? What i s the name of the data structure we used? What are the components of th i s data structu

34、re?OOP是面向对象编程的缩写,它是一种编程范式,使程序通过创建和管理对象来组织代 码。在面向对象编程中,程序被组织为一组相互关联的对象,每个对象都有自己的属性(数 据)和方法(功能)。数据结构的名称可以有很多种,常见的数据结构包括数组、链表、栈、队列、树、图等。不 同的数据结构适用于不同的场景和问题,每种数据结构都有自己的特点和使用方法。数据结构的组成部分取决于具体的数据结构类型。以数组为例,它由一系列元素组成,每个 元素都有一个唯一的索引来标识其在数组中的位置。数组的大小是固定的,可以存储不同类 型的数据。在编程语言中,通常通过使用内存中一块连续的区域来存储数组的元素。其他数 据结构的组

35、成部分也会有所不同,但都是为了有效地存储和操作数据。4. What are the character i st i cs of OOP?面向对象编程(Object-Oriented Programming,简称OOP)具有以下几个特点:1 .封装(Encapsulation):将数据和相关的操作封装在对象内部,隐藏了细节,只暴露 出必要的接口。通过封装,可以控制对对象的访问权限,提高数据的安全性和可靠性。2 .继承(Inheritance):通过继承可以创建一个新类,继承已有类的属性和方法。子类 可以复用父类的代码,并且可以通过重写父类的方法来实现个性化需求。继承提供了代码的 可重用性和扩展

36、性。3 .多态(Polymorphism):多态是指对象可以根据上下文的不同表现出不同的行为。通过 多态,可以编写出更灵活、可扩展的代码,提高代码的可读性。4 .抽象(Abstraction):抽象是指根据问题的需求将对象的共同特征提取出来形成类、else:return 该单词没有定义。 def get_meaning_count (self, word):if word in self, meanings:return len (self, meaningsword) else:return 0def get_all_info(self, word):if word in self, meanings: return self, meaningswordelse:return 该单词没有定义。、使用示例:python#创建一个Word对象word_obj = Word()#添加单词的含义word obj.add meaning (apple a round skinword_obj. add meaning(apple the fruit of family#获取单词的含义print(word_obj. get_meaning(apple # a skin,, the fruit of a tree that belon

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

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

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