2015年电大面向对象程序设计作业答案参考资料小抄_第1页
2015年电大面向对象程序设计作业答案参考资料小抄_第2页
2015年电大面向对象程序设计作业答案参考资料小抄_第3页
2015年电大面向对象程序设计作业答案参考资料小抄_第4页
2015年电大面向对象程序设计作业答案参考资料小抄_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、编程题。根据程序要求,写出函数的完整定义。 ( 40 分) 1写一个函数,找出给定字符串中大写字母字符(即 A - Z这 26 个字母)的个数(如字符串” China Computer Wrold”中大写字母字符的个数为 3 个)。 函数的原型: int CalcCapital (char *str); 函数参数: str 为所要处理的字符串; 函数返回值:所给字符串中数字字符的个数 答: int CalcCapital (char *str) if (str = NULL) return 0; /判断字符指针是否为空 int num_of_Capital = 0; /记录大写字母字符个数的变量,初值为 0 for(int i=0; stri != 0x0; i+) if (stri = Z) num_of_ Capital +; /若是大写字母,则总数加 1 return num_of_ Capital; /返回大写字母字符数 2写一个函数,用递归函数完成以下运算 : sum(n) = 1 1/2 + 1/3 1/4 + -(1/n)*(-1)n (其中 n0) 函数原型: float sum(int n); 函数参数: n 为正整数。 函数返回值:相应于给定的 n,右边表达式运算结果。 提示:你可以使用递归表达式: sum(n) = sum(n-1) -(1/n)*(-1)n 答: float sum(int n) if (n = 1) return 1; else return sum(n-1) -(1.0/n)*(-1)n; 3. 给定新数值,在一个按节点所存放数值从大到小排序的链表中,找适当位置插一个新节点,仍保持有序的链表,写一个函数,完成此操作。 函数的原型: Node * InsNode(Node * head, int newValue); 其中,链表节点的定义如下: struct Nodee int Value; /存放数值 Node * next; /指向链表中的下一个节点 ; 函数参数:函数的第一个参数 head 指向链表头一节点的指针,如果链表为 空,则 head 的值为 NULL。第二个参数newValue 为所给定的插入新节点的新数值。 函数返回值:当成功地插入新的节点时,函数返回指向新链表头一节点的指针,否则,若不能申请到内存空间,则返回 NULL。 答: Node * insNode(Node * head, int newValue) Node * newNode = new Node; /申请新的节点空间 if (newNode = NULL) return NULL;/ newNode-data = newValue; /填充新节点的内容 newNode-next = NULL; Node *pre, *cur; Pre = head; if (head = NULL) head = newNode; /插入到空链表的表头 else if(newValue=head-Value) newNode-next=head; head = newNode; /插入到链表的表头 else /在链 表寻找插入点 Node *cur,*pre = head; while(pre-next != NULL) cur = pre-next; if(newValue = cur-Value) break; else pre = cur; if(pre-next!= NULL) newNode-next = cur;/若非末尾,则有下一节点 pre-next = newNode; /将新节点插入 return head; 4写一个函数,找出给定数组中具有最小值的元素。 函数的原型: char MinCode(char charAry); 函数参数: charAry 所要处理的字符数组名; 函数返回值:返回具有最小 ASCII 码的字符。 答: char MinCode(char charAry,int len=10) char mixCode = 0x0; for(int i=0; i len; i+) if (charAry i mixCode) mixCode = stri; return mixCode; 二、理解问答题: ( 20 分) 下面的文件 stack.h 是一个堆栈类模板 Stack 的完整实现。在这个文件中,首先定义了一个堆栈元素类模板 StackItem,然后,在这个类的基础上定义了堆栈类模板 Stack。在 Stack 中使用链表存放堆栈的各个元素, top 指针指向链表的第一个节点元素, bottom 指针指向链表的最后一个节点元素,成员函数 push()将一个新节点元素加入(压进)到堆栈顶部, pop()从堆栈顶部删除(弹出)一个节点元素。为方便起见,程序中加上了行号。阅读程序,根据程序后面的问题作出相应解答。 1. /*-*/ 2. /* 文件 stack.h */ 3. /*-*/ 4. template 5. class Stack; 6. /* 定义模板类 StackItem */ 7. template 8. class StackItem 9. 10. public: 11. StackItem(const Type & elem):item(elem) 12. StackItem() 13. private: 14. Type item; 15. StackItem * nextItem; 16. friend class Stack; 17. ; 18. /* 定义模板类 Stack */ 19. template 20. class Stack 21. 22. public: 23. Stack():top( NULL), _(A)_ 24. Stack(); 25. Type pop(); 26. void push(const Type &); 27. bool is_empty() const return _(B) _ ; 28. private: 29. StackItem * top; 30. StackItem * bottom; 31. ; 32. /模板类 Stack 的函数成员 pop()的实现。 33. /从堆栈顶弹出一个节点,并返回该节点的值 34. template 35. Type Stack:pop() 36. 37. StackItem *ptop; /指向顶部节点的临时指针 38. Type retVal; /返回值 39. _(C) _; 40. retVal = top-item; 41. top = top-nextItem; 42. delete ptop; 43. return retVal; 44. 45. /模板类 Stack 的函数成员 push()的实现 46. template 47. void Stack:push(const Type & newItem) 48. 49. StackItem *pNew = new StackItem( newItem); 50. _(D)_; 51. if (bottom = NULL) bottom = top = pNew; 52. else _(E)_; 53. 54. /模板类 Stack 的析构函数 Stack()的实现 55. template 56. Stack:Stack() 57. 58. StackItem *p = top, *q; 59. while(p != NULL) 60. q = p-nextItem; 61. delete p; 62. p = q; 63. 64. 问题 1: 程序中有几处填空,将它们完成。 ( A) bottom (NULL) ( B) top = NULL; ( C) ptop = top; ( D) pNew-nextItem = top; ( E) top = pNew; 问题 2:程序第 4, 5 行有什么作用?如果没有这两行语句,程序还正确吗? 答: 不正确。因为类 StackItem 模板类的定义中用到了模板类 Stack, Stack 还没有定义,所以,必须先声明 Stack 是一个模板类,否则 ,编译程序就不知道标识符 Stack 代表什么样的含义,无法进行编译。 问题 3:程序中多处出现 const,请分别说明它们各自表示什么含义。 答:第 11、 26 和 47 行的 const 修饰的都是函数的参数,表示在这个函数体中不能改它所修饰的参数的值。第 27 行的 const修饰的是模板类 Stack 的成员函数 is_empty(),它表示在函数 is_empty()的函数体中不能改变任何数据成员的值。 问题 4:程序中模板类 Stack 的析构函数主要做了什么事情?为什么要这么做? 答: 析构函数中主要是释放存放的各个节点所 占涌空间。因为 Stack 对象在其生存期间可能加入了很多节点,从堆中申请了一些内存空间。这些空间应随着对象的消亡而释放掉,所以,需要在析构函数中释放这些空间。 问题 5:下面的程序使用了 stack.h 文件中定义的类模板,请说明下列程序中定义堆栈对象的语句 (1-5)是否正确。 #include “stack.h” void main() Stack q1; / 1 Stack q2; / 2 Stack q3(10); / 3 Stack q410; / 4 Stack *q5 = new Stack; / 5 /. delete q5; 答: 语句号 1 2 3 4 5 对 /错 错 对 错 错 对 三、 填空题 ( 20 分 ) 下面是一个求数组元素之和的程序。主程序中定义了并初始化了一个数组,然后计算该数组各元素的和,并输出结果。函数 sum 计算数组元素之和。填充程序中不完整的部分。 _a_. int sum(int ,int); void main() int ia5 = 2,3,6,8,10; b ; sumofarray = sum(ia,5); cout sum of array: sumofarray endl; int sum(int array,int len) int isum = 0; for(int i = 0; c ; d ) e ; return isum; A:#include B: int sumofarray=0; C:ilen D:i+ E:iSum+=arrayi 四、 综合编程题 ( 20 分) 定义一个日期类 date,该类对象存放一个日期,可以提供的操作有: int getyear ();/取年份 int getmonth ();/取月份 int getday ();/取日子值 void setdate (int year, int month, int day);/设置日期值 下面是测试你所定义的日期类程序: #include #include “date.h” void main() date d1(1999, 1, 14);/用所给日期定一个日期变量 date d2; /定义一个具有缺省值为 1980 年 1 月 1 日的日期 , date d3(d1);/用已有日期 x 构造一个新对象 d2.setdate(1999,3,13); cout date:; cout d1.getyear() . d1.getmonth() . d1.getday() endl; cout date:; cout d2.getyear() . d2.getmonth() . d2.getday() endl; cout date:; cout d3.getyear() . d3.getmonth() . d3.getday() endl; 写出日期类的完整定义,其 ,三个 get 函数写成内联函数形式 ,setdate 写成非内联函数形式。所有数据成员都定义为私有成员。注意构造函数的三种形式。 写出程序的运行结果。 修改程序,在日期类中定义日期的输出函数,这样,主程序就可以简化。输出格式和上面一样。只需要写出类date 的修改部分。 简化后主程序如下: void main() date d1(1999, 1, 14); date d2; date d3(d1); d2.setdate(1999,3,13); d1.print(); d2.print(); d3.print(); 答: class Date private:int year,month,day; public:Data(int yr,int mth,int dy):year(yr),month(mth),day(dy); Date():year(1980),month(1),day(1); Date(date&d1):year(d1.year),month(d1.month),day(d1.day); int GetYear()return year; int GetMonth()return month; int GetDay()return day; void SetDate(int yr,int mth,int dy); ; void Date:SetDate(int yr,int mth,int dy) year=yr; month=mth; day = dy; 程序运行结果 Date:1999.1.14 Date:1999.3.13 Date:1999.1.14 修改部分: class Date public:void Print(); void Date:Print()cout”Date”;coutyear.month.dayendl; 面向对象程序设计 大作业 班级 : 学号: 姓名: 请您删除一下内容, O( _ )O 谢谢! 2015 年中央电大期末复习考试小抄大全,电大期末考试必备小抄,电大考试必过小抄 After earning his spurs in the kitchens of The Westin, The Sheraton, Sens on the Bund, and a sprinkling of other top-notch venues, Simpson Lu fi nally got the chance to become his own boss in November 2010. Sort of. The Shanghai-born chef might not actually own California Pizza Kitchen (CPK) but he is in sole charge of both kitchen and frontof- house at this Sinan Mansionsstalwart. Its certainly a responsibility to be the head chef, and then to have to manage the rest of the restaurant as well, the 31-year-old tells Enjoy Shanghai. In hotels, for example, these jobs are strictly demarcated, so its a great opportunity to learn how a business operates across the board. It was a task that management back in sunny California evidently felt he was ready for, and a vote of confi dence from a company that, to date, has opened 250 outlets in 11 countries. And for added pressure, the Shanghai branch was also CPKs China debut. For sure it was a big step, and unlike all their other Asia operations that are franchises, they decided to manage it directly to begin with, says Simpson. Two years ago a private franchisee took over the lease, but the links to CPK headquarters are still strong, with a mainland-based brand ambassador on hand to ensure the business adheres to its ethos of creating innovative, hearth-baked pizzas, a slice of PR blurb that Simpson insists lives up to the hype. They are very innovative, he says. The problem with most fast food places is that they use the same sauce on every pizza and just change the toppings. Every one of our 16 pizza sauces is a unique recipe that has been formulated to complement the toppings perfectly. The largely local customer base evidently agrees and on Saturday and Sunday, at least, the place is teeming. The kids-eat-for-free policy at weekends is undoubtedly a big draw, as well as is the spacious second-fl oor layout overlooked by a canopy of green from Fuxing Park over the road. The company is also focusing on increasing brand recognition and in recent years has taken part in outside events such as the regular California Week. Still, the sta are honest enough to admit that business could be better; as good, in fact, as in CPKs second outlet in the popular Kerry Parkside shopping mall in Pudong. Sinan Mansions has really struggled to get the number of visitors that were envisaged when it first opened, and it hasnt been easy for any of the tenants here, adds Simpson. Were planning a third outlet in the city in 2015, and we will probably choose a shopping mall again because of the better foot traffic. The tearooms once frequented by Coco Chanel and Marcel Proust are upping sticks and coming to Shanghai, Xu Junqian visits the Parisian outpost with sweet treats. One thing the century-old Parisian tearoom Angelina has shown is that legendary fashion designer Coco Chanel not only had style and glamor but also boasted great taste in food, pastries in particular. One of the most popular tearooms in Paris, Angelina is famous for having once been frequented by celebrities such as Chanel and writer Marcel Proust. Now Angelina has packed up its French ambience, efficient service, and beautiful, comforting desserts and flown them to Shanghai. At the flagship dine-in and take-out space in Shanghai, everything mimics the original tearoom designed from the beginning of the 20th century, in Paris, the height of Belle Epoque. The paintings on the wall, for example, are exactly the same as the one that depicts the landscape of southern France, the hometown of the owner; and the small tables are intentional imitations of the ones that Coco Chanel once sat at every afternoon for hot chocolate. The famous hot chocolate, known as LAfricain, is a luxurious mixture of four types of cocoa beans imported from Africa, blended in Paris and then shipped to Shanghai. Its sinfully sweet, rich and thick as if putting a bar of melting chocolate directly on the tongue and the fresh whipped cream on the side makes a light, but equally gratifying contrast. It is also sold in glass bottles as takeaway. The signature Mont-Blanc chestnut cake consists of three parts: the pureed chestnut on top, the vanilla cream like stuffing, and the meringue as base. Get all three layers in one scoop, not only for the different textures but also various flavors of sweetness. The dessert has maintained its popularity for a century, even in a country like France, perhaps the worlds most competitive place for desserts. A much overlooked pairing, is the Paris-New York choux pastry and N226 chocolate flavored tea. The choux pastry is a mouthful of airy pecan-flavored whipped cream, while the tea, a blend of black teas from China and Ceylon, cocoa and rose petals, offers a more subtle fragrance of flowers and chocolate. Ordering these two items, featuring a muted sweetness, makes it easier for you to fit into your little black dress. Breakfast, brunch, lunch and light supper are also served at the tearoom, a hub of many cultures and takes in a mix of different styles of French cuisines, according to the management team. The semi-cooked foie gras terrine, is seductive and deceptive. Its generously served at the size and shape of a toast, while the actual brioche toast is baked into a curved slice dipped with fig chutney. The flavor, however, is honest: strong, smooth and sublime. And you dont actually need the toast for crunchiness. This is the season for high teas, with dainty cups of fine china and little pastries that appeal to both visual and physical appetites. But there is one high tea with a difference, and Pauline D. Loh finds out just exactly why it is special. Earl Grey tea and macarons are all very well for the crucial recuperative break in-between intensive bouts of holiday season shopping. And for those who prefer savory to sweet, there is still the selection of classic Chinese snacks called dim sum to satisfy and satiate. High tea is a meal to eat with eye and mouth, an in-between indulgence that should be light enough not to spoil dinner, but sufficiently robust to take the edge off the hunger that strikes hours after lunch. The afternoon tea special at Shang-Xi at the Four Seasons Hotel Pudong has just the right elements. It is a pampering meal, with touches of luxury that make the high tea session a treat in itself. Whole baby abalones are braised and then topped on a shortcrust pastry shell, a sort of Chinese version of the Western vol-au-vent, but classier. Even classier is the dim sum staple shrimp dumpling or hargow, upgraded with the addition of slivers of midnight dark truffles. This is a master touch, and chef Simon Choi, who presides unchallenged at Shang-Xi, has scored a winner again. Sweet prawns and aromatic truffles whats not to love? His masterful craftsmanship is exhibited in yet another pastry a sweet pastry that is shaped to look like a walnut, but which you can put straight into the mouth. It crumbles immediately, and the slightly sweet, nutty morsel is so easy to eat youll probably reach straight for another. My favorite is the dessert that goes by the name yangzhi ganlu, or ambrosia from the gods. The hotel calls it c

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论