版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第七章习题答案一、选择填空1、D2、A3、B4、C5、A6、C7、B8、D9、二、判断下列描述的对的性,对者划√,错者划×。1、√2、×3、×4、×5、√6、×7、√8、√9、√10、×11、√12、√13、×14、√15、√16、×17、√18、√三、分析下列程序的输出结果。1、(1)上述结构的DAG图如下所示。(2)无二义性(3)无二义性2、(1)无(2无(3)有(4)无(5)有四、分析下列程序的输出结果1、运营该程序输出如下结果。(1,2)5,6(6,9)2、该程序的输出结果如下所示(1,2)(6,9)5,6(6,9)3、该程序的输出结果如下:(13,22,30,40)4、运营该程序输出结果如下所示。D2::display()pri1=4,pri2=5pri4=6pri12=7D2::display()pri1=12,pri2=9pri4=7pri12=85、该程序输出结果如下所示:D2::display()pri1=1,pri2=4pri4=6pri12=7D2::display()pri1=9,pri2=8pri4=7pri12=86、该程序输出结果如下所示:baseclassbaseclassbaseclassderive1classderive2class五、按下列规定编写程序。1、程序内容如下所示。#include<iostream.h>#include<iomanip.h>classperson{intno;charname[10];public:voidinput(){cout<<"编号:";cin>>no;cout<<"姓名:";cin>>name;}voiddisp(){cout<<"编号:"<<no<<endl;cout<<"姓名:"<<name<<endl;}};classstudent:publicperson{private:chardepart[6];intdegree;public:voidinput(){person::input();cout<<"班号:";cin>>depart;cout<<"成绩:";cin>>degree;}voiddisp(){person::disp();cout<<"班号:"<<depart<<endl;cout<<"成绩:"<<degree<<endl;}};classteacher:publicperson{private:charprof[10];chardepart[10];public:voidinput(){person::input();cout<<"职称:";cin>>prof;cout<<"部门:";cin>>depart;}voiddisp(){person::disp();cout<<"职称:"<<prof<<endl;cout<<"部门:"<<depart<<endl;}};voidmain(){students1;teachert1;cout<<"输入一个学生数据:\n";s1.input();cout<<"输入一个教师数据:\n";t1.input();cout<<"显示一个学生数据:\n";s1.disp();cout<<"显示一个教师数据:\n";t1.disp();}2、程序内容如下所示。#include<iostream.h>#include<string.h>classstring{intlength;char*contents;public:intget_length(){returnlength;}char*get_contents(){returncontents;}~string(){deletecontents;}intset_contents(intin_length,char*in_contents);intset_contents(char*in_contents);voidprint(){cout<<contents<<endl;}};classedit_string:publicstring{intcursor;public:intget_cursor_pos(){returncursor;}voidmove_cursor(inthow_much){cursor=how_much;}intadd_at_cursor(string*new_text);intrepl_at_cursor(string*new_text);voiddele_at_cursor(inthow_much);};intstring::set_contents(intin_length,char*in_contents){length=in_length;if(!contents)deletecontents;contents=newchar[length+1];strcpy(contents,in_contents);returnlength;}intstring::set_contents(char*in_contents){length=strlen(in_contents);if(!contents)deletecontents;contents=newchar[length+1];strcpy(contents,in_contents);returnlength;}intedit_string::add_at_cursor(string*new_text){intn,k,m;char*cp,*pt;n=new_text->get_length();pt=new_text->get_contents();cp=this->get_contents();m=this->get_length();char*news=newchar[m+n+1];for(inti=0;i<cursor;i++)news[i]=cp[i];k=i;for(intj=0;j<n;i++,j++)news[i]=pt[j];cursor=i;for(j=k;j<m;j++,i++)news[i]=cp[j];news[i]='\0';set_contents(news);deletenews;returncursor;}intedit_string::repl_at_cursor(string*new_text){intn,m;char*pt,*news;n=new_text->get_length();pt=new_text->get_contents();m=this->get_length();news=newchar[m>n+cursor?m+1:n+cursor+1];news=this->get_contents();for(inti=cursor,j=0;i<n+cursor;j++,i++)news[i]=pt[j];if(m<n+cursor)news[i]='\0';cursor=i;set_contents(news);deletenews;returncursor;}voidedit_string::dele_at_cursor(inthow_much){intm;char*cp,*news;cp=this->get_contents();m=this->get_length();for(inti=cursor;i<m;i++)cp[i]=cp[i+how_much];cp[i]='\0';}voidmain(){strings1;edit_strings2;char*cp;s1.set_contents("Object_OrientedProgramming");cp=s1.get_contents();s2.set_contents(cp);s2.print();s2.move_cursor(15);s1.set_contents("Windwos");s2.add_at_cursor(&s1);s2.print();s2.move_cursor(6);s2.dele_at_cursor(9);s2.print();s1.set_contents("TTT");s2.repl_at_cursor(&s1);s2.print();}3、程序内容如下所示。#include<iostream.h>classvehicle{protected:intwheels;floatweight;public:vehicle(intwheels,floatweight);intget_wheels();floatget_weight();floatwheel_load();voidprint();};classcar:vehicle{intpassenger_load;public:car(intwheels,floatweight,intpassengers=4);intget_passengers();voidprint();};classtruck:vehicle{intpassenger_load;floatpayload;public:truck(intwheels,floatweight,intpassengers=2,floatmax_load=240000.00);intget_passengers();floatefficiency();voidprint();};vehicle::vehicle(intwheels,floatweight){vehicle::wheels=wheels;vehicle::weight=weight;}intvehicle::get_wheels(){returnwheels;}floatvehicle::get_weight(){returnweight/wheels;}voidvehicle::print(){cout<<"车轮:"<<wheels<<"个。"<<endl;cout<<"重量:"<<wheels<<"公斤。"<<endl;}car::car(intwheels,floatweight,intpassengers):vehicle(wheels,weight){passenger_load=passengers;}intcar::get_passengers(){returnpassenger_load;}voidcar::print(){cout<<"小车:"<<endl;vehicle::print();cout<<"载人:"<<passenger_load<<"人。"<<endl;cout<<endl;}truck::truck(intwheels,floatweight,intpassengers,floatmax_load):vehicle(wheels,weight){passenger_load=passengers;payload=max_load;}inttruck::get_passengers(){returnpassenger_load;}floattruck::efficiency(){returnpayload/(payload+weight);}voidtruck::print(){cout<<"卡车"<<endl;vehicle::print();cout<<"载人:"<<passenger_load<<"人。"<<endl;cout<<"效率:"<<efficiency()<<endl;cout<<endl;}voidmain(){carcar1(4,1000,5);trucktru1(10,5000,3,340000);car1.print();tru1.print();}4、程序内容如下所示。#include<iostream.h>#include<string.h>classemployee{protected:intno;charname[10];floatsalary;public:employee(){cout<<"职工编号:";cin>>no;cout<<"职工姓名:";cin>>name;salary=0;}voidpay(){}voiddisplay(){}};classtechnician:publicemployee{private:floathourlyrate;intworkhours;public:technician(){hourlyrate=100;}voidpay(){cout<<name<<"本月工作时数:";cin>>workhours;salary=hourlyrate*workhours;}voiddisplay(){cout<<"兼职技术人员:"<<name<<"(编号为:"<<no\<<")"<<"本月工资:"<<salary<<endl;}};classsalesman:virtualpublicemployee{protected:floatcommrate;floatsales;public:salesman(){commrate=0.04;}voidpay(){cout<<name<<"本月销售额:";cin>>sales;salary=sales*commrate;}voiddisplay(){cout<<"销售员:"<<name<<"(编号为:"<<no<<")"<<"本月工资:"<<
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年教学游戏活动设计与实施
- 2026年学校迎新活动方案策划
- 2026年销售业务流程管理方案
- 九位数奥数题目及答案
- 企业离职交接方案
- 第4单元 复习指导教学设计高中地理选择性必修3中图中华地图版
- 企业架构设计方案
- 第二节 学习使用天平和量筒教学设计初中物理沪科版八年级全一册-沪科版2012
- 企业锅炉运行管控方案
- 企业工单统计分析
- 2026年辽宁锦州海通实业有限公司计划招录28人备考题库及答案详解参考
- 2026年西安工业大学招聘备考题库(14人)含答案详解
- 2026青海数字经济发展集团有限公司社会招聘9人笔试参考题库及答案详解
- 2024-2025学年上海市黄浦区七年级(下)期末数学试卷(含解析)
- 2026年安徽省体育彩票管理中心编外聘用人员公开招聘11名考试参考题库及答案解析
- 2026年《中华民族共同体概论》第13讲先锋队与中华民族独立解放(1919-1949)新版课件
- 2026年江西高考化学题库及答案
- 2026年贪污贿赂刑事案件司法解释(二)课件
- 2025-2026学年沪语童谣教案
- 电子屏安全责任制度
- 2025-2026学年北师大版(2024)二年级数学下册期末综合素养评价卷(模拟冲刺二)(含答案)
评论
0/150
提交评论