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

    c++机考题目加答案.docx

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

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

    c++机考题目加答案.docx

    /集合中元素个数的最大值,默认为int型/错误代码/ Bool类型定义/定义集合类/存储元素的数组/集合中元素的个数/推断一个数是否为集合中的元素/向集合中添加元素/删除集合中的元素/把当前集合拷贝到形参指针指向的集合中/推断两个集合是否相等/交集/并集1.实现一个大小固定的整型数据元素集合及其相应操作(元素的添加、删除;推断两个集合是否相等;推断一个数是否为 集合中的元素;交集;并集)。P76.列3.24#include <iostream.h>const maxCard = 16;enum ErrCode noErr, overflow;enum Bool False, True;class Set(int elemsmaxCard;int card;public:void EmptySet( )card = 0;Bool Member(int);ErrCode AddElem(int);void Rmv日em(int);void Copy(Set *);Bool Equal(Set *);void Print();void lntersect(Set *, Set *);ErrCode Union(Set *, Set *););Bool Set:Member(int elem)(for(int i=0;i<card;+i) if(elemsi = elem)return True;return False;)ErrCode Set:Add日em(int elem)(if(Member(elem)return noErr;if(card < maxCard);int main()(Student stu50;int i,q,a,z,x,max=O,aver=O; i 为循环变量,q:学号;a:数学成果;/z:英语成果;x:计算机成果int count = 0; 表示学生人数char* we=new char10;/输入学生信息for(;)(cout<<“请输入学生的学号、姓名、数学成果、英语成果、计算机成果:(若输入的学号为0则 表示退出)"« endl;cin»q»we»a»z»x;if (q =0)break;stucount+.set_stu_inf(q,we,a,z/x);if(max>a+z+x);else max=a+z+x;aver+=(a+z+x);)/输出全部学生信息cout<<”学生信息为:"<<endl<<endl;for( i = 0; i < count; i+)stui.print();cout«endl;cout<<"全班学生总成果最高分为" <<max«endl<<"全班学生总平均分为"v<aver73<<endl<<endl;cout<< “请输入要查的学生的学号:"<<endl;cin»q;for( i = 0; i < count; i+)if (q=stui.get_reg_num()(cout<<"此学生信息为:“cvendl;stui.print();break;)if (i=count)cout<<"查无此人,<<endl;return 0;)4.定义复数类complex,运用友元函数,完成复数的加法、减法、乘数运算,以及复数的输出。P95.4.8#include<iostream.h> class Complex(double real;double image;public:Complex(double r = 0, double i = 0) (real = r; image = i;)friend void inputcomplex(Complex &comp);friend Complex addcomplex(Complex &cl, Complex &c2);friend Complex subcomplex(Complex &cl, Complex &c2);friend Complex mulcomplex(Complex &cl, Complex &c2);friend void outputcomplex(Complex &comp););void inputcomplex(Complex &comp)(cin » comp.real » comp.image;)Complex addcomplex(Complex &cl, Complex &c2)(Complex c;c.real = cl.real + c2.real;c.image = cl.image + c2.image;return c;)Complex subcomplex(Complex &cl, Complex &c2)(Complex c;c.real = cl.real - c2.real;c.image = cl.image - c2.image;return c;)Complex mulcomplex(Complex &cl, Complex &c2)(Complex c;c.real = cl.real * c2.real - cl.image * c2.image;c.image = cl.real * c2.image + cl.image * c2.real;return c;void outputcomplex(Complex &comp)cout « "C' « comp.real «« comp.image «)void main()(Complex cl,c2,result;cout<<“请输入第一个复数的实部和虚部:“<<endl;inputcomplex(cl);cout<< “请输入其次个复数的实部和虚部:“<<endl;inputcomplex(c2);result=addcomplex(cl,c2);outputcomplex(cl);cout«"+"outputcomplex(c2);cout<<”=";outputcomplex(result);cout«"n"«endl;result=subcomplex(cl,c2);outputcomplex(cl);cout«,-n;outputcomplex(c2);cout«"=,;outputcomplex(result);cout«"n"«endl;result=mulcomplex(cl,c2);outputcomplex(cl);cout«"*n;outputcomplex(c2);cout«"="outputcomplex(result);cout«endl;)5.某单位全部员工依据领取薪金的方式分为如卜几类:时薪工(hourlyworker)>计件工(pieceworker)>经理(manager)、 佣金工(commissionworker)。时薪工按工作的小时支付工资,对于每周超过50小时的加班时间,依据附加50%薪水支付 工资。按生产的每件产品给计件工支付固定工资,假定该工人仅制造一种产品。经理每周得到固定的工资。佣金工每周 得到少许的固定保底工资,加上该工人在一周内总销售的固定百分比。试编制一个程序来实现该单位的全部员工类,并 加以测试。P125 5.15#include<iostream.h>#include <string.h>class employee(protected:char name10;char emp_id8;double salary;/雇员类/姓名/职工号/薪金数public:void setinfo(char *empnamejnt empsex,char*empid)/设置雇员的基本信息strcpy(name/empname);strcpy(emp_id,empid);)void getinfo(char*empname/char*empid)(strcpy(empname,name);strcpy(empid,empjd);)double getsalary()(return salary;);/取得雇员的基本信息/取得所应得的总薪金数class hourlyworker:public employee / 时薪工protected:int hours;double perhourpay;public:hourlyworkerf)(hours=0;perhourpay=15.6;)int gethours()(return hours;void sethours(int h)(hours=h;)double getperhourpay()(return perhourpay;)void setperhourpay(double pay)(perhourpay=pay;)void computepay()/工作的小时数/每小时应得的酬劳/取得某人工作的小时数/设置某人工作的小时数/取得每小时应得的酬劳/设置每小时应得的酬劳/计算工资if(hours<=40)salary=perhourpay*hours;elsesalary=perhourpay*40+(hours-40)*1.5*perhourpay;);class pieceworker:public employee计件工protected:int pieces;double perpiecepay;public:pieceworker()(pieces=0;perpiecepay=26.8;)int getpieces()return pieces;void setpieces(int p)pieces=p;double getperpiecepay()return perpiecepay;void setperpiecepay(double ppp)perpiecepay=ppp;void computepay()salary=pieces*perpiecepay;);class manager:public employee(public:void setsalary(double s)salary=s;);class commissionworker:public employee(protected:double basesalary;double total;/每周所生产的工件数/每个工件所应得的工资数/设置生产的工件总数/经理类/设置经理的工资数/佣金工类保底工资/ 一周内的总销售额/提成的额度小时工double percent;public:commissionworker()(basesalary=500;total=0;percent=O.Ol;)double getbase()return basesalary;void setbase(double base)basesalary=base;double gettotal()return total;void settotal(double t)total =t;double getpercent()return percent;double setpercent(double p)percent=p;void computepay()salary=basesalary+total*percent;);void main()(char name10Lemp_id9;hourlyworker hworker;hworker.setinfoC'John'O/'OOl");hworker.sethours(65);hworker.getinfo(name,emp_id);hworker putepay();cout«"hourlyworker "«name«,Hs id is: ,«emp_id«" and salary is:"«hworker.getsalary()«endl;pieceworker pworker;/ 计件工pworker.setinfoC'Mark'O/'OO?");pworker.setpieces(lOO);pworker putepay();pworker.getinfo(name,empjd);cout«"pieceworker "«name«,s id is: H«empjd«" and salary is:"«pworker.getsalary()«endl;commissionworker cworker;/ 佣金工cworker.settotal(234.6);cworker.setinfoC'Jane'O/'OOB");cworker putepay();cworker.getinfo(name,emp_id);cout«"commissionworker "«name«,"s id is: "«emp_id«H and salary is:"«cworker.getsalary()«endl;manager mana;mana.setinfoC'Mike",!,"004");mana.setsalary(3500);mana.getinfo(name,empjd);cout«"manager "«name«'"s id is: ,«emp_id«H and salary is:"«mana.getsalary()«endl;)6 .考虑高校的学生状况,试利用单一继承来实现学生和毕业生两个类,设计相关的数据成员及函数,编写程序对继承状况 进行测试。P133. 11实P57提示:作为学生确定有学号、姓名、性别、学校名称及入学时间等基本信息,而毕业生除了这些信息外,还应有毕'小时 间、所获学位的信息,可依据这些内容设计类的数据成员,也可加入一些其他信息,除了设计对数据进行相应操作的成 员函数外,还要考虑到成员类型、继承模式,并在main()函数中进行相应测试。可设计多种继承模式来测试继承的属性。#include<iostream>#include<string>using namespace std;class Student/定义基类Studentpublic:/公有函数成员Student(int n,char *na,int g):number(n),grade(g) strcpy(name,na);)int getNumber()return number;)char * getName()return name;)int getGrade()return grade;)void display()cout<<“学号:"«number«endl<<“姓名:"«name«endl<<“年级:"«grade«endl;)private:int number;char name20;int grade;/私有数据成员/学号/姓名/年级);定义毕业生类class Graduate:public Studentchar designSubject20;public:elemscard+ = elem;return noErr;)return overflow;)void Set:Rmv日em(int elem)(for(int i = 0; i<card; +i)if(elemsi = elem)(for(;i<card-l;+i)elemsi = elemsi+l;-card;return;)void Set:Copy(Set *set)(for(int i = 0; i < card; +i)set->elemsi = elemsi;set->card = card;)Bool Set:Equal(Set *set)(if(card != set->card)return False;for(int i = 0; i < card;+i)/推断当前集合的某元素是否是set所指集合中的元素if(!set->Member(elemsi)return False;return True;)void Set:Print()Graduate(int n,char *na,char * deSub,int g=4):Student(n,na,g)strcpy(designSubject,deSub); void display()Student:display();cout<<”设计题目:"«designSubject«endl;);int main()创建对象Student li(2,"Li Ming”,3);Graduate zhang(3,"Zhang Gang”,”学生成果管理系统”);/显示对象的相关信息li.displayf);cout« endl«H" «endl;zhang.display();return 0;)7 .定义一个哺乳动物类,再由此派生出人类、狗类和猫类,这些类中均有speak()函数,视察在调用过程中,原委运用了哪 一个类的speak。函数。P133. 12#include<iostream>#include<string>using namespace std;class Animalfloat weight;public:void speak()void setWeight(float wt)weight = wt;float getWeight()return weight;);class Human:public Animal public:void speak() cout<<“说话”<<endl;);class Cat:public Animalpublic:void speak() cout«,,nffl°S"«endl;);class Dog:public Animalpublic:void speak() cout<<"汪汪"<<endl;);int main()/定义三个对象Human hm;Cat cat;Dog dog;/调用不同类的speak函数 cout«"A: hm.speak();coutvc"猫:"cat.speak();cout<< ”狗:";dog.speak();return 0;8 .设计一个能细分为矩形、三角形、圆形和椭圆形的图形类。运用继承将这些图形分类,找出能作为基类部分的共同特征 (如宽、高、中心点等)和方法(如初始法、求面积等),并看看这些图形能否进一步划分为子类。P133. 10#include<iostream> using namespace std;class Figure/定义基类图形类( public:/公有函数成员Figure(int wid)width = wid; float area() int getWidth()return width; private:/私有数据成员int width;/宽度或半径);class Rectangle:public Figure / 定义矩形类int height;public:Rectangle(int wid, int hei):Figure(wid)height = hei;float area()return getWidth() * height;);class Triangle:public Figure / 定义三角形类 int height;public:Triangle(int wid, int hei):Figure(wid)height = hei;float area()return 1.0/2* getWidth() * height;);class Circle:public Figure / 定义圆类public:Circle(int wid):Figure(wid)float area()return 3.14 * getWidth() * getWidth(); );int main()Rectangle rect(5,4);Triangle tri(5,4);Circle cir(5);cout<< ”矩形的面积是:" <<rect.area()«endl<<"三角形的面积是:"«tri.area()«endl<<"圆的面积是:"<<cir.area()v<endl;return 0; )9.设id一1个基类animal和它的派生类tiger、sheep,实现虚函数。提示:可自行定义这些类的成员变量,但animal基类中应有动物性别的成员变量,但要说明每种动物的叫soar ()及吃eat ()的成员函数,可用cout输出来表示。要求每个派生类生成两个对象,打乱次序存于一个数组中,然后用循环程序访 问其叫与吃的成员函数,必需用到虚函数。P173 .9#include<iostream>using namespace std;class animalprotected:char sex;public:virtual void soar()=0;virtual void eat()=0;);class tiger:public animalpublic:tiger(char s='f')sex = s;void soar()cout«"tiger 呼L"<<endl;)void eat()cout«"tiger 吃肉”<<endl;);class sheep:public animalpublic:sheep(char s='f')sex = s;void soar()cout«"sheep: 羊”<<endl;)void eat()cout«"sheep 吃草“<<endl;);int main()tiger tl,t2;sheep sl,s2;animal *array4 =&tl,&sl,&t2, &s2;for(int i = 0; i < 4; i+)arrayi->eat();arrayi->soar();) return 0; )10有三角形、正方形和圆形三种图形,求它们各自的面积。可以从中抽象出一个基类,在基类中声明一个虚函数,用来求面 积,并利用单界面、多实现版本设计各个图形求面积的方法。P173. 10#include<iostream> using namespace std; class shape/抽象类的定义( public: virtual float area()=0; ); class triangle:public shape / 三角形类 ( protected: float h,w; public: triangle(float hh,float ww) h=hh;w=ww; float area() return h*w*0.5;); class rectangle:public triangle / 矩形类 ( public: rectangle(float h,float w):triangle(h,w) () float area() return h*w;); class circle:public shape/ 圆类private:float radius;public: circle(float r) radius=r; float area() return radius*radius*3.14;);int main() (shape*s4;/ 指针数组s0=new triangle(3,4);sl=new rectangle(2,4);s2=new circle(5);cout<<"三角形、矩形和圆的面积分别为:“<<endl;for (int i=0;i<3;i+)cout«si->area()«'t'cout«endl;return 0; )IL编写程序,计算汽车运行的时间,首先建立基类car,其中含有数据成员distance存储两点间的距离,假设距离以英里计 算,速度为每小时80英里,运用虚函数travel_time ()计算并显示通过这段距离的时间,在派生类kilometre中,假定 距离以千米计算,速度为每小时120千米,运用函数travel_time ()计算并显示通过这段距离的时间。P173.6#include <iostream>using namespace std; class car(protected:double distance;public:car(double f)distance=f;virtual void travel_time() (cout«"car: travel time at 80 kph:cout«distance/80«endl;);class truck:public car (public:truck(double f):car(f)void travel_time() (cout«"truck:travel time at 120 kph:cout«distance/120«endl;);int main() (car *p,car_time(150);truck truck_time(150);p=&car_time;p->travel_time();p=&truck_time;p->travel_time();return 0; )12.设计一个留言类,实现以下功能:(1)程序第一次运行时,建立一个message.txt文本文件,并把用户输入的信息存入该 文件。(2)以后每次运行时,都先读取该文件的内容并显示给用户,然后由用户输入新的信息,退出时将新的信息存入 这个文件中。保存到文件中的内容,既可以是最新的信息,也可以包括以前全部的信息,用户可自己选择。P196. 6#include <iostream.h> #include <fstream.h>class Message public:char msg20;friend ostream & operator «(ostream &out, Message &s);friend i st ream & operator »(istream &in, Message &s););ostream & operator «(ostream &out, Message &s)(out«s.msg«'n,;return out;)istream & operator »(istream &in, Message &s)(in»s.msg;return in;)int main()(ofstream ofile;ifstream ifile;Message s;int option;ifile.open("message.txt",ios:nocreate);if(ifile.fail()第一次运行建立文件,输入信息ofile.open("message.txtH);cout<<”请输入一条信息:"<<endl;cin»s;ofile«s;ofile.close();ifile.close();else 以后运行首先显示文件内容(cout<<”文件的内容为:"<<endl;ifile»s;while(!ifile.eof()(cout«s;ifile»s;)ifile.close();for(;) 显示菜单以供用户进行选择(cout<< ”请选择:" <<endl«"1追加信息,<<endl«H2覆盖原有信息“<<endl«"3显示文件信息,<<endl«"0退出"<<endl;cin»option;switch(option)ofile.openC'message.txf'Josiiapp);cout<<”请输入一条信息:"<<endl;cin»s;ofile«s;ofile.close();break;ofile.open(,message.txt",ios:trunc);cout<<”请输入一条信息:"<<endl;cout«"',;for(int i = 0; i < card; +i)cout« elemsi«cout«Hn")void Set:lntersect(Set *set, Set *res)/ 交集:*this A *set->*res(res->card = 0;for(int i = 0; i < card; +i)for(int j = 0; j < set->card; +j)if(elemsi = set->elemsj)res->elemsres->card+ = elemsi;break;)ErrCode Set:Union(Set *set,Set *res) / 并集:*set U *this->*res (set->Copy(res);for(int i = 0; i < card; +i)if(res->AddElem(elemsi) = overflow)return overflow;return noErr;)/ F面是测试用的主程序void main()(Set si, s2, s3;sl.EmptySet();s2.EmptySet();s3.EmptySet();sl.AddElem(lO);sl.AddElem(20);sl.AddElem(30);cin»s;ofile«s;ofile.close();break;ifile.open("message.txt");ifile»s;while(!ifile.eof()(cout«s;ifile»s;)ifile.close();break;case 0:ofile.closef);return 0;)return 0;sl.AddElem(40);s2.AddElem(30);s2.AddElem(50);s2.AddElem(10);s2.AddElem(60);cout«"sl=n; sl.Print();cout«"s2=" s2.Print();s2.Rmv 日 em(50);cout«"s2-50=n; s2.Print();if(sl.Member(20)cout«"20 is in sln"sl.lntersect(&s2,&s3);cout«"sl intsec s2 =H; s3.Print();sl.Union(&s2,&s3);cout«"sl union s2 =" s3.Print();if(!sl.Equal(&s2)cout«"sl!=s2n")2.实现一个大小可变的整型数据元素集合,集合可存储的数据元素个数在对象构造时给定,由构造函数为数据元素支配存 储空间,在对象被释放时由析构函数释放存储空间。P78 3.25#include <iostream.h>const maxCard=16;enum ErrCode noErr, overflow;enum Bool False, True;class Set int size;int *elems;int card;public:Set(int sz=maxCard)card=O;size=sz;elems=new intsize;/集合中元素个数的缺省最大值/错误代码/ Bool类型定义/元素的最大个数/存储元素的数组/集合中元素的个数Set( )delete elems;Bool Member(int);ErrCode AddElem(int);void Rmv日em(int);void Copy(Set *);Bool Equal(Set *);void Print();void lntersect(Set *, Set *);ErrCode Union(Set *, Set *););Bool Set:Member(int elem)(for(int

    注意事项

    本文(c++机考题目加答案.docx)为本站会员(太**)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

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




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

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

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

    收起
    展开