问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程包含一个源程序文件proj2.cpp。其中定义了Score类。 Score是一个用于管理考试成绩的类。其中,数据成员_s指向存储成绩的数组,_n表示成绩的个数;成员函数Sort使用冒泡排序法将全部成绩按升序进行排列。 请在程序中的横线处填写适当的代码,然后删除横线,以实现Score类的成员函数Sort。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 //proj2.cpp #include <iostream> #include <cstdlib> #include <ctime> using namespace std; class Score public: Score(double * s, int n):_s(s),_n(n) double GetScore (int i) const return_s[i]; void Sort (); private: double*_s; int_n; ; void Score::Sort() for(int i=0;i<n-1;______) //********found******** for(int j=______;j>i;j--) if(_s[j]<_s[j-1]) //交换_s[j]和_s[j-1]double t=_s[j]; //********found******** ______; //********found******** ______; int main () const int NUM=10; double s[NUM]; srand(time (0)); for(int i=0;i<NUM;i++) s[i]=double(rand ())/RAND MAX*100; Score ss(s, NUM); ss.Sort(); for(int j=0;j<NUM;j++) cout<<ss.GetScore(j)<<endl; return 0;
问答题改错题
使用VC6打开考生文件夹下的工程kt8_1,此工程包含一个源程序文件kt8_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
f1functionofderive
f2functionofbase
f4functionofbase
源程序文件kt8_1.cpp清单如下:
#include
classbase
{ public:
/**********found**********/
voidf1(){cout
virtualvoidf2(){cout
virtualvoidf3(){cout
voidf4(){cout
/**********found**********/
classderive::publicbase
{ voidf1(){cout
voidf2(intx){cout
voidf4(){cout
voidmain()
{ base*p;
deriveobj2;
/**********found**********/
p=obj2;
p->f1();
p->f2();
p->f4();}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错误,请改正程序中的错误,使得程序输出: 10 TestClass1 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream>Class TestClass1{public: TestClass1(){ }, //********error********private: virtual~TestClass1() { using namespace std; cout<<"TestClass1"<<endl; }; }; class TestClass2:public TestClass1 { publi c: //********error explicit TestClass2(int i) { m i=i ; ); TestClass2&operator()(int i) { this->m i=i: } void print() { //********error cout<<m i<<endl; }private: int m i;};void fun(TestClass2 C1){ C1.print();}int main(){ fun(10); return 0 ;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数remove的作用是从集合中删除指定的元素(如果集合中存在该元素),成员函数filter的作用是去除集合中的所有负整数。请编写这个filter函数。此程序的正确输出结果应为:
5 28 2 -4 5 3 2 -75 27 66 31
5 28 2 -4 5 3 2 -75 27 66 31 6
5 28 2 -4 5 3 2 -75 27 66 31 6 -19
5 28 2 -4 5 3 -75 27 66 31 6 -19
5 28 2 -4 5 3 -75 27 66 31 6 -19 4
5 28 2 5 3 27 66 31 6 4
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:相关文件包括:main.cpp、Integers.h。
程序最后将调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。
//Integevs.h
#ifndef INTEGERS
#define INTEGERS
#include <iostream>
using namespace std;
const intMAXELEMENTS =i00; //集合最多可拥有的元素个数
class Integers{
int elem[MAXELEMENTS]; //用于存放集合元素的数组
int counter; //用于记录集合中元素个数的计数器
public:
Integers(): counter(0) {}
Integers(int data[], int size); //利用数组提供的数据创建一个整数集合
void add(int element);
void remove (int element); //删除集合中指定的元素
int getCount ()const{ return counter; } //返回集合中元素的个数
int getElement(int i)const{ return elem[i];} //返回集合中指定的元素
void filter(); //删除集合中的负整数
void show()const;
};
void writeToFile(const char * path);
#endif
//main.cpp
#include "Integers.h"
#include <iomanip>
Integers::Integers (int data[], int size): counter(0) {
for(int i=0; i<size; i++)
add (data[i]);
}
void Integers::add (int element) {
if (counter<MAXELEMENTS)
elem[counter++ ]=element;
}
void Integers::remove (int element int j;
for(j =counter-1; j>=0; j--)
if (elem[j] ==element) break;
for(int i=j; i<counter-1; i++
elem[i] =elem[i+l];;
counter--;
}
void Integers::filter () {
//******** 333********
//******** 666********
}
void Integers::show () const {
for(int i=0; i<getCount (); i++
cout <<setw(4) <<getElement(i);
cout<<endl;
}
int main () {
int d[]={5, 28, 2, -4, 5, 3, 2, -75, 27, 66, 31};
Integers s(d, 11); s.show();
s.add(6); s.show();
s.add(-19); s.show ();
s.remove(2); s.show();
s.add(4); s.show();
s.filter(); s.show();
writeToFile ("");
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程projl。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: value=63 number=1 注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;class MyClasS{ int*P; const int N;public://ERROR**********found********** MyClass(int val):N=1 { P=new int; *P=val; }//ERROR**********found********** 一MyClass(){delete*P;) friend void print(MyClass&obj);};//ERROR********** found**********void MyClass::print(MyClass&obj){ cout<<”value=”<<* (obj.P) <<end1; cout<<”number=” <<obj.N<<end1;}int main(){ MyClass obj(63); print(obj); return 0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成proj3.h,重载+运算符。
要求:
补充编制的内容写在“//*********333********”与“//********666*********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//proj3.h
#include <iostream>
#include <iomanip>
using namespace std;
class MiniString //+运算符重载
{public:
friendostream return output;}
friend istream //用于输入的临时数组
temp[0] = '/0';
input >> setw (100) >> temp;
int inLen = strlen(temp); //取输入字符串长度
if( inLen ! = O)
{
s.length = inLen; //赋长度
if(s.sPtr! = 0) delete []s.sPtr; //避免内存泄
s.sPtr = new char [s.length + 1];
strcpy( s.sPtr, temp ); //如果s不是空指针,则复制内容
}
else s.sPtr[0] = '/0'; //如果s是空指针,则为空字符串
return input;
}
MiniString (const char * s = ""):
length((s!= 0)?strlen (s): 0){setString (s);}
~MiniString() {delete [] sPtr; } //析构函数
//************* 333***********
//+运算符重载
//************* 666***********
MiniString (MiniString
sPtr = new char [s.length + 1];
strcpy( sPtr, s.sPtr);
}
private:
int length; //字符串长度
char * sPtr; //指向字符串起始位置
void setString(const char * string2) //辅助函数
{
sPtr = new char [strlen(string2) + 1]; //分配内存
if ( string2!= 0)
strcpy( sPtr, string2 ); //如果string2不是空指针,则复制内容
else sPtr [0] = '/0'; //如果string2是空指针,则为空字符串
}
};
//proj3.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "proj3.h"
int main ()
{
MiniString strl ("Hello!"), str2 ("World");
void writeToFile (char * );
MiniString temp = str1 + str2; //使用重载的+运算符
cout << temp << "/n";;
writeToFile ("");
return 0;
}
问答题请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2。此工程包含程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在程序中//************found************下的画线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: 车牌号:冀ABC1234品牌ForLand类别:卡车当前档位:0最大载重量:1~ 车牌号:冀ABC1234品牌ForLand类别:卡车当前档位:2最大载重量:1~ 车牌号:沪XY25678品牌QQ类别:小轿车当前档位:0座位数:5 车牌号:沪XY25678品牌QQ类别:小轿车当前档位:-1座位数:5 注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动 //************found************。 //源程序 #include<iostream> #include <iomanip> #include <cmath> using namespace std; class AutoMobile //"汽车"类 char*brand; //汽车品牌 char*number; //车牌号 int speed; //档位:1、2、3、4、5,空档:0,倒档:-1 public: AutoMobile(const char*the_brand,const char*the_number): speed(0) //************found************ ______; strcpy(brand,the_brand); number=new char[strlen(the_nurnber)+1]; //************found************ ______; ~AutoMobile()delete[] brand; delete[] number; const char*theBrand() const return brand; //返回品牌名称 const char*theNumber() const return number; //返回车牌号 int currentSpeed() const return speed; //返回当前档位 void changeGearTo(int the_speed) //换到指定档位 if(speed>=-1&&speed<=5)speed=the_speed; virtual const char*category() const=0; //类别:卡车、小轿车等 virtual void show() const cout<<"车牌号:"<<theNumber()<<"品牌:"<<theBrand() //************found************ <<"类别:"<<______<<"当前档位:"<<currentSpeed(); ; class Car: public AutoMobile//"小汽车"类 int seats;//座位数 public: Car(const char*the_brand,const char*the_number,int the_seats) : AutoMobile(the_brand,the_number),seats(the_seats) int numberOfSeat() const return seats,//返回座位数 const char*category() constreturn"小轿车";//返回汽车类别 void show() const AutoMobile::show(); cout<<"座位数:"<<numberOfSeat()<<endl; ; class Truck: public AutoMobilel//"卡车"类 int max_load;//最大载重量 public: Truck(const char*the_brand,const char*the_number,int the_max_load) : AutoMobile(the_brand,the_number),max_load(the_max_load) int maxLoad() const return max_load;//返回最大载重量 const char*category()const return"卡车";//返回汽车类别 void show() const //调用基类的show()函数 //************found************ cout<<"最大载重量:"<<maxLoad()<<endl; ; int main() Truck truck("ForLand","冀ABC1234",12); truck.show(); truck.changeGearTo(2); truck.show(); Car car("QQ","沪XY25678",5); car.show(); car.changeGearTo(-1); car.show(); cout<<endl; return 0:
问答题写出下列程序的运行结果。
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass();
~MyClass();
void SetValue(int val);
private:
int i;
};
MyClass::MyClass() :i(0)
{
cout << "This is a constructor! i=" << i << endl;
}
MyClass:: ~MyClass()
{
cout << "This is a destructor! i=" << i << endl;
}
void MyClass::SetValue(int val)
{
i=val;
cout << "i =" << i << endl;
}
int main()
{
int k;
MyClass my[3], *p;
p=my;
for (k=0; k<3; k++)
{
p->SetValue(k+1);
p++;
}
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹pmj2下的工程pmj2。其中有向量基类VectorBase、向量类Vector和零向量类ZemVector的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序正确输出结果应为:(1,2,3,4,5) (0,0,0,0,0,0) 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<ioStream>using namespace std;class VectorBase{ //向量基类,一个抽象类 int len;public: VectorBase(int len):len(1en){) int length()const{return len;)//向量长度,即向量中元素的个数 virtual double getElement(int i)const:0;//取第i个元素的值 virtual double sum()const=0;//求所有元素的和 void show()const{//显示向量中所有元素 cout<<”(”; for(int i=0;i<length()一1;i++) cout<<getElement(i)<<”,”; //**********found********** cout<<__________<<”)”<<endl;//显示最后一个元素 } }; class Vector:public VectorBase{ //向量类 double*val ;public: Vector(int len,double v[]=NULL):VectorBase(1en){ val=new double[len]; for(int i=0;i<len; i++)val[i]=(v==NULL?0.0:v[i]); } //**********found********** Vector(){_______;)double getElement(int index)const{return val[index];) double sum()const{. double S=0.0; //**********found********** for(int i=0;i<length();i++)______; return S; } }; class ZeroVector:public VectorBase{ //零向量类 public: ZeroVector(int len): VectorBase(len){) //**********found********** double getElement(int index)const{_____;) double sum()const{return 0.0;)};int main(){ VeCtOrBaSe*V; double d[]={1,2,3,4,5}; V=new Vector(5,d); v一>show(); delete V; v=new ZeroVector(6); v一>show(); delete V; return 0;}
问答题使用VC6打开考生文件夹proj3下的工程proj3,其中定义了一个字符串变量类StringVar。类成员的说明在程序注释中。请在//**********333**********和//**********666**********之间填写StringVar成员函数和友元函数的实现代码。在main函数中给出了一组测试数据,运行时输入: Hello Kitty 此情况下程序的输出应该是: Hello Kitty Borg Borg 注意:只需在//**********333**********和//**********666**********之间填入所编写的若干语句,不要改动程序中的其他内容。//StringVar.h#include<iostream>#include<cstdl ib>#include<cstddef>#include<cstring>USing namespace std;void writeToFile (const char *path);class StringVar{ public: StringVar(int size);//构造函数,size为字符串长度(字符个数)初始值;字符串内容初始化为空串StringVar(const char a[]);//构造函数,用参数数组a的内容初始化当前对象 StringVar (const StringVar//析构函数 int length()const{return Strlen(value);) //从输入流ins输入一个字符串,其中可以包括空格 void input_line(istream cout<<name2<<endl; cout<<name3<<endl; writeToFile(”.\\”); return 0; } //writeToFile.cpp #include<iostream> #include<fstream> #include<Sstream> #include<string> using namespace std; #include”StringVar.h” void writeToFile(const char*path) { char filename[30]; strcpy(filename,path); strcat(filename,”out.dat”); ofstream fout(filename); istringstream is(string(”Jenny Zheng”)); StringVar namel(40),name2(” John”); name1.input_line(is); StringVar name3(name2); fout<<namel<<name2<<name3; fout.close();}
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程文件proj3,该文件中定义了用于表示日期的类Date、表示人员的类Person和表示职员的类Staff;程序应当显示:
张小丽123456789012345
但程序中有缺失部分,请按以下提示把缺失部分补充完整:
(1)在“// **1** ****found****”的下方是析构函数定义中的语句,它释放两个指针成员所指向的动态空间。
(2)在“//**2** ****found****”的下方是rename函数中的一个语句,它使指针name指向申请到的足够容纳字符串new_name的空间。
(3)在“// **3** ****found****”的下方是构造函数定义的一个组成部分,其作用是利用参数表中前几个参数对基类Person进行初始化。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。填写的内容必须在一行中完成,否则评分将产生错误。
//proj3.cpp
#include <iostream>
using namespace std;
class Person
{
char * idcardno; //用动态空间存储的身份证号
char * name; //用动态空间存储的姓名
bool ismale; //性别:true为男,false为女
public:
Person(const char * pid, const char * pname, bool pmale);
~Person() {
// **1** *******found*******
______;
}
const char * getIDCardNO() const{return idcardno;}
const char * getName() const{return name;}
void rename(const char * new_name);
bool isMale() const{return is-male;}
};
class Staff: public Person
{
char * department;
double salary;
public:
Staff(const char * id_card_no, const char * p_name, bool is_male, const char * dept, double sal);
~Staff() {delete [] department;}
const char * getDepartment() const{return department;}
void setDepartment(const char * d);
double getSalary() const{return salary;}
void setSalary(double s) {salary = s;}
};
Person::Person (const char * id_card_no, const char * p_name, bool is_male):ismale(is_male)
{
idcardno = new char [strlen(id_card_no)+1];
strcpy (idcardno, id_card_no);
name = new char [strlen (p_name)+1];
strcpy(name,p_name);
}
void Person::rename (const char * new_name)
{
delete []name;
// **2** *******found*******
______;
strcpy(name,new_name);
}
Staff::Staff(const char * id_card_no, const char * p_name, bool is male,
// **3** *******found*******
const char * dept, double sal):______
{
department = new char [strlen(dept) +1];
strcpy(department,dept);
salary =sal;
}
void Staff::setDepartment(const char * dept)
{
delete []department;
department = new char [strlen(dept) +1];
strcpy(department,dept);
}
int main()
{
Staff Zhangsan("123456789012345","张三", false,"人事部",1234.56);
Zhangsan.rename("张小丽");
cout << Zhangsan.getName() << Zhangsan.getIDCardNO() << endl; return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)声明类objA1,请在注释∥********1********后添加适当的语句。
(2)为类objA0增加友元函数func0,请在注释∥********2********后添加适当的语句。
(3)为类objA1增加友元函数func0,请在注释∥********3********后添加适当的语句。
(4)函数func0返回obiAl对象中的变量和objA0的静态变量的乘积,请在注释∥********4********后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include
∥********1********
class objA0
{
private:
static int m A0;
∥********2********
};
int obj A0::m A0=10;
class objA1
{
private:
int m A1;
∥********3********
public:
objAl(int i)
{
m A1=i;
}
};
int func(objAl&obj)
{
∥********4********
}
int main()
{
objA1 obj0(10);
cout<
问答题使用VC6打开考生文件夹下的工程RevProj12。此工程包含一个源程序文件RevMain12.cpp,但在该程序中有错误。请改正程序中的错误,使它能得到正确结果。 注意:不得删行或增行,也不得更改程序的结构。 源程序文件RevMain12.cpp中的程序清单如下: //RevMain12.cpp #include<iostream> /* * * * FOUND * * * * */ using namespace std; class test private: const int value; char dep[10]; public: /* * * * *FOUND* * * * */ test() value=0; strcpy(dep,"m"); /* * * * *FOUND* * * * */ test(int newvalue) value=newvalue; strcpy (dep, "m"); /* * * * *FOUND * * * * */ void show() cout<<"value= "<<value<<end1; ; int main () test t1; const test t2; t1.show (); t2.show(); return 0;
问答题请编写一个函数void fun(int aa[],int n,int x),其中n表示数组aa中元素的个数,函数的功能是:计算前x项的和并放在aa[x]中,aa数组中的元素值和x的值由主函数通过键盘读入。
注意:用循环和数组实现。
部分源程序已存在文件PROC3.cpp中。请勿修改主函数和其他函数中的任何内容,
仅在函数resort()的花括号中填写若干语句。
文件PROC3.cpp中的程序清单如下:
//PROC3.CPP
#include <iostream>
using namespace std;
#define MAX 100
int main ()
{
void fun(int aa[],int n, int x);
int bb[MAX],i,x,n;
cout<<"Please enter the counter of the number:/n";
cin>>n;
cout<<"Please enter the number:/n";
for(i=0;i<n;i++)
cin>>bb[i];
cout<<"Input the x:/n";
cin>>x;
fun(bb, n,x);
cout<<"The data after total: "<<bb[x]<<end1;
return 0;
}
void fun(int aa[],int n, int x)
{
//*********
}
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有类CPolygon(“多边形”)、CRectangle(“矩形”)、CTriangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为:
20
10
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class CPolygon {
public:
// **********found**********
______//纯虚函数area声明
void printarea (void)
// **********found**********
{cout <<______<< endl;}
};
class CRectangle: public CPolygon {
int width; //长方形宽
int height; //长方形高
public:
CRectangle (int w, int h): width (w), height (h) {}
int area (void) {return (width * height);}
};
class CTriangle : public CPolygon {
int length; //三角形一边长
int height; //该边上的高
public:
CTriangle (int 1, int h): length (1), height (h) {}
// **********found**********
int area (void) {return (______)/2;}
};
int main() {
CRectangle rect (4,5);
CTriangle trgl (4,5);
// **********found**********
______*ppoly1, *ppoly2;
ppoly1 =
ppoly2 =
ppoly1 -> printarea();
ppoly2 -> printarea();
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)定义复数类CComplex的私有成员变量real和imaginary,分别用来表示复数的实部和虚部,都是double 类型的变量。 请在注释∥********1********后添加适当的语句。
(2)添加复数类CComplex的带一个参数的构造函数,分别将real和imaginary赋值为参数r和0。请在注释∥********2********后添加适当的语句。
(3)完成对运算符“+”的重载,分别对复数的实部和虚部相加。请在注释∥********3********后添加适当的语句。
(4)完成复数的友元函数Equal(CComplex
cout<
问答题请使用VC6或使用【答题】菜单打开考生文件夹pmj1下的工程proj1,此工程中包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:Name:sonnyType:dogName:johnType:dogName:DannyType:catName:johnType:dog注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>usingnamespacestd;enumPetstype{dog,cat,bird,fish};classPets{private:char*name;Pets_typetype;public:Pets(constchar*name="sonny",Pets_typetype=dog);Pets&operator=(constPets&s);~Pets();voidshow()const;};Pets::Pets(constchar*name,Petstypetype)//构造函数{this->name=newchar[strlen(name)+1];strcpy(this->name,name);//ERROR*********found*********type=type;}Pets::~Pets()//析构函数,释放name所指向的字符串{//ERROR*********found*********name='/0';}Pets&Pets::operator=(constPets&s){if(&s=this)//确保不要向自身赋值return*this;delete[]name,name=newchar[strlen(s.name)+1];//ERROR*********found*********strcpy(this->name,name);type=s.type;return*this;}voidPets::show()const{cout<<"Name:"<<name<<"Type:";switch(type){casedog:cout<<"dog";break;casecat:cout<<"cat";break;casebird:cout<<"bird";break;casefish:cout<<"fish";break;}cout<<end1;}intmain(){Petsmypet1,mypet2("john",dog);Petsyoupet("Danny",cat);mypet1.show();mypet2.show();youpet.show();youpet=mypet2;youpet.show();return0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Initial times are
0 d:0 h:0 m:59 s
After one second times are
0 d:0 h:1 m:0 s
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Clock
{
public:
Clock (unsigned long i = 0);
void set (unsigned long i = 0);
void print () const;
void tick(); //时间前进一秒
Clock operator ++ ();
private:
unsigned long total_sec, seconds,minutes, hours, days ;
};
Clock::Clock (unsigned long i)
: total_sec(i), seconds(i % 60),
minutes((i / 60) % 60),
hours((i / 3600) % 24),
days(i / 86400) {}
void Clock: :set (unsigned long i)
{
total sec = i;
seconds = i % 60;
minutes = (i / 60) % 60;
hours = (i /3600)% 60;
days = i / 86400;
}
// ERROR **********found**********
void Clock::print ()
{
cout <<days << "d:" <<hours <<"h:"
<<minutes <<"m:" <<seconds<<"s" <<endl;
}
void Clock::tick()
{
// ERROR **********found**********
set (total_sec++);
}
Clock Clock::operator ++ ()
{
tick ();
// ERROR **********found**********
return this;
}
int main ()
{
Clock ck(59);
cout <<"Initial times are" <<endl;
ck.print();
++ck;
cout <<"After one second times are" <<endl;
ck.print();
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,该工程含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ***found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The value is 10 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。 //proj1.cpp #include<iostream> using namespace std; class MyClass{ int valHe; public: //ERROR ********found******** void MyClass(int val):value(val){} int GetValue() const { return value;} void SetValue(int val); }; //ERROR ********found******** inline void SetYalue(int val){value=val;} int main() { MyClass obi(o); obi.SetValue(10); //ERROR ********found******** 下列语句功能是输出obj的成员value的值 cout<<"The value is"<<obj.value<<endl; return 0; }
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类Imege~和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数remove的作用是从集合中删除指定的元素(如果集合中存在该元素),成员函数sort的作用是将集合中的整数按升序进行排序。请编写这个sort函数。此程序的正确输出结果应为:52824532752766315282453275276631528245327527663161952845327527663161952845327527663161942344556192728316675要求:补充编制的内容写在“//**********333**********”与“//**********666**********”之间。不得修改程序的其他部分。注意:相关文件包括:mmn.cpp、Integers.h。程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//Integers.h#ifndefINTEGERS#defineINTEGERS#include<iostream>usingnamespacestd;constintMAXELEMENTS=100;//集合最多可拥有的元素个数classIntegers{intelem[MAXELEMENTS];//用于存放集合元素的数组intcounter;//用于记录集合中元素个数的计数器public:Integers():counter(O){}//创建一个空集合Integers(intdata[],intsize);//利用数组提供的数据创建一个整数集合voidadd(intelement);//添加一个元素到集合中voidremove(intelement);//删除集合中指定的元素intgetCount()const{returncounter;}//返回集合中元素的个数intgetElement(inti)const{returnelem[i];}//返回集合中指定的元素voidsort();//将集合中的整数按由小到大的次序进行排序voidshow()const;//显示集合中的全部元素};voidwriteToFile(constchar。path);#endif//main.cpp#include"Integers.h"#include<iomanip>Integers::Integers(intdata[],intsize):counter(0){for(inti=0;i<Size;i++)add(data[i]);}voidIntegers::add(intelement){if(counter<MAXELEMENTS)elem[counter++]=element;}voidIntegers::remove(intelement){intj;for(j=counter-1;j>=0;j--)if(elem[j]=element)break;for(inti=j;i<counter-1;i++)elem[i]=elem[i+1];counter--;}voidIntegers::sort(){//********333********//********666********}voidIntegers::show()const{for(inti=0;i<getCount();i++)cout<<setw(4)<<getElement(i);cout<<end1;}intmain(){intd[]={5,28,2,4,5,3,2,75,27,66,31);Integerss(d,11);s.show();s.add(6);s.show();s.add(19);s.show();s.Eemove(2);s.show();s.add(4);s.show();s.sort();s.show();writeToFile("");return0;}