问答题简单应用题
请编写一个函数char MaxCharacter(char * str),该函数返回参数str所指向的字符串中具有最大ASCII码的那个字符(如字符串"world"中字符''w''具有最大的ASCII码)。当str所指向的字符串为空时,则返回空字符0x0或''/0''。
输出结果如下:
Good Morning!
Max char:r
注意:部分源程序已存在文件test15_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数MaxCharacter的花括号中填写若干语句。
文件test15_2.cpp的内容如下:
#include
#include
char MaxCharacter(char * str);
void main()
{
char str[100];
strcpy(str,"Good Morning!");
char maxc=MaxCharacter(str);
cout<
问答题请使用VC6或使用【答题】菜单打开
proj1下的工程proj1。程序中位于每个“// 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
};
// ERROR *******found*******
void MyClass::print (MyClass
cout << "number =" << obj.N << endl;
}
int main()
{
MyClass obj(63);
print(obj);
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成重载赋值运算符函数,完成深复制功能。屏幕上输出的正确结果应该是:Hello!Happynewyear!要求:补充编制的内容写在“//**********333**********”与“//**********666**********”两行之间。不得修改程序的其他部分。注意:程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//proj3.h#include<iostream>#include<iomanip>usingnamespacestd;classMiniString{public:friendostream&operator<<(ostream&output,constMiniString&s)//重载流插入运算符{output<<S.sPtr;returnoutput;).friendistream&operator>>(istream&input,MiniString&s)//重载流提取运算符{chartemp[100];//用于输入的临时数细tempi0]='\0';//初始为空字符串input>>setw(100)>>temp;intinLen:strlen(temp);//输入字符长度if(inLen!=0){S.length=inLen;//赋长度if(s.sPtr!=0)delete[]s.sPtr;//避免内存泄漏s.sPtr=newchar[s.length+1];strcpy(s.sPtr,temp);//如果s不是空指针,则复制内容}elses.sPtr[0]='\0';//如果s是空指针,则为空字符串returninput;}voidmodString(constchar*string2)//更改字符串内容{if(string2!=0)//如果string2不是空指针,则复制内容{if(strlen(string2)!=length){length=strlen(string2);delete[]sPtr;sPtr=newchar[length+1];//分配内存}strcpy(sPtr,string2);}elsesPtr[0]='\0';//如果string2是空指针,则为空字符串}MiniString&operator=(constMiniString&otherString);MiniString(constchar*S=""):length((s!=0)?strlen(s):0)//构造函数{sPtr=0;if(length!=0)setString(S);}~MiniString()//析构函数{delete[]sPtr;}private:intlength;//字符串长度char*sPtr;//指向字符串起始位置voidsetString(constchar*string2)//辅助函数{sPtr=newchar[strlen(string2)+1];//分配内存if(string21=0)strcpy(sPtr,string2);//如果string2不是空指针,则复制内容elsesPtr[0]='\0';//如果string2是空指针,则为空字符串}};//proj3.cpp#include<iostream>#include<iomanip>usingnamespacestd;#include"proj3.h"MiniString&MiniString::operator:(constMiniString&otherString){//重载赋值运算符函数。提示:可以调用辅助函数setString//*************333**********//*************666**********}intmain(){MiniStringstrl("Hello!"),str2;voidwriteToFile(constchar*);str2=strl;//使用重载的赋值运算符str2.modString("Happynewyear!");cout<<strl<<'\n';cout<<str2<<'\n';writeToFile("");return0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。该工程中包含一个程序文件maln.cpp,其中有“书”类Book及其派生出的“教材”类TeachingMaterial的定义,还有主函数main的定义。请在程序中“//********found********”下的横线处填写适当的代码,然后删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为: 教材名:C++语言程序设计 页 数:299 作 者:张三 相关课程:面向对象的程序设计 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 #include <iostream> using namespace std; class Book //"书"类 char * title; //书名 int num_pages; //页数 char * writer; //作者姓名 public: Book(const char * the_title,intpages,const char * the_writer):num_pages(pages) title=new char[strlen(the_title)+1]; strcpy(title,the_title); //**********found********** ______ strcpy(writer,the_writer); ~Book()______ int numOfPages()constreturn num_pages; //返回书的页数 const char * theTitle () const return title; //返回书名 const char * theWriter () const return writer; //返回作者名 ; class TeachingMaterial:public Book //“教材”类 char * course; public: TeachingMaterial (const char * the_title,int pages,const char*thewriter,const char * the course) :______ course=new char[strlen(the_course)+1]; strcpy(course,the_course); ~TeachingMaterial()delete[]course; const char*theCourse()constreturn course;) //返回相关课程的名称 ); int main() TeachingMaterial abook ("C++语言程序设计",299,"张三","面向对象的程序设计"); cout<<"教材名:"<<a book.theTitle()<<endl <<"页 数:"<<a_book.numOfPages()<<endl <<"作 者:"<<a book.theWriter()<<endl //**********found**********<<"相关课程:"<<______; cout<<endl; return 0;
问答题使用VC++6.0打开
下的源程序文件3.cpp。其中定义的类不完整,按要求完成下列操作,将类的定义补充完整。每卖出一个水果,则计算水果的重量,还要计算所有卖出水果的总重量以及总个数,同时允许退货,请按照以下的操作,把类补充完整。
(1)定义类TCFruit的私有静态数据成员float型变量AllWeight和int型变量AllNo,请在注释1后添加适当的语句。
(2)完成类TCFruit的带一个float型变量w的构造函数,并把这个w加到AllWeight中,并且AllNo自加。请在注释2后添加适当的语句。
(3)在析构函数中,在AllWeight中减去weight,然后AllNo自减,请在注释3后添加适当的语句。
(4)完成静态成员变量的初始化为0,请在注释4后添加适当的语句。
注意:增加或者修改代码的位置已经用符号表示出来,请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
class TCFruit
{
private:
float Weight;
//********1********
static int AllNO;
public:
TCFruit(float w)
{
//********2********
AllWeight+=w;
AllNo++;
}
~TCFruit()
{
//********3********
AllWeight-=Weight;
}
void display()
{
cout<<"Sell a Fruit with"<<Weight<<"kg"<<endl;
cout<<"All sell number:"<<AllNo<<endl;
cout<<"All sell weight:"<<AllWeight<<"kg"<<endl<<endl;
}
};
//********4********
float TCFruit::AllWeight=0.0;
int main()
{
TCFruit Fruit1(1.2);
Fruit1.display();
TCFruit Fruit2(2.3);
Fruit2.display();
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motocycle类。要求将vehicle作为虚基类,避免二义性问题。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
A vehicle is running!
A vehicle has stopped!
A bicycle is running!
A bicycle has stopped!
A motorcar is running!
A motocycle is running!
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
vehicle(): MaxSpeed(0), Weight(0){
vehicle(int max_speed, int weight): MaxSpeed (max_speed ), Weight (weight)(}
//********** found**********
______Run()
{
cout << "Avehicle is running!" << endl;
}
//********** found**********
______Stop()
{
cout << "A vehicle has stopped!" << endl;
}
};
class bicycle : virtual public vehicle
{
private:
int Height;
public:
bicycle(): Height(0){}
bicycle(int max_speed, int weight,int height)
:vehicle (max_speed, weight), Height(height){};
void Run () {cout << "A bicycle isrunning!" << endl; }
void Stop() {cout << "Abicycle hasstopped!" << endl; }
};
class motorcar : virtual public vehicle
{
private:
int SeatNum;
public:
motorcar(): SeatNum(0) {}
motorcar (int max_speed, intweight, int seat_num)
//********** found**********
:______{}
void Run() {cout << "A motorcar isrunning!" << endl; }
void Stop () {cout << "A motorcarhas stopped!" << endl; }
};
//********** found**********
class motorcycle: ______
{
public:
motorcycle(){}
motorcycle (int max_speed, int weight, int height, int seet_num):bicycle(max_speed, weight, height), motorcar (max_speed, weight, seet_num){};
~motorcycle () {};
void Run () {cout << "A motorcycle is running!" << endl; }
void Stop() {cout << "A motorcycle has stopped!" << endl; }
};
int main()
{
vehicle * ptr;
vehicle a;
bicycle b;
motorcar c;
motorcycle d;
a.Run(); a. Stop();
b.Run(); b. Stop();
ptr = ptr->Run();
ptr = ptr->Run();
return 0;
}
问答题使用VC6打开下的源程序文件modi2.cpp。请实现函数fun(doubleb[],intlen)的如下功能:(1)b[]是一个数组,长度为len;(2)b[0]=0,b[1]=1;(3)b[i+2]=b[i]+b[i+1];注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。#include<iostream>voidfun(doubleb[],intlen){}voidmain(){doubleb[128];fun(b,128);for(inti=0;i<128;i++){if(i%6==5)std::coutstd::endl;}return;}
问答题使用VC++6.0打开下的源程序文件2.cpp。请完成函数fun(int
n),使其实现以下功能:当i等于3时,则打印如下内容。
AAAAAA
注意:不能修改程序的其他部分,只能修改fun函数。 试题程序:
#include<iostream.h> void fun(int n)
{ } void main()
{ int n;
cout<<"请输入打印的行数:"<<endl; cin>>n;
if(n<1) { cout<<"输入的行数必须大于0"<<endl;
return; } fun(n);
return; }
问答题使用VC6打开考生文件夹下的源程序文件modi.cpp,该程序运行时有错误,请改正错误,使得程序正常运行,并且要求最后一个catch()必须抛出执行的任何异常。
程序通过抛出异常输出:
error
0
ERROR
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在
∥********error********的下面。
{l}include
int msin()
{
try
{
throw(“error”);
}
∥********error********
catch(char s)
{
cout<
问答题综合应用
请使用"答题"菜单或使用VC6打开考生文件夹proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:
2 3 4 5 27 28 31 66 75
2 3 4 5 6 27 28 31 66 75
2 3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在//********333********与//********666********两行之间。不得修改程序的其他部分。
注意:
程序最后已经将结果输出到文件out.dat中。输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
问答题使用VC6打开考生文件夹下的工程MyProj14。此工程包含一个源程序文件MyMain14.cpp,程序中定义了3个类A、B和C,但类的定义并不完整。 请按要求完成下列操作,将类的定义补充完成: ①类A的成员函数set x(int i,int j)是虚函数,其实现的功能是将类A的保护成员x和y分别设置成i和j并且y具有默认值0。请在注释“//* *1* * ”之后添加适当的语句。 ②将类A的成员函数print()声明成员纯虚函数。请在注释“//* *2* *”之后添加适当的语句。 ③完成类A的保护数据成员x和y的定义,它们都是整型变量。请在注释“//* *3* *”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件MyMain14.cpp清单如下: //MyMain14. cpp #include <iostream> using namespace std; class A public: //* * * 1 * * * //* * * 2 * * * protected: //* * * 3 * * * ; class B : public A public: void print () cout<<x * x<<" , "; ; class C : public A public: void print () cout <<x* x * x<<end1; ; int main () A *pa; B b; C c; pa= pa->setx (5); pa->print (); pa= pa->setx (2); pa->print (); return 0;
问答题使用VC++6.0打开
下的源程序文件3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义私有成员变量year、month、day,分别表示年、月、日,类型为int。请在注释1后添加适当的语句。
(2)完成构造函数,分别给year、month、day赋值,请在注释2后添加适当的语句。
(3)完成重载符号“+=”的定义,请在注释3后添加适当的语句。
(4)完成函数print打印函数,如2005年1月5日到屏幕和文件out3.txt格式相同,请在注释4后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
#include<fstream2>
#include<iomanip>
#include<cmath>
using namespace std;
void WriteFile(int c)
{
ofstream out1;
out1.open("out3.txt",ios_base::app);
out1<<c<<"";
out1.close();
}
void WriteFik(char*Str)
{
ofstream out1;
out1.open("out3.txt",ios_base::app);
out1<<str;
out1.close();
}
void ClearFile()
{
ofstream out1;
out1.open("out3.txt");
out1.close();
}
class Date
{
public:
Date(int y,int m,int d)
{
//********1********
}
void print();
//********2********
{
month+=m;
int i=month/12;
int j=month%12;
if(j==0)
{
year+=(i-1);
month=12;
}
else
{
year+=i;
month=j;
}
return * this;
}
private:
//********3********
};
void Date::print()
{
//********4********
WriteFile(year);
WriteFile("年");
WriteFile(month);
WriteFile("月");
WriteFile(day);
WriteFile("日");
}
int main()
{
ClearFile();
Date National_day(2004,10,5);
National_day+=3;
National_day.print();
return 0;
}
问答题请使用VC6或使用【答题】菜单打开
prog1下的工程prog1。此工程中包含程序文件main.cpp,其中有类Score(“成绩”)和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
学号:12345678 课程:英语 总评成绩:85
注意:只修改每个“// ERROR ****found****”下的一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Score{
public:
Score (const char * the_course, const char * the_id, int the_normal, int the_midterm, int the_end_of_term}
: course(the_course), normal(the_normal), midterm(the_midterm), end_of_term(the_end_of_term) {
// ERROR *******found*******
strcpy (the_id, student_id);
}
const char * getCourse() const {return course;} //返回课程名称
// ERROR *******found*******
const char * getID() const {return } //返回学号
int getNormal() const {return normal;} //返回平时成绩
int getMidterm() const {return midterm;}
//返回期中考试成绩
int getEndOfTerm() const {return end_of_term;} //返回期考试成绩
int getFinal() const; //返回总评成绩
private:
const char * course; //课程名称
char student_id[12]; //学号
int normal; //平时成绩
int midterm; //期中考试成绩
int end_of_term; //期考试成绩
};
//总评成绩中平时成绩占20%,期中考试占30%,期考试占50%,最后结果四舍五入为一个整数
// ERROR *******found*******
int getFinal() const {
return normal * 0.2 + midterm * 0.3 + end_of_terms 0.5 + 0.5;
}
int main() {
char English[] = "英语";
Score score (English, "12345678",68,83,92);
cout << "学号:" << score.getID() << " ";
cout << "课程:" << score.getCourse() << " ";
cout << "总评成绩:" << score.getFinal() << endl;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。通过继承完成输入到屏幕指定的信息: TestClassA TestClassB TestClassC 其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)完成类B虚继承于A,请在注释//********1********后添加适当的语句。 (2)完成类C虚继承于A,请在注释//********2********后添加适当的语句。 (3)完成类D继承于B,C,请在注释//********3********后添加适当的语句。 (4)函数fun过调用基类的fun,完成所输出的内容,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。 #inc1ude<iostream.h> c1ass TestClassA { public: void fun(){ cout<<.’TestClassA”<<end1; ) ); //********1******** c1ass TestClassB { public: void fun() { cout<<"TestClassB"<<end1; } ); //********2******** c1ass TestClassC { public: void fun() { cout<<"TestClassC"<<end1; } }; //********3******** c1ass TestClassD { public: void fun() { ********4******** } }; void main() { TestClassD test; test.fun(); return; };
问答题使用VC6打开
下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
m=-10
n=-10
p=0
q=-10
z=A
注意:错误的语句在//******error******的下面,修改该语句即可。
#include<iostream.h>
void main()
{
double m=10;
float n=10;
bool p=1;
int q=10;
char z="a";
m=-m;
//******error******
n=~n;
//******error******
p=-p;
//******error******
q=~q;
z=z-32;
cout
"m="
m
endl;
cout
"n="
n
endl;
cout
"p="
p
endl;
cout
"q="
q
endl;
cout
"z="
z
endl;
return;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,其中在编辑窗口内显示的主程序文件中定义有类ABC和主函数main。程序文本中位于每行“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
21 23
注意:只修改每个“//ERROR****found****”下面的一行,不要改动程序中的其他任何内容。
#include <iostream>
using namespace std;
class ABC {
public:
//ERROR **********found**********
ABC() {a=0; b=0; c=0;}
ABC(int aa, int bb, int cc);
void Setab() {++a, ++b;}
int Sum() {return a+b+c;}
private:
int a,b;
const int c;
};
ABC::ABC (int aa, int bb, int cc):c(cc) {a=aa; b=bb;}
int main ()
{
ABC x(1, 2, 3), y(4, 5, 6);
ABC z, *w=
w->Setab();
// ERROR **********found**********
int s1=x. Sum()+y->Sum();
cout <<s1<<";
// ERROR **********found**********
int s2 =s1 +w.Sum();
cout <<s2 <<endl;
return 0;
}
问答题使用VC6.打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数DecToBin(char*des,int n)的功能是将十进制数据n转换成二进制数据,并将转换结果存放在des中。
如:120的二进制数据为1111000
例:
DecToBin(char*des,120);
cout
#define MAXLEN 1.024
void DecToBin(char*des,int n)
{
}
void main()
{
char des[MAXLEN];
int n=120;
DecToBin(des,n);
cout<
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。类Person完成对人的姓名和年龄的绑定。函数fun()获得年龄比较大的姓名。然后输出这个姓名到屏幕。
其中定义的类并不完整,按要求完成下列操作,
将类的定义补充完整。
(1)完成类的构造函数,请在注释∥********1********后添加适当的语句。
(2)完成构造函数对姓名的赋值,请在注释∥********2********后添加适当的语句。
(3)定义类的友元函数fun(),请在注释∥********3********后添加适当的语句。
(4)补充函数fun()的年龄比较,请在注释∥********4********后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include
C1as s Person
{
public:
∥********1********
{
int i;
for(i=0;sUserrName[i]!=0;
i++1
{
m UserName[i]=sUserrName[i];
}
∥********2********
mold=nold;
}
private:
char m_UserName[32];
int m_old;
∥********3********
};
void fun(char*s,Person&personl,
Person&person2)
{
∥********4********
if()
{
for(int i=0;personl.
m UserName[i] !=0;i++)
{
s[i] =personl.m
UserName[i];
s[i+1]=0;
}
}
else
{
for(int i=0;person2.m
UserName[i]!=0;i++)
{
s[i] =person2.m
UserName[i];
S[i+1]=0;
}
}
}
void main()
{
char S[32];
Person p1(“abc”,20);
Person p2(“def”,3 0);
fun(s,p1,p2);
cout<
问答题请使用VC6或使用【答题】菜单打开考生文件夹prosS下的工程pr093,其中包含了类TaxCaleulator(“个税计算器”)和主函数main的定义。创建“个税计算器”需要接收税率表信息和起征额信息。在main函数中,通过两个数组创建了如下的税率表:利用这个税率表创建“个税计算器”时,假定起征额为2000元(即不超过2000元的所得不征收个人所得税)。请补充完成计算应纳个人所得税额的成员函数getTaxPayable,其中的参数income为月收入。此程序的正确输出结果应为:月收入为800元时应缴纳个人所得税0元月收入为l800元时应缴纳个人所得税0元月收入为2800元时应缴纳个人所得税55元月收入为3800元时应缴纳个人所得税l55元月收入为4800元时应缴纳个人所得税295元月收入为5800元时应缴纳个人所得税455元注意:只能在函数9etTaxPayable中的“//**********333**********”和“//**********666**********”之间填人若干语句,不要改动程序中的其他内容。//TaxCalculator.h#include#includeusingflamespacestd;classTaxCalculator{public:TaxCalculator(doublethe—limits[],doublethe_rates[],intthe—length,doublethe_threshold):lower_limits(newdouble[the_length]),rates(newdouble[the—length]),list_len(the_length),threshold(the—threshold){for(inti=0;i=0){//**********333**********//**********666**********--i;}returntax_payable;}voidTaxCalculator::showTaxPayable(doubleincome)const{cout<<”月收入为”<
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程文件proj3,其中定义了用于表示特定数制的数的模板类Number和表示一天中的时间的类TimeOfDay;程序应当显示:
01:02:03.004
06:04:06.021
但程序中有缺失部分,请按照以下的提示,把缺失部分补充完整:
(1)在“// **1** ****found****”的下方是一个定义数据成员seconds的语句,seconds用来表示“秒”。
(2)在“// **2** ****found****”的下方是函数advanceSeconds中的一个语句,它使时间前进k秒。
(3)在“// **3** ****found****”的下方是函数advance中的一个语句,它确定增加k后n的当前值和进位,并返回进位。例如,若n的当前值是表示时间的55分,增加10分钟后当前值即变为5分,进位为1(即1小时)。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。填写的内容必须在一行中完成,否则评分将产生错误。
//proj3.cpp
#include <iostream>
#include <iomanip>
using namespace std;
template <int base> //数制为base的数
class Number
{
int n; //存放数的当前值
public:
Number (int i):n(i) {} //i必须小于base
int advance(int k); //当前值增加k个单位
int value()const{return n;} //返回数的当前值
};
class TimeOfDay{
public:
Number <24> hours; //小时(0~23)
Number <60> minutes; //分(0~59)
//**1** *******found*******
______; //秒(0~59)
Number <1000> milliseconds; //毫秒(0~999)
TimeOfDay(int h=0, int m=0, int s =0, int milli=0)
:hours (h), minutes (m), seconds (s), milliseconds (milli){}
void advanceMillis (int k) {advanceSeconds (milliseconds.advance(k));} //前进k毫秒
void advanceSeconds(int k) //前进k秒
{
// **2** *******found*******
______;
}
void advanceMinutes (int k) {advanceHour (minutes.advance(k));} //前进k分钟
void advanceHour (int k) {hours.advance (k);} //前进k小时
void show() const { //按“小时:分:秒.毫秒”的格式显示时间
int c=cout.fill("0"); //将填充字符设置为"0"
cout << setw(2) << hours.value() << ":" //显示小时
<< setw(2) << minutes.value() << ":" //显示分
<< setw(2) << seconds.value() << "." //显示秒
<< setw(3) << milliseconds.value(); //显示毫秒
cout.fill(c); //恢复原来的填充字符
}
};
template <int base>
int Number <base>::advance (int k)
{
n + = k; //增加k个单位
int s = 0; //s用来累计进位
// **3** *******found*******
while (n >=base)______ //n到达或超过base即进位
return s; //返回进位
}
int main()
{
TimeOfDay time (1,2,3, 4); //初始时间:1小时2分3秒4毫秒
time.show(); //显示时间
time.advanceHour(5); //前进5小时
time.advanceSeconds(122); //前进122秒(2分零2秒)
time.advanceMillis(1017); //前进1017毫秒(1秒零17毫秒)
cout << endl;
time.show(); //显示时间
cout << endl;
return 0;
}