你看行不行:
#include <iostream>#include <iomanip>
using namespace std
//Time.h
class Time
{
private:
int hour
int minute
int second
public:
Time(int h=0, int m=0,int s=0)
void setHour(int 升蔽h=0)
int getHour()
void setMinute(int m=0)
吵纳州 int getMinute()
void setSecond(int s=0)
int getSecond()
void print()
}
//Time.cpp
//#include "Time.h"
Time::Time(int h, int m,int s)
{
hour = h
minute = m
second = s
}
void Time::setHour(int h)
{
hour = h
}
int Time::getHour()
{
return hour
}
void Time::setMinute(int m)
{
minute = m
}
int Time::getMinute()
{
return minute
}
void Time::setSecond(int s)
{
second = s
}
int Time::getSecond()
{
茄局 return second
}
void Time::print()
{
cout<<setfill('0')<<setw(2)<<hour<<":"<<setw(2)<<minute<<":"<<setw(2)<<second<<endl
}
int main()
{
Time t1
t1.print()
Time t2(3,5,55)
t2.print()
return 0
}
#include <stdio.h>#include <time.h>
char __new_file_name__[40]
char* getNewFileName()
{
/*
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time )
功能:函物让枯数按照参数fmt所设定格式将time类滑闹型的参数格式化为日期时间信息,然后存储在字符串str中
(至多maxsize 个字符)。用于设定时间不同类型的代码为:
*/
time_t t = time(NULL)
// 年月日
strftime(__new_file_name__,sizeof(__new_file_name__)/sizeof(char),"%Y.%m.%d.txt",gmtime(&t))
// 年月日时分秒
//strftime(__new_file_name__,sizeof(__new_file_name__)/sizeof(char),"罩洞%Y.%m.%d.%H.%M.%S.txt",gmtime(&t))
return __new_file_name__
}
int main(int argc, char *argv[])
{
printf("%s\n",getNewFileName())
return 0
}
#include<iostream>using namespace std
class Time
{
public:
Time(void)
Time(int hour,int minute,int second)
Time operator +(const Time &time)
Time operator -(const Time &time)
friend ostream& operator <<(ostream& out,const Time &time)
friend istream& operator >>(istream& in,const Time &time)
private:
int hour
int minute
int second
}
Time::Time(void)
{
this->hour = 0
this->minute = 0
this->second = 0
}
Time::Time(int hour,int minute,int second)
{
this->hour = hour
this->minute = minute
this->second = second
}
Time Time::operator +(const Time &time)
{
Time newTime
newTime.hour = this->hour + time.hour
newTime.minute = this->minute+ time.minute
newTime.second = this->second + time.second
if(newTime.second >= 60)
{
newTime.second %= 60
newTime.minute += 1
}
if(newTime.minute >= 60)
{
型友悉 newTime.minute %= 60
newTime.hour += 1
}
if(newTime.hour >= 24)
{
newTime.hour %= 24
}
return newTime
}
Time Time::operator -(const Time &time)
{
Time newTime
newTime.hour = this->hour - time.hour
newTime.minute = this->minute - time.minute
newTime.second = this->second - time.second
if(newTime.second <卜乎 0)
{
newTime.second += 60
newTime.minute -= 1
}
if(newTime.minute < 0)
{
newTime.minute += 60
newTime.hour -= 1
}
if(newTime.hour < 0)
{
newTime.hour += 24
}
告卜return newTime
}
ostream& operator <<(ostream& out,const Time &time)
{
out<<time.hour<<":"<<time.minute<<":"<<time.second<<endl
return out
}
istream& operator >>(istream& in,const Time &time)
{
in>>time.hour>>time.minute>>time.second
return in
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)