C++期末试题1

程序填空

1

/*
请将如下程序补充完整,使得程序运行时的输出结果为:
默认构造:4
构造:2
复制:4
析构
析构
析构

注意:仅允许在指定的下划线处填写内容,并删除下划线编号,不允许增加或删除语句,也不得改动程序中的其他部分。
试题源程序如下:
*/
#include <iostream>
using namespace std;
class MyClass {
	public:

/**********FILL**********/
		MyClass() { cout<<"默认构造:"<<number<<endl; }
		MyClass(int n) { 
			number=n;  
			cout<<"构造:"<<number<<endl; 
		}  
		MyClass(const MyClass &other) { 

/**********FILL**********/
			number=other.number ; 
			cout<<"复制:"<<number<<endl;
		}

/**********FILL**********/
	~MyClass() { cout<<"析构"<<endl; }
	private:

/**********FILL**********/
		int number=4 ; 
};
void fun(MyClass &p) {  
	MyClass temp(p);
}
int main() {
	MyClass a, b(2);
	fun(a);
	return 0;
}

2

/*
请将如下程序补充完整,使得程序运行时的输出结果为:
构造:2
复制:2
复制:2
析构
析构
析构

注意:仅允许在指定的下划线处填写内容,并删除下划线编号,不允许增加或删除语句,也不得改动程序中的其他部分。
试题源程序如下:
*/
#include <iostream>
using namespace std;
class MyClass {
	public:

/**********FILL**********/
		MyClass(int n) {
			number=n;  
			cout<<"构造:"<<number<<endl; 
		}  

/**********FILL**********/
		MyClass(const MyClass &other) { 
			number=other.number;
			cout<<"复制:"<<number<<endl;
		}

/**********FILL**********/
		~MyClass() { cout<<"析构"<<endl; }
	private:
		int number;
};

/**********FILL**********/
void fun(MyClass p) {
	MyClass temp(p);
	return;
}
int main() {
	MyClass a(2);  
	fun(a);  
	return 0;
}

程序改错

1

/*
下面的程序实现复数相加运算,请改正程序中指定位置的错误,使程序的输出结果如下: 
c3=(0, 0i)
c3=c1+c2=(4.7, 1.4i)
注意:只允许修改注释“ERROR”的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序清单:
*/
#include <iostream>
using namespace std;

/**********ERROR**********/
class Complex
{
	public:

/**********ERROR**********/
		Complex(double r=0, double i=0 )
		{ real=r; imag=i; }
		Complex add(Complex &);
		void display() {
			cout<<"("<<real<<", "<<imag<<"i)"<<endl;
		}
	private: 
		double real, imag;
};

/**********ERROR**********/
Complex Complex::add(Complex &c)
{	
	return Complex(real+c.real,imag+c.imag);
}
int main() {
	Complex c1(1.2,2.4),c2(3.5,-1),c3;
	cout<<"c3="; c3.display();

/**********ERROR**********/
	c3=c1.add(c2);
	cout<<"c3=c1+c2="; c3.display();
	return 0;
}

2

/*
请将如下程序补充完整,使得程序运行时的输出结果为:
默认构造:6
构造:2
复制:2
调用fun函数
析构
析构
析构
注意:只允许修改注释“ERROR”的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序清单:
*/
#include <iostream>
using namespace std;
class MyClass {
	public:

/**********ERROR**********/
		MyClass()
		{ cout<<"默认构造:"<<number<<endl; }
		MyClass(int n) {
			number=n;
			cout<<"构造:"<<number<<endl; 
		}  

/**********ERROR**********/
		MyClass(const MyClass &other) { 
			number=other.number;
			cout<<"复制:"<<number<<endl;
		}

/**********ERROR**********/
		~MyClass()
		{ cout<<"析构"<<endl; }
	private:
		int number=6;
};

/**********ERROR**********/
void fun(MyClass &p) {
	MyClass temp(p);
	cout<<"调用fun函数"<<endl;
}
int main() {
	MyClass a, b(2);
	fun(b);
	return 0;
}

程序设计

1

/*
正方形(Square)类成员如下: 
(1)公有成员:
Square(float xx, float yy, float len) // 构造函数,其中(xx,yy)为左下角位置,len为边长
void resetSquare(float newX, float newY, float newLen)  // 重置正方形左下角坐标,以及边长newLen
float getLen()  // 返回正方形的边长
double getArea() // 计算并返回正方形的面积
bool isEqual(Square &s)  // 判断与另一个正方形是否大小相等
(2)私有成员:
float x, y   // 正方形左下角的横坐标、纵坐标
float length  // 正方形的边长
请根据上述说明,完成Square类的定义。

注意:部分源程序给出,仅允许在注释“Begin”和“End”之间填写内容,不得改动main函数和其他已有的任何内容。
试题程序:
*/
#include<iostream>
#include<fstream>
using namespace std;

/*******Begin*******/

class Square {
	public:
		Square(float xx, float yy, float len):x(xx),y(yy),length(len){}; // 构造函数,其中(xx,yy)为左下角位置,len为边长
		void resetSquare(float newX, float newY, float newLen){
			x=newX;y=newY;length=newLen;
		}  // 重置正方形左下角坐标,以及边长newLen
		float getLen(){
			return length;
		}  // 返回正方形的边长
		double getArea() {
			return length*length;
		}// 计算并返回正方形的面积
		bool isEqual(Square &s) {
			if(length==s.getLen())return true;
			else return false;
		} // 判断与另一个正方形是否大小相等
		
	private:
		float x, y ;  // 正方形左下角的横坐标、纵坐标
		float length ; // 正方形的边长
}; 



/*******End*********/

int main() {
	float x,y,len;
	cin>>x>>y>>len;
	Square s1(x,y,len),s2(s1);
	cout<<"s1 边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
	cout<<"s2 边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
	cout<<"是否相等: "<<s2.isEqual(s1)<<endl;
	cin>>x>>y>>len;
	s2.resetSquare(x,y,len);
	cout<<"s1 边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
	cout<<"s2 边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
	cout<<"是否相等: "<<s2.isEqual(s1)<<endl;
	
	ifstream in1("4.1.1.4_1-2_in.dat");
	ofstream out1("4.1.1.4_1-2_out.dat");
	while(in1>>x>>y>>len)
	{
		Square s1(x,y,len),s2(s1);
		out1<<"s1 边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
		out1<<"s2 边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
		out1<<"是否相等: "<<s2.isEqual(s1)<<endl;
		in1>>x>>y>>len;
		s2.resetSquare(x,y,len);
		out1<<"s1 边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
		out1<<"s2 边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
		out1<<"是否相等: "<<s2.isEqual(s1)<<endl<<endl;
	}
	in1.close();
	out1.close();
	return 0;
}

2

/*
时钟(Clock)类用于模拟时钟的常用功能,其成员如下: 
(1)公有成员:
Clock(int hh=0,int mm=0,int ss=0) // 构造函数,hh,mm,ss分别用于初始化时钟的时、分、秒
void setTime(int newH,int newM,int newS)  // 设置时钟的时、分、秒数据
void add(int n)  // 时钟秒数加n
void dec()  // 时钟秒数减1
int dist(Clock &c)  // 与另一个时钟对象的时间差值,单位为秒
void showTime()  // 显示(输出)当前时钟数据
(2)私有成员:
int hour, minute, second  // 时钟的时、分、秒(24小时制)

请根据上述说明,完成Clock类的定义。
测试样例:
输入:
0 0 0
3675
13 10 30
输出:
时钟c1: 0:0:0
时钟c2: 0:0:0
时钟c1与c2相差0秒
时钟c1: 1:1:15
时钟c2: 23:59:59
时钟c1与c2相差82724秒
重置后时钟c1: 13:10:30
注意:部分源程序已给出,仅允许在注释“Begin”和“End”之间补全代码,不得改动其他已有的任何内容。
试题程序:
*/
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;

/*******Begin*******/
class Clock {
	public:
		Clock(int hh=0,int mm=0,int ss=0):hour(hh),minute(mm),second(ss){}; // 构造函数,hh,mm,ss分别用于初始化时钟的时、分、秒
		void setTime(int newH,int newM,int newS) {
			hour=newH;minute=newM;second=newS;
		} // 设置时钟的时、分、秒数据
		void add(int n);// 时钟秒数加n
		void dec();// 时钟秒数减1
		int dist(Clock &c); // 与另一个时钟对象的时间差值,单位为秒
		void showTime(); // 显示(输出)当前时钟数据
		
	private:
		int hour, minute, second ;
}; 

void Clock::add(int n) {
	second+=n;
	minute+=second/60;
	second%=60;
	hour+=minute/60;
	minute%=60;
	hour%24;
}

void Clock::dec() {
	second--;
	if(second<0){
		second+=60;
		minute--;
		if(minute<0){
			minute+=60;
			hour=(hour+23)%24;
		}
	}
}

int Clock::dist(Clock &c) {
	return abs((hour*3600+minute*60+second)-(c.hour*3600+c.minute*60+c.second));
}


/*******End*********/

void Clock::showTime() {
	cout<<hour<<":"<<minute<<":"<<second<<endl;
}

int main() {
	int h,m,s,n;
	cin>>h>>m>>s;
	Clock c1(h,m,s),c2;
	cout<<"时钟c1: "; c1.showTime();
	cout<<"时钟c2: "; c2.showTime();
	cout<<"时钟c1与c2相差"<<c1.dist(c2)<<"秒"<<endl;
	cin>>n;
	c1.add(n); c2.dec();
	cout<<"时钟c1: "; c1.showTime();
	cout<<"时钟c2: "; c2.showTime();
	cout<<"时钟c1与c2相差"<<c1.dist(c2)<<"秒"<<endl;
	cin>>h>>m>>s;
	c1.setTime(h,m,s);
	cout<<"重置后时钟c1: "; c1.showTime();	
	ifstream in1("4.1.5_1-5.in");
	ofstream out1("4.1.5_1-5.out");
	streambuf *cinbackup;
	streambuf *coutbackup;
	cinbackup=cin.rdbuf(in1.rdbuf());
	coutbackup=cout.rdbuf(out1.rdbuf());
	while(cin>>h>>m>>s) {
		Clock c1(h,m,s),c2;
		cout<<"时钟c1: "; c1.showTime();
		cout<<"时钟c2: "; c2.showTime();
		cout<<"时钟c1与c2相差"<<c1.dist(c2)<<"秒"<<endl;
		cin>>n;
		c1.add(n); c2.dec();
		cout<<"时钟c1: "; c1.showTime();
		cout<<"时钟c2: "; c2.showTime();
		cout<<"时钟c1与c2相差"<<c1.dist(c2)<<"秒"<<endl;
		cin>>h>>m>>s;
		c1.setTime(h,m,s);
		cout<<"重置后时钟c1: "; c1.showTime();
		cout<<endl;
	}
	cin.rdbuf(cinbackup);
	cout.rdbuf(coutbackup);
	in1.close();
	out1.close();
	return 0;
}

3

/*
点(Point)类成员如下: 
(1)公有成员:
Point(float xx, float yy)   // 构造函数,初始化点的x, y坐标
void moveTo(float newX, float newY)  // 将点的x, y坐标移动到newX, newY
(2)私有成员:
float x, y   // 点的横坐标,纵坐标
在此基础上,定义圆(Circle)类,其成员如下:
(1)公有成员:
Circle(float x=0.0,float y=0.0,float r=1.0) // 构造函数,其中(x,y)为圆心位置,r为圆的半径,π=3.14
void resetCircle(float newX, float newY, float newR) // 重置圆心坐标x, y,以及半径radius
double getRadius()  // 返回圆的半径
double getCircumference() // 返回圆的周长
bool isEqual(Circle &c) // 判断与另一个圆是否大小相等(半径是否相等)
(2)私有成员:
Point p   // 圆心位置
float radius  // 圆的半径
double circumference // 圆的周长
请根据上述说明,完成Point,Circle两个类的定义。
注意:部分源程序给出,仅允许在注释"Begin"和"End"之间填写内容,不得改动main函数和其他已有的任何内容。
测试样例:
输入:
0 0 1
3 4 5
输出:
c1 半径: 1, 周长: 6.28
c2 半径: 1, 周长: 6.28
是否相等: 1
重置后:
c1 半径: 1, 周长: 6.28
c2 半径: 5, 周长: 31.4
是否相等: 0
试题程序:
*/
#include<iostream>
#include<fstream>
using namespace std;
const double PI=3.14;

/*******Begin*******/
class Point{
	public:
		Point(float xx, float yy):x(xx),y(yy){}  // 构造函数,初始化点的x, y坐标
		void moveTo(float newX, float newY) {
			x=newX;y=newY;
		} // 将点的x, y坐标移动到newX, newY
		
	private:
		float x, y ;  // 点的横坐标,纵坐标
};

class Circle{
	public:
		Circle(float x=0.0,float y=0.0,float r=1.0):p(x,y){
			radius=r;
			circumference=2*PI*radius;
		}// 构造函数,其中(x,y)为圆心位置,r为圆的半径,π=3.14
		void resetCircle(float newX, float newY, float newR) {
			p.moveTo(newX,newY);
			radius=newR;
			circumference=2*PI*radius;
		}// 重置圆心坐标x, y,以及半径radius
		double getRadius() {
			return radius;
		} // 返回圆的半径
		double getCircumference(){
			return circumference;
		} // 返回圆的周长
		bool isEqual(Circle &c) {
			if(radius==c.radius) return true;
			return false;	
			
		}// 判断与另一个圆是否大小相等(半径是否相等)
		
	private:
		Point p ;  // 圆心位置
		float radius ; // 圆的半径
		double circumference ;// 圆的周长
};

/*******End*********/

int main() {
	float x,y,r;
	cin>>x>>y>>r;
	Circle c1(x,y,r),c2;
	cout<<"c1 半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<endl;
	cout<<"c2 半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<endl;
	cout<<"是否相等: "<<c2.isEqual(c1)<<endl;	
	cin>>x>>y>>r;
	c2.resetCircle(x,y,r);
	cout<<"重置后:\nc1 半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<endl;
	cout<<"c2 半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<endl;
	cout<<"是否相等: "<<c2.isEqual(c1)<<endl;
	ifstream in1("4.2.2_2-2.in");
	ofstream out1("4.2.2_2-2.out");
	while(in1>>x>>y>>r) {
		Circle c1(x,y,r),c2;
		out1<<"c1 半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<endl;
		out1<<"c2 半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<endl;
		out1<<"是否相等: "<<c2.isEqual(c1)<<endl;		
		in1>>x>>y>>r;
		c2.resetCircle(x,y,r);
		out1<<"重置后:\nc1 半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<endl;
		out1<<"c2 半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<endl;
		out1<<"是否相等: "<<c2.isEqual(c1)<<endl<<endl;
	}
	return 0;
}

4

/*
点(Point)类成员如下: 
(1)公有成员:
Point(float xx, float yy) // 构造函数,初始化点的x, y坐标
void moveTo(float newX, float newY) // 将点的x, y坐标移动到newX, newY
(2)私有成员:
float x, y   // 点的横坐标x,纵坐标y
在此基础上,定义矩形(Rectangle)类,其成员如下:
(1)公有成员:
	Rectangle(float xx=0.0,float yy=0.0,float ww=1.0,float hh=1.0) // 构造函数,初始化矩形左下角坐标x, y,以及宽w和高h
void resetRect(float newX, float newY, float newW, float newH) // 重置矩形左下角坐标为(newX,newY),以及宽w和高h
double getArea() // 计算并返回矩形的面积
double getCircumference() // 计算并返回矩形的周长
bool isSquare() // 判断是否为正方形
(2)私有成员:
Point p   // 矩形左下角位置
float w, h  // 矩形的宽和高 
请根据上述说明,完成Point,Rectangle两个类的定义。

注意:部分源程序给出,仅允许在注释“Begin”和“End”之间填写内容,不得改动main函数和其他已有的任何内容。
试题程序:
*/
#include<iostream>
#include<fstream>
using namespace std;

/*******Begin*******/
class Point {
	public:
		Point(float xx, float yy):x(xx),y(yy){} // 构造函数,初始化点的x, y坐标
		void moveTo(float newX, float newY){
			x=newX;y=newY;
		} // 将点的x, y坐标移动到newX, newY
	private:
		float x, y;   // 点的横坐标x,纵坐标y
};

class Rectangle {
	public:
		Rectangle(float xx=0.0,float yy=0.0,float ww=1.0,float hh=1.0):p(xx,yy),w(ww),h(hh){} // 构造函数,初始化矩形左下角坐标x, y,以及宽w和高h
		void resetRect(float newX, float newY, float newW, float newH) {
			p.moveTo(newX,newY);
			w=newW;h=newH;
		}// 重置矩形左下角坐标为(newX,newY),以及宽w和高h
		double getArea() {
			return w*h;
		}// 计算并返回矩形的面积
		double getCircumference(){
			return 2*w+2*h;
		} // 计算并返回矩形的周长
		bool isSquare(){
			if(w==h)return true;
			return false;
		} // 判断是否为正方形
		
	private:
		Point p;   // 矩形左下角位置
		float w, h;  // 矩形的宽和高 
};


/*******End*********/

int main() {
	float x,y,w,h;
	cin>>x>>y>>w>>h;
	Rectangle rect1(x,y,w,h),rect2;
	cout<<"rect1 面积: "<<rect1.getArea()<<" 周长: "<<rect1.getCircumference()<<endl;
	cout<<"是否正方形: "<<rect1.isSquare()<<endl;
	cout<<"rect2 面积: "<<rect2.getArea()<<" 周长: "<<rect2.getCircumference()<<endl;
	cout<<"是否正方形: "<<rect2.isSquare()<<endl;	
	cin>>x>>y>>w>>h;
	rect2.resetRect(x,y,w,h);
	cout<<"重置后:\nrect2 面积: "<<rect2.getArea()<<" 周长: "<<rect2.getCircumference()<<endl;
	cout<<"是否正方形: "<<rect2.isSquare()<<endl;

	ifstream in1("4.2.1_1-2_in.dat");
	ofstream out1("4.2.1_1-2_out.dat");
	while(in1>>x>>y>>w>>h) {
		Rectangle rect1(x,y,w,h),rect2;
		out1<<"rect1 面积: "<<rect1.getArea()<<" 周长: "<<rect1.getCircumference()<<endl;
		out1<<"是否正方形: "<<rect1.isSquare()<<endl;
		out1<<"rect2 面积: "<<rect2.getArea()<<" 周长: "<<rect2.getCircumference()<<endl;
		out1<<"是否正方形: "<<rect2.isSquare()<<endl;		
		in1>>x>>y>>w>>h;
		rect2.resetRect(x,y,w,h);
		out1<<"重置后:\nrect2 面积: "<<rect2.getArea()<<" 周长: "<<rect2.getCircumference()<<endl;
		out1<<"是否正方形: "<<rect2.isSquare()<<endl<<endl;
	}
	in1.close();
	out1.close();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值