C++期末试题6

程序设计

1

/*
课程(Course)类主要用于处理某门课程的成绩,主要成员如下:
(1)私有成员:
int number    // 课程编号 
string name   // 课程名称 	
int credit     // 课程学分
int n;        // 选修课程的学生数量
float *score;   // 指向保存学生成绩的数组 
int max,min   // 课程的最高分、最低分
float average  // 课程的平均分
int count     // 不及格(<60)的学生数量
(2)公有成员:
Course(int n);   // 构造函数,动态创建一个长度为n的数组,返回值赋给score,并将credit, count, average初始化为0,n为学生数量 
~Course();     // 析构函数,删除成绩数组 
void input()    // 依次输入课程编号、课程名称、学分以及n名学生的成绩 
void process()	  // 计算课程的最高分、最低分、平均分,以及不及格的学生数量
void print()    // 输出课程编号、课程名称、最高分、最低分、平均分以及不及格学生数
请根据上述说明,完成Course类的定义。

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

class Course {
		int number;    // 课程编号 
		string name;   // 课程名称 	
		int credit;    // 课程学分
		int n;         // 选修课程的学生数量
		float *score;  // 指向保存学生成绩的数组 
		int max,min;   // 课程的最高分、最低分
		float average; // 课程的平均分
		int count;     // 不及格(<60)的学生数量
	public:
		Course(int n);   // 构造函数,动态创建一个长度为n的数组,返回值赋给score,并将credit, count, average初始化为0,n为学生数量 
		~Course();       // 析构函数,删除成绩数组
		void input();    // 依次输入课程编号、课程名称、课程学分以及n名学生的成绩 
		void process();  // 计算课程的最高分、最低分、平均分,以及不及格的学生数量
		void print() {   // 输出课程编号、名称、学分、最高分、最低分、平均分,以及不及格学生数
			cout<<"课程编号: "<<number<<endl;
			cout<<"课程名称: "<<name<<" 课程学分: "<<credit<<endl;
			cout<<"最高分: "<<max<<" 最低分: "<<min<<" 平均分: "<<average<<" 不及格数: "<<count<<endl<<endl;
		}  
};  

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

Course::Course(int n){
	this->n=n;
	score=new float[n];
	credit=0;count=0;average=0;
} 

Course::~Course(){
	delete[] score;
}

void Course::input(){
	cin>>number>>name>>credit;
	for(int i=0;i<n;i++) cin>>score[i];
}

void Course::process(){
	max=score[0];min=score[0];
	for(int i=0;i<n;i++){
		if(max<score[i])max=score[i];
		if(min>score[i])min=score[i];
		average+=score[i];
		if(score[i]<60)count++;
	}
	average/=n;
}


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

int main() {
	int n;
	cin>>n;
	Course c(n);
	c.input();c.process();c.print();

	ifstream in1("6.6.2_2_in.dat");
	ofstream out1("6.6.2_2_out.dat");
	streambuf *cinbackup;
	streambuf *coutbackup;
	cinbackup=cin.rdbuf(in1.rdbuf());
	coutbackup=cout.rdbuf(out1.rdbuf());	
	while(cin>>n) {
		Course c(n);
		c.input();c.process();c.print();
	}	
	cin.rdbuf(cinbackup);
	cout.rdbuf(coutbackup);
	in1.close();
	out1.close();
	return 0;
}

2

/*
点(Point)类成员如下: 
(1)公有成员:
Point(float xx, float yy)  // 构造函数,初始化点的x, y坐标
float getX() const  // 返回横坐标x
float getY() const  // 返回纵坐标y
void setX(float newX)  // 重设横坐标为newX
void setY(float newY)  // 重设纵坐标为newY
(2)私有成员:
float x, y  // 点的横坐标,纵坐标
由Point类公有派生出圆(Circle)类,基类Point的x, y成员作为圆心的坐标,并新增如下成员:
(1)公有成员:
Circle(float x=0.0, float y=0.0, float r=1.0)  // 构造函数,其中(x,y)为圆心位置,r为圆的半径
void moveTo(float newX, float newY)  // 平移操作,将圆心移动到newX, newY
float getRadius() const  // 返回圆的半径
double getCircumference() const  // 计算并返回圆的周长,π=3.14159
double getArea() const  // 计算并返回圆的面积,π=3.14159
double dist(const Circle &c) const  // 计算并返回到另一个圆的距离(圆心之间的距离)
bool isEqual(const Circle &c) const  // 判断与另一个圆是否大小相等
(2)私有成员:
float radius  // 圆的半径 

请根据上述说明,完成Point,Circle两个类的定义。
注意:部分源程序已给出,仅允许在注释“Begin”和“End”之间填写内容,不得改动其他已有的任何内容。

测试样例:
输入:
0 0 1
3 4
输出:
初始: 
c1:圆心: (0, 0), 半径: 1, 周长: 6.28318, 面积: 3.14159
c2:圆心: (0, 0), 半径: 1, 周长: 6.28318, 面积: 3.14159
c1与c2圆心之间距离:0
c1与c2大小是否相等:1

平移后: 
c2:圆心: (3, 4), 半径: 1, 周长: 6.28318, 面积: 3.14159
c1与c2圆心之间距离:5
c1与c2大小是否相等:1

试题程序:
*/
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
const double PI=3.14159;

/*******Begin*******/
class Point{
	public:
		Point(float xx, float yy){
			x=xx;y=yy;
		}  // 构造函数,初始化点的x, y坐标
		float getX() const {
			return this->x;
		} // 返回横坐标x
		float getY() const{
			return this->y;
		}  // 返回纵坐标y
		void setX(float newX){
			this->x=newX;
		}  // 重设横坐标为newX
		void setY(float newY){
			this->y=newY;
		}  // 重设纵坐标为newY
		
	private:
		float x, y ; // 点的横坐标,纵坐标
};

class Circle:public Point{
	public:
		Circle(float x=0.0, float y=0.0, float r=1.0):Point(x,y){
			this->radius=r;
		}  // 构造函数,其中(x,y)为圆心位置,r为圆的半径
		void moveTo(float newX, float newY){
			setX(newX);setY(newY);
		}  // 平移操作,将圆心移动到newX, newY
		float getRadius() const{
			return this->radius;
		}  // 返回圆的半径
		double getCircumference() const {
			return 2*PI*radius;
		} // 计算并返回圆的周长,π=3.14159
		double getArea() const {
			return PI*radius*radius;
		} // 计算并返回圆的面积,π=3.14159
		double dist(const Circle &c) const {
			double x=this->getX()-c.getX();
			double y=this->getY()-c.getY();
			return sqrt(x*x+y*y);
		} // 计算并返回到另一个圆的距离(圆心之间的距离)
		bool isEqual(const Circle &c) const {
			if(c.getRadius()==this->getRadius())return true;
			return false;
		} // 判断与另一个圆是否大小相等
		
	private:
		float radius;  // 圆的半径 
};



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

int main() {
	float x,y,r;
	cin>>x>>y>>r;
	Circle c1(x,y,r),c2;
	cout<<"初始: "<<endl;
	cout<<"c1:圆心: ("<<c1.getX()<<", "<<c1.getY()<<"), "<<"半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<", 面积: "<<c1.getArea()<<endl;
	cout<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
	cout<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;	
	cout<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
	
	cin>>x>>y;
	c2.moveTo(x,y);
	cout<<"平移后: "<<endl;
	cout<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
	cout<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;	
	cout<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;

	ifstream in1("7.1.2_1-s2_in.dat");
	ofstream out1("7.1.2_1-s2_out.dat");
	while(in1>>x>>y>>r) {
		Circle c1(x,y,r),c2;
		out1<<"初始: "<<endl;
		out1<<"c1:圆心: ("<<c1.getX()<<", "<<c1.getY()<<"), "<<"半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<", 面积: "<<c1.getArea()<<endl;
		out1<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
		out1<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;	
		out1<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
		
		in1>>x>>y;
		c2.moveTo(x,y);
		out1<<"平移后: "<<endl;
		out1<<"c2:圆心: ("<<c2.getX()<<", "<<c2.getY()<<"), "<<"半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<", 面积: "<<c2.getArea()<<endl;
		out1<<"c1与c2圆心之间距离:"<<c1.dist(c2)<<endl;	
		out1<<"c1与c2大小是否相等:"<<c1.isEqual(c2)<<endl<<endl;
	}
	in1.close();
	out1.close();
	return 0;
}

改错

1

/*
请改正程序中指定位置的错误,使程序的输出结果如下:
x=0, y=1
x=1, y=3
x=3; y=7

注意:只允许修改注释"ERROR"的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序清单:
*/
#include <iostream>  
using namespace std; 
 
/**********ERROR**********/
class Test 
{		

/**********ERROR**********/
		int x;
		static int y;   
	public: 
		Test() { y+=1; }

/**********ERROR**********/
		Test(int i,int j):x(i) 
		{ y+=j; }
		void display() const;  
		void display() { cout<<"x="<<x<<", y="<<y<<endl; }  
}; 

int Test::y=0; 

/**********ERROR**********/
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; 
}

2

/*
请改正程序中指定位置的错误,使程序的输出结果如下: 
Base::display
Derive::display
Derive::display const
Base::display
Derive::display const
Derive::display const

注意:只允许修改注释"ERROR"的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
试题源程序:
*/
#include <iostream>
using namespace std;
class Base { 
	public:

/**********ERROR**********/
		virtual void display() const{ 
			cout<<"Base::display"<< endl; 
		} 	
};
class Derive:public Base {  
	public: 

/**********ERROR**********/
		virtual void display() const;   
		void display() { cout<<"Derive::display"<<endl; }
};
void Derive::display() const {  
	cout<<"Derive::display const"<<endl; 
}

/**********ERROR**********/
void Display(const Base &p) { 
	p.display();  
}
int main() { 
	Base b1; Derive d1;

/**********ERROR**********/
	const Derive d2; 
	b1.display(); d1.display(); d2.display(); 	 
	Display(b1); Display(d1); Display(d2); 
	return 0; 
}

填空

1

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

注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线)。
试题源程序:
*/
#include <iostream>  
using namespace std;  

class A { 
		int x,y; 
	public: 
 		A(int a,int b) { x=a, y=b; } 
		void show() { cout<<x<<", "<<y<<endl; } 
}; 

class B: virtual protected A {
		int k; 
  	public: 
		B(int a,int b,int c):A(a,b) { k=c; } 
		void show() { cout<<k<<endl; }  
}; 


/**********FILL**********/
class C: virtual public A { /*重要*/
		int m; 
	public: 
		C(int a,int b,int c):A(a,b) { m=c; } 
		void show() { cout<<m<<endl; } 
}; 


/**********FILL**********/
class D: public B,public C {/*重要*/
		int n; 
	public: 
/**********FILL**********/
		D(int a,int b,int c,int d, int e): A(a,b), B(a,b,c), C(a,b,d) { n=e; }
		void show() { cout<<n<<endl; } 
};
 
int main() { 
	D d(1,3,5,7,9); 
/**********FILL**********/
	d.A::show(); 
	d.B::show();
	d.C::show();
	d.show(); 
	return 0; 
}

2

/*
下面程序在日期(Date)类定义的基础上,重载"=="运算符,用于判断两个日期对象是否相等(对应数据值相等),若相等则返回true,否则返回false;重载运算符">>",用于以"年 月 日"的顺序输入日期对象。但程序不完整。
请将程序补充完整,使得程序运行时输出正确结果。例如,输入:
2022 12 28
2022 12 28
则输出结果为:
日期d1:2022-12-28
日期d2:2022-10-24
d1和d2不是同一天
重置后日期d2:2022-12-28
d1和d2是同一天

注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线编号)。
试题源程序:
*/
#include <iostream>
using namespace std;
class Date { 
	public:		
		Date(int yy=2022,int mm=10,int dd=24);  // 构造函数,yy,mm,dd分别用于初始化日期的年、月、日 
		void setDate(int newY,int newM,int newD);  // 设置日期的年、月、日为newY, newM, newD 
		void showDate() const {  // 显示(输出)当前日期,输出格式为"年-月-日" 
			cout<<this->year<<"-"<<this->month<<"-"<<this->day<<endl;
		}
		bool operator ==(const Date &) const;  // 运算符==重载 

/**********FILL**********/
	friend istream& operator>>(istream &, Date &);  // 运算符>>重载  //重要
	private:
		int year,month,day;  // 日期的年、月、日 
};

/**********FILL**********/
Date::Date(int yy,int mm,int dd) {  
	this->year=yy; this->month=mm; this->day=dd;
}
void Date::setDate(int newY,int newM,int newD) { 
	this->year=newY; this->month=newM; this->day=newD; 
}

/**********FILL**********/
bool Date::operator ==(const Date &d) const{  
	if(this->year==d.year && this->month==d.month && this->day==d.day) return true;  
	else return false; 
}
istream &operator >>(istream &in, Date &d) { 
	in>>d.year>>d.month>>d.day;

/**********FILL**********/
	return cin; 
} 

int main() {
	int y,m,d;
	Date d1,d2;
	cin>>d1;	
	cout<<"日期d1:"; d1.showDate();
	cout<<"日期d2:"; d2.showDate();
	if(d1==d2) cout<<"d1和d2是同一天"<<endl; 
	else cout<<"d1和d2不是同一天"<<endl;
	cin>>y>>m>>d;
	d2.setDate(y,m,d);
	cout<<"重置后日期d2:"; d2.showDate();
	if(d1==d2) cout<<"d1和d2是同一天"<<endl; 
	else cout<<"d1和d2不是同一天"<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值