欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

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

    • 资源ID:58300704       资源大小:58.04KB        全文页数:37页
    • 资源格式: DOC        下载积分:9金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要9金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    第六章习题答案一、选择填空 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、 18、 19、 20、× 21、× 22、×三、分析下列程序的输出结果。1、运营该程序输出结果如下所示。Default constructor calledConstructor calleda=0,b=0a=4,b=82、运营该程序输出结果如下所示。a=7,b=93、运营该程序输出结果如下所示。1044、运营该程序输出结果如下所示。1035,789.5045、运营该程序输出结果如下所示。10,1,2,3,4,5,6,7,8111,12,13,14,15,16,17,18,1919,18,17,16,15,14,13,12,116、运营该程序输出结果如下所示。Starting1:Default constructor called.Default constructor called.Default constructor called.Eding1:Starting2:Constructor: a=5,b=6Constructor: a=7,b=8Constructor: a=9,b=10Ending2:Destructor called.a=9,b=10Destructor called.a=7,b=8Destructor called.a=5,b=6Destructor called.a=5,b=6 Destructor called.a=3,b=4Destructor called.a=1,b=27、运营该程序输出结果如下所示。Default constructor called.Default constructor called.Default constructor called.Default constructor called.Destructor called.Constructor1 called.Destructor called.Constructor2 called.Destructor called.x=0,y=0x=5,y=0x=2,y=3Destructor called.Destructor called.Destructor called.8、运营该程序输出结果如下所示。Constructor called.0Constructor called.5Destructor called.55Destructor called.59、运营该程序输出结果如下所示。Constructor called.55Destructor called.510、运营该程序输出结果如下所示。Default Constructor called.Constructor:real=6.8,imag=0Constructor:real=5.6,imag=7.90+0I6.8+0I5.6+7.9IConstructor:real=1.2,imag=3.4Constructor:real=5,imag=0Default Constructor called.1.2+3.4I5+0I0+0I11、答:该程序中用string.h中所包含的函数有3种,它们是strcpy( )、strcat( )和strlen( ).该程序中使用了函数重载。它们是下述两个构造函数重载:String( )和String(const char * str)。类中成员函数Setc( )的功能是用来设立字符数组中某位置的一个指定字符。类中成员函数Getc( )的功能是用来从某个字符数组中获取指定位置的字符。类中成员函数Append( )的功能是在一个字符数组中追加一个指定的字符串,即将指定的字符串添加到已知串的后边。不行。该程序中有两处使用了new运算符。运营该程序输出结果如下所示。empty.a string.9a string.ithis a string.四、改正以下程序中的错误。1、该程序中point类的构造函数定义不对的,在main()中队数据成员的访问不对的,修改如下:#include <iostream.h>class pointint x1,x2;public: point(int x,int y)point:x=x;point:y=y; void disp() cout<<x1<<endl; cout<<x2<<endl;/;void main()point data(5,5);data.disp();2、在main()函数中的p.x+=5和p.y+=6两个语句是错误的,由于保护数据仅能被类的成员函数访问。五、按下列规定编写程序。1、程序内容如下所示。#include <iostream.h>class Testpublic: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)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<<add(a2)<<endl;cout<<Test:fun(a2)<<endl;2、程序内容如下所示。#include <iostream.h>#include <string.h>class Product char *name; int price; int quantity;public: Product(char *n,int p,int q) name =new charstrlen(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<<"数量不够"<<endl; else quantity-=n; r=money%price; cout<<"产品:"<<name<<"单价:"<<price<<"元 顾客" cout<<money<<"元,买了"<<n<<"台,剩余"<<r<<"元"<<endl;void get() const cout<<"产品:"<<name<<"单价:"<<price<<"元 剩余"<<quantity<<"台"<<endl;void main() Product p1("电视机",2023,15); p1.buy(7000); p1.get(); p1.buy(4500); p1.get();3、程序内容如下所示。#include <iostream.h>#include <stdlib.h>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(); 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 day5; char month5; char year5; _itoa(m_nDay,day,10); _itoa(m_nMonth,month,10); _itoa(m_nYear,year,10); cout<<day<<"/"<<month<<"/"<<year<<endl;void CDate:AddDay() m_nDay+; if (IsLeapYear() if(m_nMonth=2)&&(m_nDay=30)m_nMonth+; m_nDay=1; return; else 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; 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.SetDate(2023,2,28); cout<<"当前日期=>:" d.Display(); d.AddDay(); cout<<"当前日期加1=>:" d.Display();4、程序内容如下所示。#include <iostream.h>class Tc private:double unlead,lead,total;int unprice,price; public:Tc()unprice=17;price=16;void getdata() cout<<"无铅汽油总量;" cin>>unlead; cout<<"有铅汽油总量;" cin>>lead; total=unprice*unlead+price*lead;void disp() cout<<"总收入:"<<total<<endl;void main() Tc A; A.getdata(); A.disp();5、程序内容如下所示。#include <iostream.h>class CFactorialint value;int fact; public:CFactorial(int val);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<<value<<"!="<<fact<<endl;void main() CFactorial A(5); A.CalculateFactorial(); A.Display();6、程序内容如下所示。#include <iostream.h>#include <iomanip.h>class rectangle private:float ledge,sedge; public:rectangle();rectangle(float a,float b) ledge=a;sedge=b;float area() return ledge*sedge;void addarea(rectangle r1,rectangle r2) cout<<"总面积:"<<r1.ledge*r1.sedge+ r2.ledge*r2.sedge<<endl;void main() rectangle A(3.5,2.5),B(4.2,3.8),C; C.addarea(A,B);7、程序内容如下所示。#include <iostream.h>#include <iomanip.h>class rectangle private:float ledge,sedge; public:rectangle();rectangle(float a,float b) ledge=a;sedge=b;float area() return ledge*sedge;void showlength() cout<<"周长:"<<(ledge+sedge)*2<<endl;rectangle tlength(rectangle r2) rectangle temp; temp.ledge=ledge+r2.ledge; temp.sedge=sedge+r2.sedge; 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 <iostream.h>#include <iomanip.h>class Line private:double x1,x2,y1,y2; public:Line();Line(double a,double b,double c,double d) x1=a;y1=b;x2=c;y2=d; cout<<"线段端点"<<x1<<","<<y1<<")-("<<x2<<","<<y2<<")"<<endl;friend twoline(Line l1,Line l2) double r1=l2.y1*(l2.x2-l1.x1)-l1.y1*(l2.x2-l2.x1)+(l1.x1-l2.x1)*(l2.y2-l2.y1); double r2=(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<<""<<endl;else if (r>0&&r<1&&t>=1) cout<<""<<endl; else if(r>=1 | r<=0)&& t>0&&t<1) cout<<""<<endl; else cout<<""<<endl;void main() Line A(2,2,18,18),B(1,12,19,5); twoline(A,B);9、本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(no),姓名(name)和成绩(degree),而成绩degree是类cdegree的对象。cdegree类有3个数据成员,分别为数学(math),英语(english)和物理(phy)分数。程序内容如下所示。#include <iostream.h>class student int no; char name10; 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<<"物理分数:" cin>>degree.phy; void disp() cout<<"学号:"<<no<<endl; cout<<"姓名:"<<name<<endl; cout<<"数学分数:"<<degree.math<<endl; cout<<"英语分数:"<<degree.english<<endl; cout<<"物理分数:"<<degree.phy<<endl;void main() student stud; stud.getdata(); stud.disp();10、程序内容如下所示。#include <iostream.h>class Studentint english,computer,total; public:void getscore();void display();void sort(Student *);Student();void Student:getscore() cout<<"输入英语成绩" cin>>english; cout<<"输入计算机成绩" cin>>computer; total=english+computer;void Student:sort(Student *p) int tmp,i,j; for(j=0;j<2;j+)for(i=0;i<2;i+) if (total<p->total) 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 Student:display() cout<<"英语="<<english<<"计算机="<<computer<<"总分="<<total<<endl;void main() Student *A3; for (int j=0;j<3;j+) Aj=new Student; cout<<"学生"<<j+1<<endl; Aj->getscore();int i;for (j=0;j<2;j+) for(i=0;i<2;i+) Ai->sort(Ai+1);cout<<endl<<"排序结果如下:"<<endl;for (i=0;i<3;i+) Ai->display();11、程序内容如下所示。#include <iostream.h>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) /入栈成员函数 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; cout<<"入栈顺序:" for (int i=0;i<9;i+) cout<<arri<<" "A.push(arri);cout<<endl<<"出栈顺序:"for (i=0;i<9;i+) cout<<A.pop()<<" "cout<<endl;12、程序内容如下所示。#include <iostream.h>struct list int data; list *next;class Queue list *ptrf,*ptrb; /队首和队尾指针 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;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<<arri<<" " A.enqueue(arri); cout<<endl<<"出队顺序:" for(i=0;i<5;i+) cout<<A.dequeue()<<" " cout<<endl;13、程序内容如下所示。#include<iostream.h>#include<stdlib.h>typedef struct tree int data; tree *left,*right,*father;*bstree;class Btree static int n; static int m;public: tree *root; Btree() root=NULL; void create_Btree(int); void inorder(bstree); /中序遍历 void display() cout<<endl<<"中序遍历序列: "<<endl;inorder(root);cout<<endl; int count(bstree); /计算二叉树的个数 int print(bstree,int); /输出要查找的值相应的双亲结点的内容;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<<k<<"的双亲结点的内容为:"<<p->father->data<<endl; return 0; void Btree:create_Btree(int x) bstree newnode=new tree; newnode->data=x; newnode->right=newnode->left=NULL; if(root=NULL) root=newnode; else bstree back; 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->father=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) /这是中序遍历二叉树,采用了递归的方法。 if(temp!=NULL) inorder(temp->left); cout<<temp->data<<" " inorder(temp->right); void main() Btree A; int array=7,4,1,5,12,8,13,11; int k; k=sizeof(array)/sizeof(array0); cout<<"建立排序二叉树顺序: "<<endl; for(int i=0;i<k;i+) cout<<arrayi<<" " A.create_Btree(arrayi); cout<<endl; cout<<"二叉树节点个数: "<<A.count(A.root)<<endl; A.display(); 14、程序内容如下所示。#include <iostream.h>#include <stdio.h>struct list int data; list *next;class Stacklist *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 *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; newnode->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; 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<<"输入的是回文数据。"<<endl;else cout<<"输入的不是回文数据。"<<endl;15、程序内容如下所示。#include <iostream.h>#include <iomanip.h>class magic int m44; 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:

    注意事项

    本文(2023年面向对象程序设计C山师习题答案.doc)为本站会员(可****阿)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

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

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

    收起
    展开