/*
请将如下程序补充完整,使得程序运行时的输出结果为:
012
注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线编号)。
试题程序:
*/#include<iostream>
using namespace std;
class myClass{/**********FILL**********/staticint a ;
public:myClass(){ a++;}~myClass(){ a--;}staticintgetA();};int myClass::a=0;/**********FILL**********/int myClass::getA(){return a;}intmain(){/**********FILL**********/
cout<<myClass::getA();
myClass d[12];
cout<<d[0].getA()<<endl;return0;}
2
/*
如下程序通过定义Boat和Car两个类的一个友元函数totalWeight,计算二者的重量之和。
请将如下程序补充完整,使得程序运行时的输出结果为:
900
注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线编号)。
试题源程序如下:
*/#include<iostream>
using namespace std;/**********FILL**********/
class Boat ;
class Car {
public:Car(int j){ weight=j;}
friend inttotalWeight(Car &,Boat &);
private:int weight;};
class Boat {
public:Boat(int j){ weight=j;}
friend inttotalWeight(Car &,Boat &);
private:int weight;};/**********FILL**********/inttotalWeight(Car &c,Boat &b){return c.weight+b.weight;}intmain(){
Car c1(400);
Boat b1(500);/**********FILL**********/
cout<<totalWeight(c1, b1)<<endl;return0;}
改错
1
/*
请改正程序中指定位置的错误,使程序的输出结果如下:
x-y=-1
x+y=7
注意:只允许修改注释"ERROR"的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序清单:
*/#include<iostream>
using namespace std;/**********ERROR**********/
class Test
{constint x;/**********ERROR**********/staticint y;
public:Test(int i,int j):x(i){ y=j;}/**********ERROR**********/voiddisplay()const{ cout<<"x+y="<<x+y<<endl;}voiddisplay(){ cout<<"x-y="<<x-y<<endl;}};int Test::y=0;intmain(){
Test t1(1,2);
t1.display();const Test t2(3,4);/**********ERROR**********/
t2.display();return0;}
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**********/voiddisplay()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**********/voidDisplay()const{ cout<<"The computer id is "<<id<<endl;/**********ERROR**********/
mon.display();}
private:int id;
Monitor mon;};intmain(){const Computer myComputer(101,'A');
myComputer.Display();return0;}
程序设计
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;constint N=100;
class ARRAY {int data[N];// 数组data用于存储待处理的数据,N为常量int num;// 当前元素的个数int max,min;// 元素的最大值、最小值float average;// 元素的平均值
public:ARRAY(int x[],int n);//初始化成员数组data,n为元素个数voidprocess();//计算data数组中的最大值、最小值和平均值voidprint(){// 输出当前数组中的元素及其最大值、最小值和平均值
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*********/intmain(){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();return0;}