C++期末试题2

程序填空

1

/*
请将如下程序补充完整,使得该程序运行时输出如下结果:
10
11
12
注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线编号)。
试题源程序如下:
*/
#include <iostream>
using namespace std;
class myClass {
		static int a;  
	public:
		myClass() { a++; }
		~myClass() { a--; }

/**********FILL**********/
		static int getA(){
			return a;    
		}
};

/**********FILL**********/
int myClass::a =10;
int main() {

/**********FILL**********/
	cout<< myClass::getA <<endl;
	myClass a;
	cout<<a.getA()<<endl;
	myClass b;
	cout<<b.getA()<<endl;
	return 0;
}

2

/*
请将如下程序补充完整,使得程序运行时的输出结果为:
x=0, y=1
x=1, y=3
x=3; y=7

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

/**********FILL**********/
		const int  x =0 ;

/**********FILL**********/
		 static int y   ; 
	public: 
		Test() { y+=1; }
		Test(int i,int j):x(i) { y+=j; } 
		void display() const;  

/**********FILL**********/
		void display()  { cout<<"x="<<x<<", y="<<y<<endl; } 
}; 
int Test::y=0;  

/**********FILL**********/
 void Test::display() const {
	cout<<"x="<<x<<"; y="<<y<<endl;
} 
int main() {  
	Test t1; t1.display(); 
	Test t2(1,2); t2.display();
	const Test t3(3,4);  
	t3.display(); 
	return 0; 
}

改错

1

/*
请改正程序中指定位置的错误,使程序的输出结果如下: 
0,2,1
1,4,2
注意:只允许修改注释"ERROR"的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序清单:
*/
#include <iostream>
using namespace std;
class myClass {
  public:
		myClass(int i)

/**********ERROR**********/
		:a(i){
		}
		void print()
		{ cout<<a<<','<<b<<endl; }

/**********ERROR**********/
		 void static showB()  
		{ cout<<b<<','; }
  private:
		const int a;
		static int b;
};

/**********ERROR**********/
int myClass::b=0;
int main() {

/**********ERROR**********/
  myClass::showB();
  myClass a1(2); a1.print(); a1.showB();
  myClass a2(4); a2.print();
  return 0;
}

程序设计

1

/*
矩形(Rectangle)类的成员如下: 
(1)公有成员:
Rectangle(float xx1=0.0,float yy1=0.0,float xx2=1.0,float yy2=4.0) // 构造函数,初始化矩形各数据成员,其中(xx1, yy1)为左下角坐标,(xx2, yy2)为右上角坐标
void moveTo(float newX, float newY)  // 平移操作,将左下角平移到(newX, newY)
double getCircumference () // 返回矩形的周长
bool isSquare()  // 判断是否为正方形
bool isEqual(Rectangle &r) // 判断与另一个矩形的周长是否相等
(2)私有成员:
float x1, y1, x2, y2  // 矩形左下角坐标(x1, y1)和右上角坐标(x2, y2)
double circumference  // 矩形的周长
请根据上述说明,完成Rectangle类的定义。

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

/*******Begin*******/
class Rectangle {
	public :
		Rectangle(float xx1=0.0,float yy1=0.0,float xx2=1.0,float yy2=4.0):x1(xx1),y1(yy1),x2(xx2),y2(yy2){
			circumference=2*(x2-x1+y2-y1);
		} // 构造函数,初始化矩形各数据成员,其中(xx1, yy1)为左下角坐标,(xx2, yy2)为右上角坐标
		void moveTo(float newX, float newY) {
			float x_offset=newX-x1,y_offset=newY-y1;
            x1=newX;y1=newY;
            x2+=x_offset,y2+=y_offset;
		} // 平移操作,将左下角平移到(newX, newY)
		double getCircumference (){
			return circumference;
		} // 返回矩形的周长
		bool isSquare() {
			if(x2-x1==y2-y1)return true;
			return false; 
		} // 判断是否为正方形
		bool isEqual(Rectangle &r) {
			if(r.getCircumference()==this->circumference) return true;
			else return false;
		}// 判断与另一个矩形的周长是否相等
		
	private:
		float x1, y1, x2, y2 ; // 矩形左下角坐标(x1, y1)和右上角坐标(x2, y2)
		double circumference ; // 矩形的周长	
};




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

int main() {
	float x1,y1,x2,y2;
	cin>>x1>>y1>>x2>>y2;
	Rectangle r1(x1,y1,x2,y2),r2(r1);
	cout<<"矩形r1: 周长"<<r1.getCircumference()<<", 是否正方形: "<<r1.isSquare()<<endl;	
	cout<<"矩形r2: 周长"<<r2.getCircumference()<<", 是否正方形: "<<r2.isSquare()<<endl;
	cout<<"周长是否相等: "<<r2.isEqual(r1)<<endl;
	cin>>x1>>y1;
	r2.moveTo(x1,y1);
	cout<<"平移后:\n矩形r1: 周长"<<r1.getCircumference()<<", 是否正方形: "<<r1.isSquare()<<endl;	
	cout<<"矩形r2: 周长"<<r2.getCircumference()<<", 是否正方形: "<<r2.isSquare()<<endl;
	cout<<"周长是否相等: "<<r2.isEqual(r1)<<endl;
	
	ifstream in1("4.1.1.3_4-2_in.dat");
	ofstream out1("4.1.1.3_4-2_out.dat");
	while(in1>>x1>>y1>>x2>>y2)
	{
		Rectangle r1(x1,y1,x2,y2),r2(r1);
		out1<<"矩形r1: 周长"<<r1.getCircumference()<<", 是否正方形: "<<r1.isSquare()<<endl;	
		out1<<"矩形r2: 周长"<<r2.getCircumference()<<", 是否正方形: "<<r2.isSquare()<<endl;
		out1<<"周长是否相等: "<<r2.isEqual(r1)<<endl;
		in1>>x1>>y1;
		r2.moveTo(x1,y1);
		out1<<"平移后:\n矩形r1: 周长"<<r1.getCircumference()<<", 是否正方形: "<<r1.isSquare()<<endl;	
		out1<<"矩形r2: 周长"<<r2.getCircumference()<<", 是否正方形: "<<r2.isSquare()<<endl;
		out1<<"周长是否相等: "<<r2.isEqual(r1)<<endl<<endl;		
	}
	in1.close();
	out1.close();
	return 0;
}

2

点(Point)类成员如下: 
(1)公有成员:
Point(float xx, float yy)   // 构造函数,初始化点的x, y坐标
void moveTo(float newX, float newY)  // 将点的x, y坐标移动到newX, newY2)私有成员:
float x, y   // 点的横坐标,纵坐标
在此基础上,定义正方形(Square)类,其成员如下:
(1)公有成员:
Square(float x=0.0,float y=0.0,float len=1.0)  // 构造函数,初始化所有数据成员,其中(x,y)为左下角位置,len为边长
void resetSquare(float newX, float newY, float newLen) // 重置正方形左下角坐标为(newX, newY),边长为newLen
double getLen() // 返回正方形的边长
double getCircumference() // 返回正方形的周长
bool isEqual(Square &s) // 判断与另一个正方形是否大小相等2)私有成员:
Point p   // 正方形左下角位置
float length  // 正方形的边长
double circumference  // 正方形的面积,周长
请根据上述说明,完成Point,Square两个类的定义。

注意:部分源程序给出,仅允许在注释“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 ;  // 点的横坐标,纵坐标
};

class Square{
	public:
		Square(float x=0.0,float y=0.0,float len=1.0):p(x,y){
			
			length=len;
			circumference=4*length;
			
		}  // 构造函数,初始化所有数据成员,其中(x,y)为左下角位置,len为边长
		void resetSquare(float newX, float newY, float newLen){
			p.moveTo(newX,newY);
			length=newLen;
			circumference=4*length;
		} // 重置正方形左下角坐标为(newX, newY),边长为newLen
		double getLen(){
			return length;
		} // 返回正方形的边长
		double getCircumference(){
			return circumference;
		} // 返回正方形的周长
		bool isEqual(Square &s){
			if(s.length==this->length) return true;
			else return false;
		} // 判断与另一个正方形是否大小相等
	private:
		Point p ;  // 正方形左下角位置
		float length ; // 正方形的边长
		double circumference;  // 正方形的面积,周长
};


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

int main() {
	float x,y,len;
	cin>>x>>y>>len;
	Square s1(x,y,len),s2;
	cout<<"s1 边长: "<<s1.getLen()<<", 周长: "<<s1.getCircumference()<<endl;
	cout<<"s2 边长: "<<s2.getLen()<<", 周长: "<<s2.getCircumference()<<endl;
	cout<<"是否相等: "<<s2.isEqual(s1)<<endl;
		
	cin>>x>>y>>len;
	s2.resetSquare(x,y,len);
	cout<<"重置后:\ns1 边长: "<<s1.getLen()<<", 周长: "<<s1.getCircumference()<<endl;
	cout<<"s2 边长: "<<s2.getLen()<<", 周长: "<<s2.getCircumference()<<endl;
	cout<<"是否相等: "<<s2.isEqual(s1)<<endl;
	ifstream in1("4.2.3_2-2_in.dat");
	ofstream out1("4.2.3_2-2_out.dat");
	while(in1>>x>>y>>len)
	{
		Square s1(x,y,len),s2;
		out1<<"s1 边长: "<<s1.getLen()<<", 周长: "<<s1.getCircumference()<<endl;
		out1<<"s2 边长: "<<s2.getLen()<<", 周长: "<<s2.getCircumference()<<endl;
		out1<<"是否相等: "<<s2.isEqual(s1)<<endl;	
		in1>>x>>y>>len;
		s2.resetSquare(x,y,len);
		out1<<"重置后:\ns1 边长: "<<s1.getLen()<<", 周长: "<<s1.getCircumference()<<endl;
		out1<<"s2 边长: "<<s2.getLen()<<", 周长: "<<s2.getCircumference()<<endl;
		out1<<"是否相等: "<<s2.isEqual(s1)<<endl<<endl;
	}
	in1.close();
	out1.close();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值