问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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格式
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
"你是在哪看到我的照片?"怎么说英语 留恋地看着我的照片用英语怎么说? 高德iphone地图下载 苹果手机高德地图怎么下载地图 孕中期适合出游吗 哪些孕妇不适合长途旅行 孕妇坐长途车旅行的好处 孕妇出游的最佳时间 孕晚期适合出游吗 孕妇外出旅行禁忌 小米11烧wifi啥表现 时间的算法 头带大字是什么生肖 函授大专可以直接考研究生吗 大专生可以报函授研究生吗?怎么报名?上课的形式是什么样的?需要多久能拿到证书?和在职的研究生是一样 函授大专能不能考研究生? 装修之前需要做什么准备 装修的具体流程是什么 什么是电视策划 一亿两千万人民币有多重 一亿人民币每张10元有多重 10元是多重? 一张10元纸币的质量是多少克?设计一个实验来准确测量 10元人民币(100元一张的)有多重 260亿人民币有多重 10元人民币一吨多少钱 十元的纸币有多重 十元人民币的重量? 一张现在通用的十元钱人民币的重量是多少? 事业单位招考和公务员考试的内容有什么区别么? 事业单位考试与公务员考试有什么具体区别? 事业单位考试内容跟公务员有区别吗 急求大学网球课准备活动前的小游戏(越新颖越好) 我上体育课时,想在开始的准备活动时做一个有趣的小游戏,请高手指教。(最好不好任何器械) 如何才能使党章内化于心,外化于行 谈谈自己对护理专业学习的想发50字 介绍一下护士的执业范围。(50字内) 腊肠放微波炉里 多久多少火候才好吃 这件衣服属于jk制服吗? 诈骗行为量刑标准有哪些 诈骗犯罪量刑标准是什么 关于诈骗罪量刑标准是什么 诈骗罪的犯罪量刑标准是什么? 诈骗罪共有什么量刑标准 诈骗的量刑标准是什么 吃什么可以升血压,我现在血压是60/80 农业银行云南省临沧市凤庆县勐佑镇有服务点吗 弯刀的作用蚂蚁森林 东莞调控升级 涉及社保延长、提高首付、假离婚等方面 作文 奶酪不见了530字 我的奶酪不见了的作文550个字以下 从前,有个迷宫,住着4个小家伙;守着不知是谁存放的奶酪,过着无忧无虑的生活.有一天,发现奶酪不见了!...