C++期末试题3

程序填空

1

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

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

/**********FILL**********/
	static int a ; 
	public:  
		myClass() { a++; }  
		~myClass() { a--; }  
		static int getA(); 
};  
int myClass::a=0; 

/**********FILL**********/
   int myClass::getA() { return a; } 
 
int main() {  

/**********FILL**********/
	cout<<myClass::getA() ; 
	myClass d[12];  
	cout<<d[0].getA()<<endl;
	return 0;  
}

2

/*
如下程序通过定义Boat和Car两个类的一个友元函数totalWeight,计算二者的重量之和。
请将如下程序补充完整,使得程序运行时的输出结果为:
900
注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线编号)。
试题源程序如下:
*/
#include <iostream>
using namespace std;

/**********FILL**********/
class Boat    ; 
class Car {
	public:
		Car(int j) { weight=j; }
		friend int totalWeight(Car &,Boat &);  
	private:
		int weight;
};
class Boat {
	public:
		Boat(int j) { weight=j; }
		friend int totalWeight(Car &,Boat &); 
	private:
		int weight;
};

/**********FILL**********/
int totalWeight(Car &c,Boat &b) {
	return c.weight+b.weight;
}
int main() {
	Car c1(400);
	Boat b1(500);

/**********FILL**********/
	cout<< totalWeight(c1, b1) <<endl;
	return 0;
}

改错

1

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

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

/**********ERROR**********/
	static int y;
	public: 
		Test(int i,int j): x(i) { y=j; } 

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

/**********ERROR**********/
	t2.display() ;
	return 0; 
}

2

/*
请改正程序中指定位置的错误,使程序的输出结果如下:
The computer id is 101
The type of monitor is A

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

/**********ERROR**********/
		void display() const 
		{ cout<<"The type of monitor is "<<type<<endl; }
	private:
		char type;
};
class Computer {
	public:

/**********ERROR**********/
		Computer(int i, char c):mon(c)
		{ id=i; }

/**********ERROR**********/
		void Display() const
		{	cout<<"The computer id is "<<id<<endl;  

/**********ERROR**********/
			mon.display(); 
		}
	private:
		int id;
		Monitor mon;
};

int main() {
	const Computer myComputer(101, 'A');
	myComputer.Display(); 
	return 0;
}

程序设计

1

/*
ARRAY类用于存放一个一维int型数组,计算并输出当前数组中各元素的最大值、最小值和平均值。具体成员如下:
(1)私有成员:
l	int data[N]:数组data用于存储待处理的数据,N为常量
l	int num:当前元素的个数
l	int max:元素的最大值
l	int min:元素的最小值
l	float average:所有元素的平均值
(2)公有成员:
l	构造函数ARRAY(int x[], int n):初始化成员数组data,n为元素个数
l	void process():计算data数组中的最大值、最小值和平均值
l	void print():输出当前数组中的元素及其最大值、最小值和平均值
请根据上述说明,完成ARRAY类的定义。

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

class ARRAY {
		int data[N];   // 数组data用于存储待处理的数据,N为常量
		int num;       // 当前元素的个数
		int max,min;   // 元素的最大值、最小值
		float average; // 元素的平均值
	public:
		ARRAY(int x[], int n); //初始化成员数组data,n为元素个数
		void process();    //计算data数组中的最大值、最小值和平均值
		void print() {    // 输出当前数组中的元素及其最大值、最小值和平均值	
			cout<<"共有"<<num<<"个元素: " ;
			for(int i=0;i<num;i++) cout<<data[i]<<' ';
			cout<<endl;
			cout<<"max="<<max<<endl;
			cout<<"min="<<min<<endl;
			cout<<"average="<<average<<endl; 
	}
};

/*******Begin*******/
ARRAY::ARRAY(int x[], int n){
	num=n;
	for(int i=0;i<num;i++) data[i]=x[i];
} 

void ARRAY::process(){
	max=min=data[0];
	average=0.0;
	for(int i=0;i<n;i++){
		if(max<data[i]) max=data[i];
		if(min>data[i]) min=data[i];
		average+=data[i];
	}
	average/=num;
	
}


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

int main() {
	int a[N],n;
	cin>>n;  // 输入待处理元素个数 
	for(int i=0;i<n;i++) cin>>a[i];
	ARRAY arr(a,n);
	arr.process();
	arr.print();

	ifstream in1("6.1.1_3_in.dat");
	ofstream out1("6.1.1_3_out.dat");
	streambuf *cinbackup;
	streambuf *coutbackup;
	cinbackup=cin.rdbuf(in1.rdbuf());
	coutbackup=cout.rdbuf(out1.rdbuf());
	while(cin>>n) {
		for(int i=0;i<n;i++) cin>>a[i];
		ARRAY arr(a,n);
		arr.process();
		arr.print();
		cout<<endl; 
	}
	cin.rdbuf(cinbackup);
	cout.rdbuf(coutbackup);
	in1.close();
	out1.close();
	return 0;
}

2

/*
课程(Course)类主要用于存储某门课程的基础信息和考试成绩,并统计该课程的最高分、最低分、平均分和不及格的学生人数。
Course类的成员声明已给出,请参照注释,完成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,n为学生数量 
		Course(const Course &c); // 实现对象的深复制
		~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];
} 

Course::Course(const Course &c){
	number=c.number;name=c.name;credit=c.credit;n=c.n;max=c.max;min=c.min;average=c.average;count=c.count;
	score=new float[n];
	for(int i=0;i<n;i++) score[i]=c.score[i];
}

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=min=score[0],count=0;average=0;
	for(int i=0;i<n;i++){
		if(max<score[i]) max=score[i];
		if(min>score[i]) min=score[i];
		if(score[i]<60) count++;
		average+=score[i];
	}
	average/=n;
}

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

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

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

3

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

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

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

/*******Begin*******/
Stu::Stu(int n){
	this->n=n;
	score=new float[n];
	count,average=0;
} 

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

void Stu::input(){
	cin>>number>>name;
	for(int i=0;i<n;i++) cin>>score[i];
}
void Stu::process(){
	average=0;
	for(int i=0;i<n;i++) {
		if(score[i]<60) count--;
		average+=score[i];
	}
	average/=n;
}


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

int main() {
	int n;
	cin>>n;
	Stu s(n);
	s.input();
	s.process();
	s.print();

	ifstream in1("6.6.1_2_in.dat");
	ofstream out1("6.6.1_2_out.dat");
	streambuf *cinbackup;
	streambuf *coutbackup;
	cinbackup=cin.rdbuf(in1.rdbuf());
	coutbackup=cout.rdbuf(out1.rdbuf());	
	while(cin>>n) {
		Stu s(n);
		s.input();
		s.process();
		s.print();
	}	
	cin.rdbuf(cinbackup);
	cout.rdbuf(coutbackup);
	in1.close();
	out1.close();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值