基本操作 原题
#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;
}
基本操作 答案
#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() const
{
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;
}
简单应用 原题
#include <iostream>
using namespace std;
class Point //定义坐标点类
{
public:
Point(int xx=0, int yy=0) {x=xx; y=yy;}
void PrintP(){cout<<"Point:("<<x<<","<<y<<")";}
private:
int x,y; //点的横坐标和纵坐标
};
class Circle //定义圆形类
{
public:
Circle():rr(0){} //无参构造函数
Circle(Point& cen, double rad=0); //带参构造函数声明
double Area(){return rr*rr*3.14159;} //返回圆形的面积
//PrintP函数定义,要求输出圆心坐标和半径
//**********found**********
void PrintP(){________________; cout<<rr<<endl;}
private:
Point cc; //圆心坐标
double rr; //圆形半径
};
//带参构造函数的类外定义,要求由cen和rad分别初始化cc和rr
//**********found**********
Circle::____________(Point& cen, double rad)
//**********found**********
____________ {rr=rad;}
int main() {
Point x, y(4,5);
Circle a(x,3), b(y,6);
// 输出两个圆的圆心坐标和半径
a.PrintP();
//**********found**********
____________;
cout<<a.Area()<<' '<<b.Area()<<endl;
return 0;
}
简单应用 答案
#include <iostream>
using namespace std;
class Point //定义坐标点类
{
public:
Point(int xx=0, int yy=0) {x=xx; y=yy;}
void PrintP(){cout<<"Point:("<<x<<","<<y<<")";}
private:
int x,y; //点的横坐标和纵坐标
};
class Circle //定义圆形类
{
public:
Circle():rr(0){} //无参构造函数
Circle(Point& cen, double rad=0); //带参构造函数声明
double Area(){return rr*rr*3.14159;} //返回圆形的面积
//PrintP函数定义,要求输出圆心坐标和半径
//**********found**********
void PrintP(){cc.PrintP(); cout<<rr<<endl;}
private:
Point cc; //圆心坐标
double rr; //圆形半径
};
//带参构造函数的类外定义,要求由cen和rad分别初始化cc和rr
//**********found**********
Circle::Circle(Point& cen, double rad)
//**********found**********
:cc(cen) {rr=rad;}
int main() {
Point x, y(4,5);
Circle a(x,3), b(y,6);
// 输出两个圆的圆心坐标和半径
a.PrintP();
//**********found**********
b.PrintP();
cout<<a.Area()<<' '<<b.Area()<<endl;
return 0;
}
综合应用 原题
//main.cpp
#include"Array.h"
//统计出数组a中大于等于x的元素个数
template<class Type>
int Array<Type>::Count(Type x) { //补充函数体
//********333********
//********666********
}
void main(){
int s1[8]={20, 13, 36, 45, 32, 16, 38, 60};
double s2[5]={3.2, 4.9, 7.3, 5.4, 8.5};
Array<int> d1(s1,8);
Array<double> d2(s2,5);
int k1, k2;
k1=d1.Count(30); k2=d2.Count(5);
cout<<d1.Length()<<' '<<d2.Length()<<endl;
cout<<k1<<' '<<k2<<endl;
writeToFile(".\\"); //不用考虑此语句的作用
}
//Array.h
#include<iostream>
#include<cstdlib>
using namespace std;
template<class Type>
class Array { //数组类
Type *a;
int size;
public:
Array(Type b[], int len): size(len) //构造函数
{
if(len<1 || len>100) {cout<<"参数值不当!\n"; exit(1);}
a=new Type[size];
for(int i=0; i<size; i++) a[i]=b[i];
}
int Count(Type x); //统计出数组a中大于等于x的元素个数
int Length() const{ return size; } //返回数组长度
~Array(){delete []a;}
};
void writeToFile(const char *); //不用考虑此语句的作用
综合应用 答案
#include"Array.h"
//统计出数组a中大于等于x的元素个数
template<class Type>
int Array<Type>::Count(Type x) { //补充函数体
//********333********
int count=0;
for(int i=0;i<size;i++)
if(a[i]>=x) count++;
return count;
//********666********
}
void main(){
int s1[8]={20, 13, 36, 45, 32, 16, 38, 60};
double s2[5]={3.2, 4.9, 7.3, 5.4, 8.5};
Array<int> d1(s1,8);
Array<double> d2(s2,5);
int k1, k2;
k1=d1.Count(30); k2=d2.Count(5);
cout<<d1.Length()<<' '<<d2.Length()<<endl;
cout<<k1<<' '<<k2<<endl;
writeToFile(".\\"); //不用考虑此语句的作用
}