C++时间计算程序
发布网友
发布时间:2022-06-05 09:23
我来回答
共6个回答
热心网友
时间:2023-11-15 12:19
没做完,再等等
#include <iostream>
using namespace std;
class time
{
private:
short int hour;
short int minute;
short int sec;
short int year;
short int month;
short int day;
bool am , pm , _12_24 , AD ;
public:
time()
{
SetYear();
SetMonth();
SetDay();
SetHour();
SetMinute();
SetSec();
cout << "\n\n" ;
_12_24 = true;
}
void SetYear()
{
a:
cout << "请输入年:";
cin >> year;
if (year == 0 )
{
cout << "年份不得为0,请重新输入!" ;
goto a;
}
else if (year > 0 )
{
AD = true;
}
else if (year < 0 )
{
AD = false ;
}
}
void SetMonth()
{
a:
cout << "请输入月:";
cin >> month;
if (month > 12)
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void SetDay()
{
int i ;
if (year % 4 == 0 )
{
i = 29;
}
else
{
i = 28 ;
}
const int c[] = {31,i,31,30,31,30,31,31,30,31 ,30,31};
for (i = 0 ;i < (month-1) ;i++)
{
;
}
a:
cout << "请输入日:";
cin >> day;
if (day > c[i])
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void SetHour()
{
a:
cout << "请输入时:";
cin >> hour;
if (hour > 24)
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void SetMinute()
{
a:
cout << "请输入分:";
cin >> minute;
if (minute > 60)
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void SetSec()
{
a:
cout << "请输入秒:";
cin >> sec;
if (sec > 60)
{
cout << "输入错误,请重新输入!" << endl;
goto a;
}
}
void display()
{
cout << endl ;
if (year < 0 )
AD = false;
else
AD = true;
if (AD == true)
{
cout << "公元" << year;
}
else if (AD == false)
{
double temp;
temp = (double)year;
temp = abs(temp);
year = (int)temp;
cout << "公元前" << year;
}
cout <<"年" << month << "月" << day << "日" << endl;
if (_12_24 != true)
{
if (am == true)
{
cout << "上午" ;
}
else if (pm == true)
{
cout << "下午" ;
}
}
cout << hour << "时" << minute << "分" << sec << "秒" << endl;
}
void conversion()
{
int flag;
cout <<endl << "\t\t\t1.转换为24小时." << endl
<< "\t\t\t2.转换为12小时." << endl << "请选择:" ;
cin >> flag;
switch(flag)
{
case 1:
{
if (_12_24 != true )
{
_12_24 = true;
am = pm = false;
hour += 12;
}
else
{
break;
}
}
case 2:
{
if (_12_24 != false)
{
_12_24 = false;
if (hour >= 12 )
{
pm = true;
am = false;
hour -= 12;
break ;
}
if (hour < 12)
{
pm = false;
am = true;
break ;
}
}
else
{
break;
}
}
}
}
bool compare(time a)
{
if (year > a.ViewYear() )
{
return true;
}
else
{
if (month > a.ViewMonth() )
{
return true;
}
else
{
if (day > a.ViewDay() )
{
return true;
}
else
{
if (hour > a.ViewHour() )
{
return true;
}
else
{
if (minute > a.ViewMinute())
{
return true;
}
else
{
if (sec > a.ViewSec())
{
return true;
}
else
{
return false;
}
}
}
}
}
}
}
void operator + (int a )
{
int flag , i;
if (year % 4 == 0 )
{
i = 29;
}
else
{
i = 28 ;
}
const int c[] = {31,i,31,30,31,30,31,31,30,31 ,30,31};
for (i = 0 ;i < (month-1);i++)
{
;
}
cout << "\t\t\t1.给年加" << endl << "\t\t\t2.给月加" << endl
<< "\t\t\t3.给日加" << endl << "\t\t\t4.给时加" << endl
<< "\t\t\t5.给分加" << endl <<"\t\t\t6.给秒加" <<endl
<< "请选择:";
cin >> flag;
switch(flag)
{
case 1:
{
year += a ;
if (AD == false && year >= 0 )
{
AD = true ;
year += 1;
}
break;
}
case 2:
{
month += a;
if (month >= 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 0 ;
}
break;
}
case 3:
{
int temp;
day += a;
if (day >= c[i])
{
month += 1 ;
day -= c[i];
if (month > 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 1 ;
}
}
break;
}
case 4:
{
int temp;
hour += a ;
if (hour >= 24)
{
day += 1;
hour = 0;
if (day >= c[i])
{
month += 1 ;
day -= c[i];
if (month > 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 1 ;
}
}
}
break;
}
case 5:
{
int temp;
minute += a ;
if (minute >= 60)
{
hour += 1 ;
if (hour >= 24)
{
day += 1;
hour = 0;
if (day >= c[i])
{
month += 1 ;
day -= c[i];
if (month > 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 1 ;
}
}
}
}
break;
}
case 6:
{
int temp;
sec += a;
if (sec >= 60 )
{
minute += 1 ;
sec = 0 ;
if (minute >= 60)
{
hour += 1 ;
if (hour >= 24)
{
day += 1;
hour = 0;
if (day >= c[i])
{
month += 1 ;
day -= c[i];
if (month > 12 )
{
year += 1 ;
if (AD == false && year >= 0 )
{
year += 1;
AD == true;
}
month = 1 ;
}
}
}
}
}
}
break;
}
}
void operator - (int a )
{
int flag;
cout << "\t\t\t1.给年减" << endl << "\t\t\t2.给月减" << endl
<< "\t\t\t3.给日减" << endl << "\t\t\t4.给时减" << endl
<< "\t\t\t5.给分减" << endl << "\t\t\t6.给秒减" <<endl
<<"请选择:";
cin >> flag;
switch(flag)
{
case 1:
{
year -= a;
if (year <= 0 && AD == true)
{
year -= 1;
AD = false ;
}
break;
}
case 2:
{
if (a < month)
month -= a;
else
{
year -= 1 ;
if (year <= 0 && AD == true)
{
year -= 1;
AD = false ;
}
if (a < 12 )
month = 12 - (a - month ) ;
else
cout <<"您输入的数大于或者等于12,请直接用年来减";
}
break;
}
case 3:
{
int i ;
if (year % 4 == 0 )
{
i = 29;
}
else
{
i = 28 ;
}
const int c[] = {31,i,31,30,31,30,31,31,30,31 ,30,31};
for (i = 0 ;i < (month-1) ;i++)
{
;
}
if (day >= c[i])
{
cout << "您输入的数大于或者等于了这一月的天数,请直接用月减";
break;
}
if (a < day)
day -= a;
else
{
month -= 1;
if (month == 0 )
{
month = 12 ;
year -= 1 ;
if (year <= 0 && AD == true )
{
year -= 1;
}
}
day = c[i] - (a - day) - 1 ;
}
break;
}
case 4:
{
if (a <= hour)
hour -= a ;
else
{
}
break;
}
case 5:
{
if (a <= minute)
minute -= a ;
else
{
}
break;
}
case 6:
{
if (a <= sec)
sec -= a;
else
{
}
break;
}
}
}
};
/*
2、重载”+”、”-”运算,日期时间加上天数和时间等于新的日期时间,日期时间
减去天数和时间等于新的日期时间;(考虑闰年的情况)
*/
热心网友
时间:2023-11-15 12:19
有三个文件
//1、DealTime.h
#ifndef CAESAR_DEALTIME_H__DEF
#define CAESAR_DEALTIME_H__DEF
#include <time.h>
//this is a class based on second,that can handle time operater
class CDealTime
{
public:
CDealTime(); //use current time
CDealTime(time_t tm); //use time_t time
CDealTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec); //use specif time
virtual ~CDealTime();
public: //
static CDealTime GetLocalTime();
public: //for get value
time_t GetTime() const; //gain time_t value
int GetYear() const; //gain year
int GetMonth() const; //gain month
int GetDay() const; //gain day
int GetHour() const; //gain hour
int GetMinute() const; //gain minute
int GetSecond() const; //gain second
int GetDayOfWeek() const; //gain the current day of week(from 1 to 7)
int GetDayOfMonth() const; //gain the last days of current month
int GetDayOfYear() const; //gain the last days of current year(from 1.1)
public: //for operator
CDealTime& operator=(const CDealTime& timesrc);
CDealTime& operator=(const time_t& timesrc);
CDealTime operator+(const CDealTime& tm);
CDealTime operator+(const time_t tm);
CDealTime operator-(const CDealTime& tm);
CDealTime operator-(const time_t tm);
CDealTime& operator+=(const CDealTime& tm);
CDealTime& operator+=(const time_t tm);
CDealTime& operator-=(const CDealTime& tm);
CDealTime& operator-=(const time_t tm);
int operator==(const CDealTime& tm) const;
int operator!=(const CDealTime& time ) const;
int operator>(const CDealTime& time ) const;
int operator>=(const CDealTime& time ) const;
int operator<(const CDealTime& time ) const;
int operator<=(const CDealTime& time ) const;
protected:
time_t m_time;
};
#endif
//2、DealTime.cpp
// DealTime.cpp: implementation of the CDealTime class.
//
//////////////////////////////////////////////////////////////////////
#include "DealTime.h"
#include <assert.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDealTime::CDealTime()
{
m_time = time(NULL);
}
CDealTime::CDealTime(time_t tm)
{
m_time = tm;
}
CDealTime::CDealTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec)
{
struct tm atm;
atm.tm_sec = nSec;
atm.tm_min = nMin;
atm.tm_hour = nHour;
assert(nDay >= 1 && nDay <= 31);
atm.tm_mday = nDay;
assert(nMonth >= 1 && nMonth <= 12);
atm.tm_mon = nMonth - 1; // tm_mon is 0 based
assert(nYear >= 1900);
atm.tm_year = nYear - 1900; // tm_year is 1900 based
atm.tm_isdst = -1;
m_time = mktime(&atm);
assert(m_time != -1);
}
CDealTime::~CDealTime()
{
}
CDealTime CDealTime::GetLocalTime()
{
return CDealTime(time(NULL));
}
time_t CDealTime::GetTime() const
{
return m_time;
}
int CDealTime::GetYear() const
{
return localtime(&m_time)->tm_year + 1900; //it's year since 1900
}
int CDealTime::GetMonth() const
{
return localtime(&m_time)->tm_mon + 1; //it's mon base on 0
}
int CDealTime::GetDay() const
{
return localtime(&m_time)->tm_mday;
}
int CDealTime::GetHour() const
{
return localtime(&m_time)->tm_hour;
}
int CDealTime::GetMinute() const
{
return localtime(&m_time)->tm_min;
}
int CDealTime::GetSecond() const
{
return localtime(&m_time)->tm_sec;
}
int CDealTime::GetDayOfWeek() const
{
return localtime(&m_time)->tm_wday+1;
}
int CDealTime::GetDayOfMonth() const
{
return localtime(&m_time)->tm_mday;
}
int CDealTime::GetDayOfYear() const
{
return localtime(&m_time)->tm_yday+1;
}
CDealTime& CDealTime::operator=(const CDealTime& timesrc)
{
m_time = timesrc.m_time;
return *this;
}
CDealTime& CDealTime::operator=(const time_t& timesrc)
{
m_time = timesrc;
return *this;
}
CDealTime CDealTime::operator+(const CDealTime& tm)
{
return CDealTime(m_time + tm.m_time);
}
CDealTime CDealTime::operator+(const time_t tm)
{
return CDealTime(m_time + tm);
}
CDealTime CDealTime::operator-(const CDealTime& tm)
{
return CDealTime(m_time - tm.m_time);
}
CDealTime CDealTime::operator-(const time_t tm)
{
return CDealTime(m_time - tm);
}
CDealTime& CDealTime::operator+=(const CDealTime& tm)
{
m_time += tm.m_time;
return *this;
}
CDealTime& CDealTime::operator+=(const time_t tm)
{
m_time += tm;
return *this;
}
CDealTime& CDealTime::operator-=(const CDealTime& tm)
{
m_time -= tm.m_time;
return *this;
}
CDealTime& CDealTime::operator-=(const time_t tm)
{
m_time -= tm;
return *this;
}
int CDealTime::operator==(const CDealTime& tm) const
{
return (m_time == tm.m_time);
}
int CDealTime::operator!=(const CDealTime& time ) const
{
return(m_time != time.m_time);
}
int CDealTime::operator>(const CDealTime& time ) const
{
return (m_time>time.m_time);
}
int CDealTime::operator>=(const CDealTime& time ) const
{
return (m_time>=time.m_time);
}
int CDealTime::operator<(const CDealTime& time ) const
{
return (m_time<time.m_time);
}
int CDealTime::operator<=(const CDealTime& time ) const
{
return (m_time<=time.m_time);
}
//3、testdealtime.cpp
//测试代码
// testdealtime.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include "DealTime.h"
int main(int argc, char* argv[])
{
CDealTime tm = CDealTime::GetLocalTime(); //
CDealTime tm2; //
int year = tm.GetYear();
int month = tm.GetMonth();
int day = tm.GetDay();
int hour = tm.GetHour();
int minute=tm.GetMinute();
int second = tm.GetSecond();
printf("now is %04u-%02u-%02u %02u:%02u:%02u\n",year,month,day,hour,minute,second);
//the time passed
tm2 = tm + 60*60*25;
year = tm2.GetYear();
month = tm2.GetMonth();
day = tm2.GetDay();
hour = tm2.GetHour();
minute =tm2.GetMinute();
second = tm2.GetSecond();
printf("after 60*60*25 second data is %04u-%02u-%02u %02u:%02u:%02u\n",year,month,day,hour,minute,second);
CDealTime tm3(2004,12,31,23,62,65);
year = tm3.GetYear();
month = tm3.GetMonth();
day = tm3.GetDay();
hour = tm3.GetHour();
minute =tm3.GetMinute();
second = tm3.GetSecond();
printf("tm3 is %04u-%02u-%02u %02u:%02u:%02u\n",year,month,day,hour,minute,second);
getchar();
return 0;
}
参考资料:这个程序很完美吧,请给分,谢谢合作
热心网友
时间:2023-11-15 12:20
晕了,被骗了,还以为是内核级的...... -_-!
热心网友
时间:2023-11-15 12:21
阳历没有闰日或农历没有闰月的年份。阳历平年365天,农历平年354天或355天。
或者农作物收成很平常的年份。
公元年数可被4整除为闰年,但是正百的年数必须是可以被400整除的菜是闰年。其他都是平年。闰年的2月有29天。例如:1996年是闰年,2000年是闰年,而平年二月有28天,1900年是平年,1997年是平年。
而农历有闰月的年份称为闰年
闰年: 1.为了弥补人为的年份规定与地球实际绕日公转的时间差,2.而人为把时间差补上了的年份,该年即为闰年.
遵循的规律为: 四年一闰,百年不闰,四百年再闰.
if((year % 400 == 0)||(year % 4 == 0)&&(year % 100 != 0))闰年的计算方法
热心网友
时间:2023-11-15 12:21
//我会转换。
//注意: 此处12点算上午.
//注意: 这里文本可全部拷走,包括注释.
//程序如下
//24 to 12
void function1(int hh, mm, ss)
{
if (hh>12) printf("p.m. %d, %d, %d /n", hh-12, mm, ss);
if (hh<=12) printf("a.m. %d, %d, %d" /n", hh, mm, ss);
};
//12 to 24
//updown为上下午,等于0为上午,等于1为下午
void function2(int updown, hh, mm, ss)
{
if (updown=1) hh=hh+12;
printf("a.m. %d, %d, %d" /n", hh, mm, ss);
};
//Main program
void main()
{
int name, updown;
printf("24 or 12?");
scanf(" %d /n", name);
if (name==24)
{
scanf(" %d,%d,%d /n", hh, mm, ss);
function1(hh, mm, ss);
};
if (name==12)
{
scanf(" %d,%d,%d,%d /n", updown, hh, mm, ss);
function2(updown, hh, mm, ss);
};
};
热心网友
时间:2023-11-15 12:22
只是时间转换,而且为c格式