C++试题:声明和实现一个时间类time24。

用C++声明和实现一个时间类time24:a)实现获得和设置由时,分组成的24小时制时间的方法;b)编写程序实现“+”运算,即实现time24对象与整型分钟数相加的运算,... 用C++声明和实现一个时间类time24:
a)实现获得和设置由时,分组成的24小时制时间的方法;
b)编写程序实现“+”运算,即实现time24对象与整型分钟数相加的运算,要求该运算返回一个time24对象;
c)实现time24类的等价运算符重载,即比较两个time24对象是否相等;
d)实现输入和输出重载。
要求该类具有构造函数,并适当使用友元函数和运算符重载方法。
您的答案是:
最佳答案 #include <exception>
#include <ostream>

class invalid_time : std::exception {};

class time24
{
public:
time24(unsigned hour, unsigned min);
~time24();

void set(unsigned hour, unsigned min);
void add_min(unsigned min);

unsigned hour() const;
unsigned min() const;

private:
unsigned _hour;
unsigned _min;

friend bool operator==(time24 const&, time24 const&);
};

time24::time24(unsigned hour, unsigned min)
: _hour(0), _min(0)
{
set(hour, min);
}

time24::~time24()
{
}

void time24::set(unsigned hour, unsigned min)
{
if( hour>23 || min>59 )
throw invalid_time();

_hour = hour;
_min = min;
}

void time24::add_min(unsigned min)
{
unsigned dh = (_min + min) / 60;

_hour = (_hour + dh) % 24;
_min = (_min + min) % 60;
}

unsigned time24::hour() const
{
return _hour;
}

unsigned time24::min() const
{
return _min;
}

time24& operator+(time24& t, unsigned min)
{
t.add_min(min);
return t;
}

time24& operator+(unsigned min, time24& t)
{
return operator+(t, min);
}

bool operator==(time24 const& lhs, time24 const& rhs)
{
return lhs._hour == rhs._hour
&& lhs._min == rhs._min;
}

std::ostream& operator<<(std::ostream& os, time24 const& t)
{
os << "hour=" << t.hour() << ", min=" << t.min();
return os;
}

能不能麻烦您把main()也补充下啊,太感谢您了!
展开
 我来答
OGRobot
推荐于2016-08-03 · TA获得超过1516个赞
知道小有建树答主
回答量:335
采纳率:100%
帮助的人:611万
展开全部
#include <exception>
#include <iostream>

struct invalid_time : std::exception { char const* what() const throw() { return "invalid time value!"; } };
struct bad_time_format : std::exception { char const* what() const throw() { return "bad time format!"; } };

class time24
{
public:
time24();
time24(unsigned hour, unsigned min);
~time24();

void set(unsigned hour, unsigned min);
void add_min(unsigned min);

unsigned hour() const;
unsigned min() const;

private:
unsigned _hour;
unsigned _min;

friend bool operator==(time24 const&, time24 const&);
};

time24::time24()
: _hour(0), _min(0)
{
}

time24::time24(unsigned hour, unsigned min)
: _hour(0), _min(0)
{
set(hour, min);
}

time24::~time24()
{
}

void time24::set(unsigned hour, unsigned min)
{
if( hour>23 || min>59 )
throw invalid_time();

_hour = hour;
_min = min;
}

void time24::add_min(unsigned min)
{
unsigned dh = (_min + min) / 60;

_hour = (_hour + dh) % 24;
_min = (_min + min) % 60;
}

unsigned time24::hour() const
{
return _hour;
}

unsigned time24::min() const
{
return _min;
}

time24& operator+(time24& t, unsigned min)
{
t.add_min(min);
return t;
}

time24& operator+(unsigned min, time24& t)
{
return operator+(t, min);
}

bool operator==(time24 const& lhs, time24 const& rhs)
{
return lhs._hour == rhs._hour
&& lhs._min == rhs._min;
}

std::ostream& operator<<(std::ostream& os, time24 const& t)
{
os << t.hour() << ":" << t.min();
return os;
}

std::istream& operator>>(std::istream& is, time24& t)
{
char c;
unsigned h, m;

is >> h >> c >> m;
if( c != ':' )
throw bad_time_format();
t.set( h, m );

return is;
}

int main()
{
try
{
time24 a, b;

//使用重载的>>输入时间,格式为: 小时:分钟
std::cout << "please input time a: ";
std::cin >> a;

std::cout << "please input time b: ";
std::cin >> b;

//使用重载<<输出a,b两个时间值
std::cout << "time a is: " << a << std::endl;
std::cout << "time b is: " << b << std::endl;

//使用operator==判断输入的两个时间是否相等
if( a == b )
std::cout << "time a == time b" << std::endl;
else
std::cout << "time a <> time b" << std::endl;

//输入一个整形值,用于测试对时间的加运算符
unsigned addMin;
std::cout << std::endl << "please input a integer: ";
std::cin >> addMin;

a = a + addMin;
b = b + addMin;
std::cout << "time a + " << addMin << " minute = " << a << std::endl;
std::cout << "time b + " << addMin << " minute = " << b << std::endl;
}
catch( std::exception const& err )
{
std::cout << "Error: " << err.what() << std::endl;
}

std::cin.clear();
std::cin.get();
return 0;
}

//运行结果
please input time a: 16:50
please input time b: 22:10
time a is: 16:50
time b is: 22:10
time a <> time b

please input a integer: 2222
time a + 2222 minute = 5:52
time b + 2222 minute = 11:12
请按任意键继续. . .

上次帮你做作业已经是在害你了,main很简单,就是按题目要求演示这个类的使用,这个你自己思考下很容易写出来的
来自:求助得到的回答
百度网友1fc36d4
2011-11-25 · TA获得超过3128个赞
知道大有可为答主
回答量:1497
采纳率:100%
帮助的人:1822万
展开全部
把类的实现也修改了下

#include <exception>
#include <iostream>
using namespace std;

class invalid_time : std::exception {};

class time24
{
public:
friend std::ostream& operator<<(std::ostream& os, time24 const& t);
friend std::istream& operator>>(std::istream& is, time24 &t);
friend bool operator==(time24 const&, time24 const&);

time24();
time24(unsigned hour, unsigned min);
~time24();

void set(unsigned hour, unsigned min);
void add_min(unsigned min);

unsigned hour() const;
unsigned min() const;

private:
unsigned _hour;
unsigned _min;
};

time24::time24()
: _hour(0), _min(0)
{
}

time24::time24(unsigned hour, unsigned min)
: _hour(0), _min(0)
{
set(hour, min);
}

time24::~time24()
{
}

void time24::set(unsigned hour, unsigned min)
{
if( hour>23 || min>59 )
throw invalid_time();

_hour = hour;
_min = min;
}

void time24::add_min(unsigned min)
{
unsigned dh = (_min + min) / 60;

_hour = (_hour + dh) % 24;
_min = (_min + min) % 60;
}

unsigned time24::hour() const
{
return _hour;
}

unsigned time24::min() const
{
return _min;
}

time24 operator+(time24& t, unsigned min)
{
time24 ret(t);
ret.add_min(min);
return ret;
}

time24 operator+(unsigned min, time24& t)
{
return operator+(t, min);
}

bool operator==(time24 const& lhs, time24 const& rhs)
{
return lhs._hour == rhs._hour
&& lhs._min == rhs._min;
}

std::ostream& operator<<(std::ostream& os, time24 const& t)
{
os << "hour=" << t.hour() << ", min=" << t.min() << endl;;
return os;
}

std::istream& operator>>(std::istream& is, time24 &t)
{
cout << "input hour, min:";
is>>t._hour>>t._min;
return is;
}

int main()
{
time24 t1,t2;

cout<<"使用重载输入函数设置t1:"<<endl;
cin>>t1;

cout<<endl<<"使用获得事和分的方法显示t1: "<<endl;
cout<<"hour="<<t1.hour()<<", min="<<t1.min()<<endl;

cout<<endl<<"使用重载输出函数显示t1:"<<endl;
cout<<t1;

cout<<endl<<"使用设置时和分得方法设置t2:"<<endl;
int h,m;
cout<<"请给t2输入小时和分钟:";
cin>>h>>m;
t2.set(h,m);

cout<<endl<<"使用重载输出函数显示t2:"<<endl;
cout<<t2;

cout<<endl<<"测试time24对象与整型分钟数相加:t1=t1+30"<<endl;
t1=t1+30;
cout<<"运算完成后,t1:"<<t1;

cout<<endl<<"测试time24类的等价运算符重载:t1==t2"<<endl;
if(t1==t2)
{
cout<<"t1等价于t2"<<endl;
}
else
{
cout<<"t1不等价于t2"<<endl;
}

cin.get();
cin.get();
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式