c++关于时间类的编程题
定义一个时间类time,有三个成员变量hour,minute,second,定义构造函数,析构函数以及用于改变,获取,输出时间信息的公有函数,主函数中定义时间对象,并通过...
定义一个时间类time,有三个成员变量hour,minute,second,定义构造函数,析构函数以及用于改变,获取,输出时间信息的公有函数,主函数中定义时间对象,并通过各种成员函数完成时间的设定,改变,获取,输出等功能。
可以在200内无限提高财富值 展开
可以在200内无限提高财富值 展开
2个回答
展开全部
#include <iostream>
using namespace std;
class Clock
{
public:
Clock() : hour(0),minute(0),second(0){}
Clock(int h, int m, int s):hour(h),minute(m),second(s){}
~Clock(){}
void setTime(int h, int m, int s);
Clock getTime();
friend Clock operator+ (const Clock &clock, const int second);
void print();
private:
int hour;
int minute;
int second;
};
void Clock::setTime(int h, int m, int s)
{
if (h >= 0 && h <= 24)
{
hour = h;
}
if (m >= 0 && m <= 59)
{
minute = m;
}
if (s >= 0 && s <= 59)
{
second = s;
}
}
Clock Clock::getTime()
{
return *this;
}
Clock operator+ (const Clock &clock, const int second)
{
Clock newclock(clock);
int m, h;
newclock.second += second;
if (newclock.second >= 60)
{
m = newclock.second / 60;
newclock.second = newclock.second % 60;
newclock.minute = newclock.minute + m;
if (newclock.minute >= 60)
{
h = newclock.minute / 60;
newclock.minute = newclock.minute % 60;
newclock.hour = newclock.hour + h;
if (newclock.hour >= 24)
{
newclock.hour = newclock.hour % 24;
}
}
}
if (newclock.second < 0)
{
m = newclock.second / 60 - 1;
newclock.second = newclock.second % 60 + 60;
newclock.minute = newclock.minute + m;
if (newclock.minute < 0)
{
h = newclock.minute / 60 - 1;
newclock.minute = newclock.minute % 60 + 60;
newclock.hour = newclock.hour + h;
if (newclock.hour < 0)
{
newclock.hour = newclock.hour % 24 + 24;
}
}
}
return newclock;
}
Clock operator- (const Clock &clock, const int second)
{
Clock newclock(clock + (-second));
return newclock;
}
void Clock::print()
{
cout << hour << ":" << minute << ":" << second << endl;
}
int main()
{
Clock c1(23, 59, 59);
Clock c2, c3;
cout << "c1: ";
c1.print();
c2.setTime(2, 5, 58);
cout << "c2: ";
c2.print();
c3 = c2.getTime();
cout << "c3: ";
c3.print();
cout << "c1+4: ";
(c1+64).print();
cout << "c1-4: ";
(c1-120).print();
return 0;
}
using namespace std;
class Clock
{
public:
Clock() : hour(0),minute(0),second(0){}
Clock(int h, int m, int s):hour(h),minute(m),second(s){}
~Clock(){}
void setTime(int h, int m, int s);
Clock getTime();
friend Clock operator+ (const Clock &clock, const int second);
void print();
private:
int hour;
int minute;
int second;
};
void Clock::setTime(int h, int m, int s)
{
if (h >= 0 && h <= 24)
{
hour = h;
}
if (m >= 0 && m <= 59)
{
minute = m;
}
if (s >= 0 && s <= 59)
{
second = s;
}
}
Clock Clock::getTime()
{
return *this;
}
Clock operator+ (const Clock &clock, const int second)
{
Clock newclock(clock);
int m, h;
newclock.second += second;
if (newclock.second >= 60)
{
m = newclock.second / 60;
newclock.second = newclock.second % 60;
newclock.minute = newclock.minute + m;
if (newclock.minute >= 60)
{
h = newclock.minute / 60;
newclock.minute = newclock.minute % 60;
newclock.hour = newclock.hour + h;
if (newclock.hour >= 24)
{
newclock.hour = newclock.hour % 24;
}
}
}
if (newclock.second < 0)
{
m = newclock.second / 60 - 1;
newclock.second = newclock.second % 60 + 60;
newclock.minute = newclock.minute + m;
if (newclock.minute < 0)
{
h = newclock.minute / 60 - 1;
newclock.minute = newclock.minute % 60 + 60;
newclock.hour = newclock.hour + h;
if (newclock.hour < 0)
{
newclock.hour = newclock.hour % 24 + 24;
}
}
}
return newclock;
}
Clock operator- (const Clock &clock, const int second)
{
Clock newclock(clock + (-second));
return newclock;
}
void Clock::print()
{
cout << hour << ":" << minute << ":" << second << endl;
}
int main()
{
Clock c1(23, 59, 59);
Clock c2, c3;
cout << "c1: ";
c1.print();
c2.setTime(2, 5, 58);
cout << "c2: ";
c2.print();
c3 = c2.getTime();
cout << "c3: ";
c3.print();
cout << "c1+4: ";
(c1+64).print();
cout << "c1-4: ";
(c1-120).print();
return 0;
}
展开全部
time.h:
class time {
public:
time() {
hour=minute=second=0;
};
bool setTime(int h,int m,int s);
bool setHour(int h);
bool setMinute(int m);
bool setSecond(int s);
int const getHour();
int const getMinute();
int const getSecond();
void printTime();
~time() {
};
private:
int hour,minute,second;
};
-----------------------------------------------------------------------------------------------------------
time.cpp:
#include <iostream>
#include "time.h"
bool time::setTime(int h,int m,int s) {
if ((h>=0&&h<24)&&(m>=0&&m<60)&&(s>=0&&s<60)) {
hour=h;
minute=m;
second=s;
return true;
} else
return false;
};
bool time::setHour(int h) {
if (h>=0&&h<24) {
hour=h;
return true;
} else
return false;
};
bool time::setMinute(int m) {
if (m>=0&&m<60) {
minute=m;
return true;
} else
return false;
};
bool time::setSecond(int s) {
if (s>=0&&s<60) {
second=s;
return true;
} else
return false;
};
int const time::getHour() {
return hour;
};
int const time::getMinute() {
return minute;
};
int const time::getSecond() {
return second;
};
void time::printTime() {
if (hour<10)
std::cout<<"0"<<hour;
else
std::cout<<hour;
std::cout<<":";
if (minute<10)
std::cout<<"0"<<minute;
else
std::cout<<minute;
std::cout<<":";
if (second<10)
std::cout<<"0"<<second;
else
std::cout<<second;
std::cout<<std::endl;
};
-----------------------------------------------------------------------------------------------------------
main.cpp:
#include <iostream>
#include "time.h"
int main (void) {
time t;
t.setTime(12,0,0);
t.printTime();
t.setHour(13);
t.printTime();
t.setMinute(30);
t.printTime();
t.setSecond(30);
t.printTime();
if (t.setHour(30))
t.printTime();
else
std::cout<<"setHour to 30: fail!"<<std::endl;
std::cout<<t.getHour()<<":"<<t.getMinute()<<":"<<t.getSecond()<<std::endl;
return 0;
}
class time {
public:
time() {
hour=minute=second=0;
};
bool setTime(int h,int m,int s);
bool setHour(int h);
bool setMinute(int m);
bool setSecond(int s);
int const getHour();
int const getMinute();
int const getSecond();
void printTime();
~time() {
};
private:
int hour,minute,second;
};
-----------------------------------------------------------------------------------------------------------
time.cpp:
#include <iostream>
#include "time.h"
bool time::setTime(int h,int m,int s) {
if ((h>=0&&h<24)&&(m>=0&&m<60)&&(s>=0&&s<60)) {
hour=h;
minute=m;
second=s;
return true;
} else
return false;
};
bool time::setHour(int h) {
if (h>=0&&h<24) {
hour=h;
return true;
} else
return false;
};
bool time::setMinute(int m) {
if (m>=0&&m<60) {
minute=m;
return true;
} else
return false;
};
bool time::setSecond(int s) {
if (s>=0&&s<60) {
second=s;
return true;
} else
return false;
};
int const time::getHour() {
return hour;
};
int const time::getMinute() {
return minute;
};
int const time::getSecond() {
return second;
};
void time::printTime() {
if (hour<10)
std::cout<<"0"<<hour;
else
std::cout<<hour;
std::cout<<":";
if (minute<10)
std::cout<<"0"<<minute;
else
std::cout<<minute;
std::cout<<":";
if (second<10)
std::cout<<"0"<<second;
else
std::cout<<second;
std::cout<<std::endl;
};
-----------------------------------------------------------------------------------------------------------
main.cpp:
#include <iostream>
#include "time.h"
int main (void) {
time t;
t.setTime(12,0,0);
t.printTime();
t.setHour(13);
t.printTime();
t.setMinute(30);
t.printTime();
t.setSecond(30);
t.printTime();
if (t.setHour(30))
t.printTime();
else
std::cout<<"setHour to 30: fail!"<<std::endl;
std::cout<<t.getHour()<<":"<<t.getMinute()<<":"<<t.getSecond()<<std::endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询