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

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/6883479.html】到电脑端继续下载(重复下载【60天内】不扣币)。

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

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(2022年面向对象程序设计C山师习题答案.doc)为本站上传会员【a199****6536】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

2022年面向对象程序设计C山师习题答案.doc

1、第六章习题答案 一、选择填空 1、A 2、C 3、D 4、B 5、D 6、A 7、C 8、A 9、D 10、A 11、C 12、A 13、B 14、C 15、C 16、D 17、B 18、C 19、A 20、D 21、C 22、B 二、判断下列描述旳对旳性,对者划√,错者划×。 1、√ 2、× 3、× 4、× 5、√ 6、√ 7、× 8、√ 9、× 10、√ 11、√ 12、√ 13、√ 14、√ 15、× 16、√ 17、√

2、 18、√ 19、√ 20、× 21、× 22、× 三、分析下列程序旳输出成果。 1、运行该程序输出成果如下所示。 Default constructor called Constructor called a=0,b=0 a=4,b=8 2、运行该程序输出成果如下所示。 a=7,b=9 3、运行该程序输出成果如下所示。 104 4、运行该程序输出成果如下所示。 1035,789.504 5、运行该程序输出成果如下所示。 1 {} {0,1,2,3,4,5,6,7,8} 1 {11,12,13,14,15,16,17,18,19} {19,1

3、8,17,16,15,14,13,12,11} 6、运行该程序输出成果如下所示。 Starting1: Default constructor called. Default constructor called. Default constructor called.Eding1: Starting2: Constructor: a=5,b=6 Constructor: a=7,b=8 Constructor: a=9,b=10 Ending2: Destructor called.a=9,b=10 Destructor called.a=7,b=8 Destruc

4、tor called.a=5,b=6 Destructor called.a=5,b=6 Destructor called.a=3,b=4 Destructor called.a=1,b=2 7、运行该程序输出成果如下所示。 Default constructor called. Default constructor called. Default constructor called. Default constructor called. Destructor called. Constructor1 called. Destructor called. Co

5、nstructor2 called. Destructor called. x=0,y=0 x=5,y=0 x=2,y=3 Destructor called. Destructor called. Destructor called. 8、运行该程序输出成果如下所示。 Constructor called.0 Constructor called.5 Destructor called.5 5 Destructor called.5 9、运行该程序输出成果如下所示。 Constructor called.5 5 Destructor called.5 10

6、运行该程序输出成果如下所示。 Default Constructor called. Constructor:real=6.8,imag=0 Constructor:real=5.6,imag=7.9 0+0I 6.8+0I 5.6+7.9I Constructor:real=1.2,imag=3.4 Constructor:real=5,imag=0 Default Constructor called. 1.2+3.4I 5+0I 0+0I 11、答: ⑴该程序中用string.h中所包括旳函数有3种,它们是strcpy( )、strcat( )和strlen

7、 ). ⑵该程序中使用了函数重载。它们是下述两个构造函数重载:String( )和String(const char * str)。 ⑶类中组员函数Setc( )旳功能是用来设置字符数组中某位置旳一种指定字符。 ⑷类中组员函数Getc( )旳功能是用来从某个字符数组中获取指定位置旳字符。 ⑸类中组员函数Append( )旳功能是在一种字符数组中追加一种指定旳字符串,即将指定旳字符串添加到已知串旳后边。 ⑹不行。 ⑺该程序中有两处使用了new运算符。 ⑻运行该程序输出成果如下所示。 empty. a string. 9 a string. i this a strin

8、g. 四、改正如下程序中旳错误。 1、该程序中point类旳构造函数定义不对旳,在main()中队数据组员旳访问不对旳,修改如下: #include class point { int x1,x2; public: point(int x,int y){point::x=x;point::y=y;} void disp() { cout<

9、main()函数中旳p.x+=5和p.y+=6两个语句是错误旳,由于保护数据仅能被类旳组员函数访问。 五、按下列规定编写程序。 1、程序内容如下所示。 #include class Test { public: Test(){} Test(int i,int j=20) { t1=i; t2=j; t+=j-i; } static int fun(Test&T); friend int add(Test&T); private: int t1,t2; static int t; }; int Test::fun(Test&T)

10、 { t+=T.t; return t; } int add(Test&T) { int n=T.t1+T.t2; return n; } int Test::t=5; void main() { Test a1,a2(10),a3(15,25); cout< #include class Product { char *name; int price;

11、int quantity; public: Product(char *n,int p,int q) { name =new char[strlen(n)+1]; strcpy(name,n); price=p; quantity=q; } ~Product() { if(name) { delete [] name; name=0; } } void buy(int money) { int n,r; n=money/price; if(n>quantity) cout<<"数量不够"<

12、 else { quantity-=n; r=money%price; cout<<"产品:"<

13、 p1.get(); p1.buy(4500); p1.get(); } 3、程序内容如下所示。 #include #include class CDate { private: int m_nDay; int m_nMonth; int m_nYear; public: CDate(); CDate(int day,int month,int year); void Display(); void AddDay();

14、 void SetDate(int day,int month,int year); ~CDate(); private: bool IsLeapYear(); //判断该年与否为闰年 }; CDate::CDate() { } CDate::CDate(int day,int month,int year) { m_nDay=day; m_nMonth=month; m_nYear=year; } void CDate::Display() { char day[5]; char month[5];

15、 char year[5]; _itoa(m_nDay,day,10); _itoa(m_nMonth,month,10); _itoa(m_nYear,year,10); cout<

16、 { if((m_nMonth==2)&&(m_nDay==29)) { m_nMonth++; m_nDay=1; return; } } if(m_nDay>31) { if(m_nMonth==12) { m_nYear++; m_nMonth=1; m_nDay=1; } else { m_nMonth++; m_nDay=1; } } } void CDate::SetDate(int day,int month,int year) { m_nDay=day;

17、 m_nMonth=month; m_nYear=year; } CDate::~CDate() { } bool CDate::IsLeapYear() { bool bLeap; if (m_nYear%4!=0) bLeap=false; else if(m_nYear%100!=0) bLeap=true; else if(m_nYear%400!=0) bLeap=false; else bLeap=true; return bLeap; } void main() { CDate d; d.S

18、etDate(,2,28); cout<<"目前日期=>:"; d.Display(); d.AddDay(); cout<<"目前日期加1=>:"; d.Display(); } 4、程序内容如下所示。 #include class Tc { private: double unlead,lead,total; int unprice,price; public: Tc(){unprice=17;price=16;} void getdata() { cout<<"无铅汽油总量;"; cin>

19、>unlead; cout<<"有铅汽油总量;"; cin>>lead; total=unprice*unlead+price*lead; } void disp() { cout<<"总收入:"< class CFactorial { int value; int fact; public: CFactorial(int v

20、al); void CalculateFactorial(); void Display(); }; CFactorial::CFactorial(int val) { value=val; fact=1; } void CFactorial::CalculateFactorial() { int i=value; while(i>1) fact*=i--; } void CFactorial::Display() { cout<

21、torial A(5); A.CalculateFactorial(); A.Display(); } 6、程序内容如下所示。 #include #include class rectangle { private: float ledge,sedge; public: rectangle(){}; rectangle(float a,float b) { ledge=a; sedge=b; }; float area() { return ledge*sedge; }; vo

22、id addarea(rectangle r1,rectangle r2) { cout<<"总面积:"< #include class rectangle { private: float ledge,sedge; pu

23、blic: rectangle(){}; rectangle(float a,float b) { ledge=a; sedge=b; }; float area() { return ledge*sedge; }; void showlength() { cout<<"周长:"<<(ledge+sedge)*2<

24、 return temp; } }; void main() { rectangle A(3.5,2.5),B(4.2,3.8); cout<<"A"; A.showlength(); cout<<"B"; B.showlength(); rectangle C=A.tlength(B); cout<<"C"; C.showlength(); } 8、程序内容如下所示。 #include #include class Line { private: double

25、 x1,x2,y1,y2; public: Line(){}; Line(double a,double b,double c,double d) { x1=a;y1=b;x2=c;y2=d; cout<<"线段端点"<

26、l1.y2-l1.y1)*(l2.x2-l2.x1)-(l1.x2-l1.x1)*(l2.y2-l2.y1); double r=r1/r2; double t=((l1.x1-l2.x1)+r*(l1.x2-l1.x1))/(l2.x2-l2.x1); if (r>0&&r<1&&t>0&&t<1) cout<<""<0&&r<1&&t>=1) cout<<""<=1 || r<=0)&& t>0&&t<1) cout<<""<

27、 cout<<""< class student { int no;

28、 char name[10]; class cdegree { public: int math; int english; int phy; }degree; public: void getdata() { cout<<"学号:"; cin>>no; cout<<"姓名:"; cin>>name; cout<<"数学分数:"; cin>>degree.math; cout<<"英语分数:"; cin>>degree.english; cout<<"

29、物理分数:"; cin>>degree.phy; } void disp() { cout<<"学号:"<

30、0、程序内容如下所示。 #include class Student { int english,computer,total; public: void getscore(); void display(); void sort(Student *); ~Student(); }; void Student::getscore() { cout<<"输入英语成绩"; cin>>english; cout<<"输入计算机成绩"; cin>>computer; total=english+computer; }

31、 void Student::sort(Student *p) { int tmp,i,j; for(j=0;j<2;j++) for(i=0;i<2;i++) if (totaltotal) { tmp=total; total=p->total; p->total=tmp; tmp=english; english=p->english; p->english=tmp; tmp=computer; computer=p->computer; p->computer=tmp; } } void

32、Student::display() { cout<<"英语="<getscore(); } int i; for (j=0;j<2;j++) for(i=0;i<2;i++) A[i]->sort(A[i+1]); cout<

33、display(); } 11、程序内容如下所示。 #include struct list //定义栈 { int data; list *next; }; class Stack //定义一种栈操作类 { list *ptr; public: Stack(){ptr=NULL;} void push(int i); int pop(); }; void Stack::push(int x) //入栈组员函数

34、 { list *newnode=new list; newnode->data=x; newnode->next=ptr; ptr=newnode; } int Stack::pop() //出栈组员函数 { list *top; int value; value=ptr->data; top=ptr; ptr=ptr->next; delete top; return value; } void main() { Stack A; int arr[]={5,2,8,1,4,3,9,7,6};

35、 cout<<"入栈次序:"; for (int i=0;i<9;i++) { cout< struct list { int data; list *next; }; class Queue { list *ptrf,*ptrb; //队首和队

36、尾指针 public: Queue() { ptrf=ptrb=NULL; } void enqueue(int); int dequeue(); }; void Queue::enqueue(int x) //入队组员函数 { list *newnode=new list; newnode->data=x; newnode->next=NULL; if (ptrb==NULL) ptrf=ptrb=newnode; else { ptrb->next=newnode; ptrb=newnode;

37、 } }; int Queue::dequeue() //出队组员函数 { list *tmp; int value; value=ptrf->data; tmp=ptrf; ptrf=ptrf->next; delete tmp; return value; } void main() { Queue A; int arr[]={3,12,8,9,11}; cout<<"入队次序;"; for (int i=0;i<5;i++) { cout<

38、]); } cout< #include typedef struct tree { int data; tree *left,*right,*father; }*bstree; class Btree { static int n; static int m; public:

39、 tree *root; Btree() { root=NULL; } void create_Btree(int); void inorder(bstree); //中序遍历 void display() {cout<

40、 //输出要查找旳值对应旳双亲结点旳内容 }; int Btree::n=0; int Btree::m=0; int Btree::print(bstree p,int k) { if(p==NULL)return 0; else { if(p->data!=k) { print(p->left,k); print(p->right,k); } else { cout<

41、father->data<data=x; newnode->right=newnode->left=NULL; if(root==NULL) root=newnode; else { bstree back;

42、bstree current=root; while(current!=NULL) { back=current; if(current->data>x) current=current->left; else current=current->right; } if(back->data>x) {back->left=newnode;newnode->fath

43、er=back;} else {back->right=newnode;newnode->father=back;} } } int Btree::count(bstree p) { if(p==NULL) return 0; else return count(p->left)+count(p->right)+1; //这是运用了函数嵌套即递归旳措施。 } void Btree::inorder(bstree temp) //这是中序遍历二叉树,采用了递归旳

44、措施。 { if(temp!=NULL) { inorder(temp->left); cout<data<<" "; inorder(temp->right); } } void main() { Btree A; int array[]={7,4,1,5,12,8,13,11}; int k; k=sizeof(array)/sizeof(array[0]); cout<<"建立排序二叉树次序: "<

45、r(int i=0;i #include struct list { int data; list *next; }; cla

46、ss Stack { list *ptr; public: Stack(){ptr=NULL;} void push(int i); int pop(); int empty() { if (ptr==NULL) return 1; else return 0; } }; void Stack::push(int x) { list *newnode=new list; newnode->data=x; newnode->next=ptr; ptr=newnode; } int Stack::pop() { list

47、top; int value; value=ptr->data; top=ptr; ptr=ptr->next; delete top; return value; } class Queue { list *ptrf,*ptrb; public: Queue() { ptrf=ptrb=NULL; } void enqueue(int); int dequeue(); }; void Queue::enqueue(int x) { list *newnode=new list; newno

48、de->data=x; newnode->next=NULL; if (ptrb==NULL) ptrf=ptrb=newnode; else { ptrb->next=newnode; ptrb=newnode; } }; int Queue::dequeue() { list *tmp; int value; value=ptrf->data; tmp=ptrf; ptrf=ptrf->next; delete tmp; return value; } void main() { Stack S

49、 Queue Q; char ch; cout<<" 输入数据:"; while((ch=getchar())!='.') { S.push(ch); Q.enqueue(ch); } while(!S.empty() && S.pop()==Q.dequeue()); if (S.empty()) cout<<"输入旳是回文数据。"< #include

50、 class magic { int m[4][4]; int step; int first; int sum; public: void getdata(); void setfirstmagic(); void generatemagic(); void printmagic(); }; void magic::getdata() { cout<<"输入魔方起始值:"; cin>>first; cout<<" 输入相邻元素差值:"; cin>>step; } void magic::setfirst

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服