100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 大一下学期C++期末模拟考试参考答案

大一下学期C++期末模拟考试参考答案

时间:2018-09-18 22:46:30

相关推荐

大一下学期C++期末模拟考试参考答案

期末模拟考试参考答案

中国传媒大学春季学期,C++程序设计期末模拟答案

说在前面:以下并不是官方给出的答案,而是学生自制版,提交结果AC,但不能保证绝对的正确性,仅供大家参考。我在程序中以注释的形式,标记了一些要点/讲解,如果有问题可以微信问我(pyx2466079565,加的话备注一下)

还有几天就考试了,奥利给!

题目传送门:春季cpp模拟考试

1:qm计算长方体的体积

编写函数getVolume计算长方体体积,有三个形参:length(长)、width(宽)、height(高),其中width和height带有默认值2和3。

在主函数中以不同参数形式调用getVolume函数,输出体积。

样例输入

3 4 5

样例输出

The volume of box1 is 60The volume of box2 is 36The volume of box3 is 18

AC代码:

#include <iostream>using namespace std;int getVolume(int length,int width=2,int height=3){return length*width*height;//直接返回体积即可}int main(){int a,b,c;cin>>a>>b>>c;cout<<"The volume of box1 is "<<getVolume(a,b,c)<<endl;cout<<"The volume of box2 is "<<getVolume(a,b)<<endl;cout<<"The volume of box3 is "<<getVolume(a)<<endl;return 0;}

解题建议:这种需要自己写输出语句的题,为了防止格式错误导致PE(比如某两个单词之间可能是俩空格,你自己写的时候可能只写了一个),建议直接从样例复制粘贴。

2:qm-找数据并统计

写函数findcount(…)

函数功能是统计指针buf指向的数组中,x出现的次数和第一次出现的位置。参数buf指向数组第一个元素,n是数组元素个数,x是要统计的数,index是x在数组中第一次出现的位置(从1开始计算)。

#include <iostream>using namespace std;void display(int count,int index){if(count>0)cout<<count<<","<<index<<endl;elsecout<<"none"<<endl;}// 在此处补充你的代码int main(){int a[10] = {5, 7, 8, 9, 8, 2, 7, 3, 6, 4 };int b[7] = {5, 2, 5, 6, 5, 7, 9};int ct,index;ct = findcount(a, 10, 8,index);display(ct,index);ct = findcount(b, 7, 5,index);display(ct,index);ct = findcount(b, 7, 1,index);display(ct,index);return 0;}

样例输入

样例输出

2,33,1none

AC代码:

#include <iostream>using namespace std;void display(int count,int index){if(count>0)cout<<count<<","<<index<<endl;elsecout<<"none"<<endl;}//答案部分int findcount(int *p,int len,int t,int &loc){int num=0;//直接倒着找,每次找到目标值都记录,这样最后剩下的就是“第一个”出现的for(int i=len-1;i>=0;i--){if(p[i]==t){num++;loc=i+1;}}return num;}//答案部分结束int main(){int a[10] = {5, 7, 8, 9, 8, 2, 7, 3, 6, 4 };int b[7] = {5, 2, 5, 6, 5, 7, 9};int ct,index;ct = findcount(a, 10, 8,index);//注意理解这里的形式和上面函数的对应关系display(ct,index);ct = findcount(b, 7, 5,index);display(ct,index);ct = findcount(b, 7, 1,index);display(ct,index);return 0;}

解题建议:一定要按照下面已经给出的函数模样去写,下面给的findcount有四个参数,分别是数组名数组长度目标数字目标数字所在位置,所以我们自己写函数的时候,也一定得是这四个参数、按照这样的顺序排列。

前三个参数只需调用即可,而最后一个index,要求函数在计算过程中,改变index本身的值,并且使这种改变被保留下来,因此需要使用引用

3:qm类与对象-自定义数组

请输入十个数(int 范围内),输出其中的最大值,以面向对象的方式实现。输入数字以空格键隔开,输入输出参照样例。

#include <iostream>using namespace std;// 在此处补充你的代码int main( ){Array_max arrmax; //定义对象arrmaxarrmax.set_value( ); //调用arrmax的input_value函数,向数组元素输入数值arrmax.max_value( ); //调用arrmax的max_value函数,找出数组元素中的最大值arrmax.show_value( ); //调用arrmax的show_value函数,输出数组元素中的最大值return 0;}

样例输入

18 72 36 28 17 62 88 13 16 17

样例输出

max=88

AC代码

#include <iostream>using namespace std;//答案class Array_max{public:int a[10];//数组int find_max;//用于存储最大值void set_value(){for(int i=0;i<10;i++)cin>>a[i];}void max_value(){find_max=-1;//因为要找最大的数,而所有数都非负,所以把它初始化为-1for(int i=0;i<10;i++){find_max=max(find_max,a[i]);//让find_max等于“现在的值”和“a[i]”中大的那一项,最后留下的就是最大值}}void show_value(){cout<<"max="<<find_max;}};//答案结束int main( ){Array_max arrmax; //定义对象arrmaxarrmax.set_value( ); //调用arrmax的input_value函数,向数组元素输入数值arrmax.max_value( ); //调用arrmax的max_value函数,找出数组元素中的最大值arrmax.show_value( ); //调用arrmax的show_value函数,输出数组元素中的最大值return 0;}

4:qm男女生体重调查

学校开展对某班级学生的身体健康调查,假设班级人数不超过60人。需分别输入N个男生体重,M个女生体重(以kg为单位),分别统计全班人数、总体重以及平均体重,男生人数、总体重以及平均体重,女生人数、总体重以及平均体重。现要求用C++编程,模拟以上调查情况。

#include <iostream>using namespace std;class Student{// 在此处补充你的代码void print() {cout << Student::number << ":" << Student::weight / Student::number << endl;cout << Boy::number << ":" << Boy::weight / Boy::number << endl;cout << Girl::number << ":" << Girl::weight / Girl::number << endl; }int main(){Girl* g1 = new Girl(40);Girl* g2 = new Girl(41);Boy* b1 = new Boy(50);Boy* b2 = new Boy(52);print();}

样例输入

样例输出

4:45.752:512:40.5

AC代码

#include <iostream>using namespace std;//答案class Student{public:static int number;//静态变量static double weight;Student(double w){weight+=w;number++;}};int Student::number=0;//静态变量的初始化double Student::weight=0;class Boy{public:static int number;static double weight;Student s;//子对象Boy(double w):s(w)//子对象的初始化列表{weight+=w;number++;}};int Boy::number=0;double Boy::weight=0;class Girl{public:static int number;static double weight;Student s;//子对象Girl(double w):s(w)//子对象的初始化列表{weight+=w;number++;}};int Girl::number=0;double Girl::weight=0;//答案结束void print() {cout << Student::number << ":" << Student::weight / Student::number << endl;cout << Boy::number << ":" << Boy::weight / Boy::number << endl;cout << Girl::number << ":" << Girl::weight / Girl::number << endl; }int main(){Girl* g1 = new Girl(40);Girl* g2 = new Girl(41);Boy* b1 = new Boy(50);Boy* b2 = new Boy(52);print();}

解题建议:注意到print函数中,采用类名:: 变量名的方式输出,联想题目要求,可以得知需要使用静态变量。方便起见,我没有考虑继承,而是采用了子对象的形式,每个类都有自己单独的number和weight,这样不容易混乱

子对象相关知识可以参考我之前的博客:子对象&堆对象

5:qm继承与派生-工资管理

编写一个程序实现小型公司的工资管理。该公司主要有3类人员:经理(Manager)、技术人员(Technician)、销售员(Salesman)。这些人员都是公司职员,都有编号、姓名、基本工资等信息。月工资的计算方法是:经理的每月工资固定为8000元;技术人员的月工资=基本工资3000元+100元/小时*工作的小时数,销售员的月工资=基本工资2000元+按当月销售额的4%提成。要求编程计算该公司人员的月工资并显示全部信息。

#include <iostream>using namespace std;#include<string.h>class Employee{protected:int num;string name;float basicSalary;public:Employee(int n,string nam,float b){num=n;name=nam;basicSalary=b;}void show(){cout<<num<<","<<name<<","<<basicSalary<<endl;}};// 在此处补充你的代码int main(){Manager m(1000,"Wang",8000);m.show();Technician t(1001,"Zhang",3000,90);t.show();Salesman s(1002,"Li",2000,90000);s.show();return 0;}

样例输入

样例输出

1000,Wang,80001001,Zhang,3000120001002,Li,20005600

AC代码

#include <iostream>using namespace std;#include<string.h>class Employee{protected:int num;string name;float basicSalary;public:Employee(int n,string nam,float b){num=n;name=nam;basicSalary=b;}void show(){cout<<num<<","<<name<<","<<basicSalary<<endl;}};//答案class Manager: public Employee{public:Manager(int id,char na[],float sal):Employee(id,na,sal){}};class Technician: public Employee{public:float add;Technician(int id,char na[],float sal,int t):Employee(id,na,sal){add=t*100+basicSalary;}void show(){Employee::show();cout<<add<<endl;}};class Salesman: public Employee{public:float add;Salesman(int id,char na[],float sal,float tot):Employee(id,na,sal){add=tot*0.04+basicSalary;}void show(){Employee::show();cout<<add<<endl;}};//答案结束int main(){Manager m(1000,"Wang",8000);m.show();Technician t(1001,"Zhang",3000,90);t.show();Salesman s(1002,"Li",2000,90000);s.show();return 0;}

6:qm类的组合-计算两点距离

已知Point类:两个私有成员,双精度变量X,Y;有参数构造函数需要对X,Y设置默认值0;实现拷贝构造函数;定义三个函数void SetXY(double x,double y)实现设置X、Y数值,double GetX()返回X值,double GetY()返回Y值。

定义Line类:两个私有访问权限子对象Point p1,p2;实现Line类构造函数;实现成员函数double GetLength(),获取Line对象长度,并通过主函数的测试。

#include <iostream>#include <math.h>using namespace std;class Point{public:Point(double x,double y){X = x;Y = y; }Point(const Point& p){X = p.X;X = p.Y;}void setXY(double x,double y){X = x;Y = y;}double getX(){return X;}double getY(){return Y;}private:double X,Y;};class Line{// 在此处补充你的代码};int main(){double x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;Line ln(x1,y1,x2,y2);cout<<ln.getLength()<<endl;return 0;}

样例输入

0 0 6 8

样例输出

10

AC代码

#include <iostream>#include <math.h>using namespace std;class Point{public:Point(double x,double y){X = x;Y = y; }Point(const Point& p){X = p.X;X = p.Y;}void setXY(double x,double y){X = x;Y = y;}double getX(){return X;}double getY(){return Y;}private:double X,Y;};//答案class Line{private:Point p1,p2;//子对象public:Line(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2){}//采用初始化列表进行初始化,注意参数的顺序不要错了!double getLength(){double s1=(p1.getX()-p2.getX())*(p1.getX()-p2.getX());double s2=(p1.getY()-p2.getY())*(p1.getY()-p2.getY());return sqrt(s1+s2);//正常求平面两点间距离即可,sqrt函数用于开根}};//答案结束int main(){double x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;Line ln(x1,y1,x2,y2);cout<<ln.getLength()<<endl;return 0;}

7:qm矩形类的多态表示

设计一个程序满足以下要求:

1) 矩形类(Rectangle),有长(length)和宽(width),有计算其面积的函数Area(),并设其为虚函数。

2) 以Rectangle 类为基类派生一个长方体类(Cuboid ),增加高(height)。有与基类相同的覆盖函数Area(),计算该长方体的表面积。

3) 以Rectangle 类为基类派生一个正方体类(Cube),其中有变量side 可选择以长(length)为边(1)或以宽(width)为边(2)。有与基类相同的覆盖函数Area(),计算该正方体的表面积。

using namespace std;#include<iostream>#include<math.h>// 在此处补充你的代码int main(){int n;cin>>n;double length(0),width(0),height(0),side(0);Rectangle *p[n];for(int i=0;i<n;i++){p[i]=NULL;}int type(0);double s=0;for(int i=0;i<n;i++){cin>>type;if (type==0){cin>>length>>width;p[i]=new Rectangle(length, width);s=s+p[i]->Area();delete p[i];}else if (type==1){cin>>length>>width>>height;p[i]=new Cuboid(length, width,height);s=s+p[i]->Area();delete p[i];}else if (type==2){cin>>length>>width>>side;p[i]=new Cube(length, width,side);s=s+p[i]->Area();delete p[i];}else; }cout<<s;return 0;}

样例输入

30 1.2 3.41 5.6 7.8 1.0 2 9.0 10.0 2

样例输出

718.24

AC代码

using namespace std;#include<iostream>#include<math.h>//答案class Rectangle{public:double length,width;virtual double Area(){//定义虚函数,用于动态联编return length*width;}Rectangle(double l,double w){length=l,width=w;}};class Cuboid: public Rectangle{public:double height;Cuboid(double l,double w,double h):Rectangle(l,w){height=h;}double Area(){//长方体表面积计算return (height*width+height*length+width*length)*2;}};class Cube: public Rectangle{public:int side;Cube(double l,double w,int s):Rectangle(l,w){side=s;}double Area(){if(side==1){//正方体表面积计算return length*length*6;}else return width*width*6;}};//答案结束int main(){int n;cin>>n;double length(0),width(0),height(0),side(0);Rectangle *p[n];for(int i=0;i<n;i++){p[i]=NULL;}int type(0);double s=0;for(int i=0;i<n;i++){cin>>type;if (type==0){cin>>length>>width;p[i]=new Rectangle(length, width);s=s+p[i]->Area();delete p[i];}else if (type==1){cin>>length>>width>>height;p[i]=new Cuboid(length, width,height);s=s+p[i]->Area();delete p[i];}else if (type==2){cin>>length>>width>>side;p[i]=new Cube(length, width,side);s=s+p[i]->Area();delete p[i];}else; }cout<<s;return 0;}

8:qm运算符重载-RMB类

定义一个RMB类,一个RMB对象的数据成员包含元(yuan)和角分(jf),实现RMB的累加

#include<iostream>using namespace std;class RMB{public:RMB(unsigned int d, unsigned int c);RMB operator+(RMB&);void display(){cout<<(yuan + jf / 100.0)<<endl; }protected:unsigned int yuan;unsigned int jf;};RMB::RMB(unsigned int d, unsigned int c){yuan = d + c/100;jf = c % 100;}// 在此处补充你的代码int main(){RMB d1(1, 50);RMB d2(2, 80);RMB d3(0, 0);d3 = d1 + d2;d3.display();}

样例输入

样例输出

4.3

AC代码

#include<iostream>using namespace std;class RMB{public:RMB(unsigned int d, unsigned int c);RMB operator+(RMB&);//成员函数形式的运算符重载void display(){cout<<(yuan + jf / 100.0)<<endl; }protected:unsigned int yuan;unsigned int jf;};RMB::RMB(unsigned int d, unsigned int c){yuan = d + c/100;jf = c % 100;}RMB RMB ::operator+(RMB &r) {//声明时省略了引用参数名,这里可以任意取个名字jf=(jf+r.jf)%100;//注意jf不会超过100,所以需要考虑进位的问题yuan=yuan+r.yuan+(jf+r.jf)/100;return *this;}int main(){RMB d1(1, 50);RMB d2(2, 80);RMB d3(0, 0);d3 = d1 + d2;d3.display();}

这套模拟题总的来说,难度适中,比较考察思维,尤其要考察的,是按照已经给出的功能和已有部分程序的格式,将程序补充完整。

基础不太好的同学可以参考如下解题建议:

别着急做,先把每个题的题意读懂,然后有选择性的做;学会关注排名,优先选择通过率高/通过人数多的题目做;尽量别尝试走捷径,能用笨办法就用笨办法,太花哨反而容易翻车;

祝大家顺利!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。