c++习题参考答案2.doc_第1页
c++习题参考答案2.doc_第2页
c++习题参考答案2.doc_第3页
c++习题参考答案2.doc_第4页
c++习题参考答案2.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

第七章一、简答题1答:链表是一种重要的数据结构,是一种动态分配内存的结构。相对来说,用数组不能实现动态存储。当用数组来进行数据存储时,必须事先确定固定的长度;而链表是一组结点的序列,每个结点是由系统存储到不同的地址空间的。它们之间的连接是由指针实现的。插入和删除一个结点是非常容易的,而且链表比数组能表示更复杂的数据类型。插入和删除一个结点的基本方法就是,首先对链表进行遍历,找到需要插入的地方或需要删除的结点,然后改变相邻结点指针的连接关系就可以了。2答:该程序实现了一个时钟的显示。首先定义了一个结构clock,包括三个整形成员hour,minute,second来表示时间,并且定义了clock型的变量aClock。在主程序中,首先对aClock各成员赋值,然后循环10000次,每次循环调用一次子函数updata()和display(),用来计时和显示时间。程序的输出结果是:0:0:10:0:20:0:30:0:4.0:0:590:1:00:1:1.0:59:591:0:0.2:46:40二、编程题1、#include#include#includestruct Student int code; char name20; int cj1;int cj2;void main() Student a10 =8311001, Smith, 56, 87,8512901, Kerry, 43, 87,9022101, Levy, 89, 99,8508020, Doris, 87, 64,8881232, Ella, 76, 67,9123001, Carrie, 56, 98,8100825, Barbara, 76, 67,9012120, Carmen, 76, 70,8712001, Brice, 90, 98,8100923, Auden, 80,85; int zcj10; for(int i=0; i10; i+)zcji=ai.cj1+ai.cj2;int max,k;for(i=0;i3;i+) max=0;k=0;for(int j=0;j10;j+)if(maxzcjj)max=zcjj; k=j; zcjk=0; coutak.code ak.cj1 ak.cj2endl; 2、#include#include#includestruct Student int code; char name20; char sex; unsigned age;struct Node int code; char name20; char sex; unsigned age; Node* next;void Insert(Node*& head, Node*& t) if(!head | t-age head-age) t-next = head; head = t; return; for(Node* p=head; p-next; p=p-next) if(t-age p-next-age) break; t-next = p-next; p-next = t;void Display(const Node*& head)coutnext) coutsetw(10) code setw(12) namesetw(8) sex=M?male:female) setw(6) age endl;void main() Student a10 =8311001, Smith, M, 18,8512901, Kerry, F, 19,9022101, Levy, M, 16,8508020, Doris, F, 20,8881232, Ella, F, 18,9123001, Carrie, M, 22,8100825, Barbara, F, 23,9012120, Carmen, M, 20,8712001, Brice, M, 19,8100923, Auden, M, 20; Node* first=NULL; for(int i=0; icode=ai.code; strcpy(pN-name,);pN-sex=ai.sex; pN-age=ai.age; Insert(first, pN); Display(first);3、#include#include#includestruct Student char name20; Student* next; ;void Display(const Student*& head) coutnext) coutsetw(12) namepS-name; pEnd=pS;while(inext=pS;pEnd=pS; pS=new Student;cin pS-name;i+;pEnd-next=NULL;delete pS;return(head);Student * Insert(Student *head,char str1, char str2)Student*stud=new Student; strcpy(stud-name,str2);if(head=NULL)/空表,将结点插在head指针下即可 head=stud;/表示链首stud-next=NULL;/表示链尾return head;if(strcmp(head-name,stud-name)=0)/结点插入的位置在链首stud-next=head;/指向链首结点head=stud;/插入结点成为链首return head;Student *pguard=head;while(pguard-next!=NULL&strcmp(pguard-next-name,str1)!=0) pguard=pguard-next;stud-next=pguard-next;pguard-next=stud;return head;void main() Student* first=NULL; first=Create();first=Insert(first,Jeck,Zhanghai); Display(first);第八章一、简答题1答:在本题中,程序中共定义了三个带参数的宏,且后两个宏定义的替换文本中又包含前面定义过的宏;程序中的输出语句不仅是用宏名表示的,而且还放在了循环语句中。事实上,只要按照宏替换的原则将宏名一步步展开,分析出结果并不困难。下面依次给出语句PRINT2(k,k+12.4);的宏替换结果:首先将宏PRINT2用对应的替换文本替换,并用实参k替换形参a,用实参k+12.4替换形参b,得到PR(k);PRINT1(k+12.4);替换后的语句中又出现了两个宏PR和PRINT1。若先将PRINT1展开,以上语句变为:PR(k);PR(k+12.4);coutendl;最后将PR宏展开,得到: coutk;coutk+12.4;coutendl;这时可以清楚的看出:原本一条语句PRINT2(k,k+12.4);经宏替换后变成了三条语句。由于for循环中没有使用复合语句,所以只有第一条语句coutk;属于for循环体,后两条语句要等执行完循环体后才执行。根据for语句中的条件可知,循环体将执行3次,k的取值依次为0、50、100,每输出完一项并不换行,而是用一个逗号分隔。退出for循环后,k的值为150。执行语句coutk+12.4;时,输出162.4,然后执行语句cout=0)&(c)=9)?1:0 第九章一、简答题1答:#include class Pointpublic: void Set(double ix,double iy) /设置坐标 x=ix; y=iy; double xOffset() /取y轴坐标分量 return x; double yOffset() /取x轴坐标分量 return y; double angle() /取点的极坐标 return (180/3.14159)*atan2(y,x); double radius() /取点的极坐标半径 return sqrt(x*x+y*y); protected: double x; /x轴分量 double y; /y轴分量 /error: 缺少分号void main() Point p; double x,y; cout x y; p.Set(x,y); p.x+=5; /error: 私有成员不能在类外被访问 p.y+=6; /error: 同上 cout angle= p.angle() ,radius= p.radius() ,x offset= p.xOffset(),y offset= p.yOffset() endl;2答:Constructing normally.Constructing with a number: 20Display a number: 1Display a number: 20Destructing.Destructing.四、编程题1答:#ifndef DATE#define DATE#include class Datepublic: void Display(); void AddOneDay(); void SetDay(int y,int m,int d);protected: bool Legal(int y, int m, int d); bool IsLeapYear(int y); int year; int month; int day;void Date:Display() cout day / month / year 9999|y1|d1|m12) return false; int dayLimit=31; switch(m) case 4: case 6: case 9: case 11: dayLimit-; if(m=2) dayLimit = IsLeapYear(y) ? 29 : 28; return (ddayLimit)? false : true;bool Date:IsLeapYear(int y) return !(y%4)&(y%100)|!(y%400);#endif2答:参考程序如下:#include class Complexdouble R;double I;public:Complex(double R = 0.0, double I = 0.0)this-R = R;this-I = I;Complex()/* copy constructor function */Complex(const Complex& c)this-R = c.R;this-I = c.I;/* (+ - * /) */friend const Complex operator+(const Complex& a, const Complex& b);friend const Complex operator-(const Complex& a, const Complex& b);friend const Complex operator*(const Complex& a, const Complex& b);friend const Complex operator/(const Complex& a, const Complex& b);/* - */friend const Complex operator-(const Complex& a);/* = != */friend bool operator=(const Complex& a, const Complex& b);friend bool operator!=(const Complex& a, const Complex& b);friend ostream& operator(ostream& out, const Complex& c);/* + overloading */const Complex operator +(const Complex& a, const Complex& b)Complex c;c.R = a.R + b.R;c.I = a.I + b.I;return c;/* - overloading */const Complex operator -(const Complex& a, const Complex& b)Complex c;c.R = a.R - b.R;c.I = a.I - b.I;return c;/* * overloading */const Complex operator *(const Complex& a, const Complex& b)Complex c;c.R = (a.R * b.R) - (a.I * b.I);c.I = (a.R * b.I) + (a.I * b.R);return c;/* / overloading */const Complex operator /(const Complex& a, const Complex& b)Complex c;double temp;temp = b.R*b.R + b.I*b.I;if(temp = 0)c.R = 0; c.I = 0;return c;c.R = (a.R*b.R + a.I*b.I) / temp;c.I = (a.I*b.R - a.R*b.I) / temp;return c;/* - overloading */const Complex operator -(const Complex& a)Complex c;c.R = - a.R;c.I = - a.I;return c;/* = overloading */bool operator=(const Complex& a, const Complex& b)if(a.I = b.I) & (a.R = b.R)return true;elsereturn false;/* != overloading */bool operator!=(const Complex& a, const Complex& b)if(a.I = b.I) & (a.R = b.R)return false;elsereturn true;/* ostream overloading */ostream& operator(ostream& out, const Complex& c)out(c.R)+(c.I)i;return out;void main()Complex a(1);Complex b(1, 2);Complex c;double da = 100.0;int ia = 99;couta = aendl;coutb = bend

温馨提示

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

评论

0/150

提交评论