得到下一秒的日期和时间并计算是第几天

推荐方法一,方法二本人所写,给方法一比较简直就是刚学C 而且C也没学好,不会C++,面向对象的思想真不是一撮而就的事啊!我需要更加努力!加油。。。。。。。

方法一:

#include<iostream.h>

#include<stdio.h>

class date

{

private:

int m_nYear, m_nMonth, m_nDay, m_nHour, m_nMin, m_nSec;

int GetDay(int month);

bool IsLeapYear();

 

public:

date(int,int,int,int,int,int);

const date GetNextDate();

void printDate();

int FindDayIndex();

};

date::date(int year, int month, int day, int hour, int min, int sec)

{

this->m_nYear = year;

this->m_nMonth = month;

this->m_nDay = day;

this->m_nHour = hour;

this->m_nMin = min;

this->m_nSec = sec;

}

 

 

bool date::IsLeapYear()

{

if((m_nYear % 400 == 0) || (m_nYear % 4 == 0 && m_nYear % 100 != 0))

  return true;

else

  return false;

}

 

 

int date::GetDay(int month)

{

int day;

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

  day = 31;

  break;

 

case 2:

  if(IsLeapYear())

   day = 29;

  else

   day = 28;

  break;

default:

  day = 30;

}

return day;

}

const date date:: GetNextDate()

{

 

date dat = *this;

if(dat.m_nSec != 59)

  dat.m_nSec ++;

else

{

  dat.m_nSec = 0;

  dat.m_nMin += 1;

}

if(dat.m_nMin == 60)

{

  dat.m_nHour += 1;

  dat.m_nMin = 0;

}

if(dat.m_nHour == 24)

{

  dat.m_nDay += 1;

  dat.m_nHour = 0;

}

if(dat.m_nDay == dat.GetDay(this->m_nMonth) + 1)

{

  dat.m_nDay = 1;

  dat.m_nMonth += 1;

}

if(dat.m_nMonth == 13)

{

  dat.m_nMonth = 1;

  dat.m_nYear += 1;

}

return dat;

}

 

int date::FindDayIndex()

{

int totalDay = 0;

for(int i = 1; i < m_nMonth; i++)

  totalDay += GetDay(i);

totalDay += m_nDay;

return totalDay;

}

void date::printDate()

{

cout<<m_nYear<<"-"<<m_nMonth<<"-"<<m_nDay<<" "<<m_nHour<<":"<<m_nMin

  <<":"<<m_nSec<<endl

  <<"这是"<<m_nYear<<"年的第"<<FindDayIndex()<<""<<endl;

}

int main()

{

cout<<"请输入一个日期,格式如:2002-5-8 11:23:59"<<endl;

int year, month, day, hour, min, sec;

scanf("%d-%d-%d %d:%d:%d",&year, &month, &day, &hour, &min, &sec);

date dat(year, month, day, hour, min, sec);

date nextDat = dat.GetNextDate();

nextDat.printDate();

}

方法二:

#include<iostream>
using namespace std;
bool isleapYear(int year)
{
 if((year%4==0&&year%100!=0)||(year%400==0))
  return true;
 else return false;

}
int getDay(int month,int year)
{
 int day;
 switch(month)
 {
 case 1:
 case 3:
 case 5:
 case 7:
 case 8:
 case 10:
 case 12:  day=31;break;
 case 2:
  if(isleapYear(year))
   day=29;
  else day=28;
  break;
 default: day=30;
 }
 return day;

}
void count(int year,int month,int day)
{

 int Cday=0;
 while(month--!=0)
   Cday+=getDay(month,year);
 Cday+=day;
 cout<<"今天是今年的第"<<Cday<<"天"<<endl;
}
void getNext(int second,int minute,int hour,int day,int month,int year)
{
 if(second!=59)
  second++;
 else
 {
  second=0;
  minute++;
 }
 if(minute==60)
 {
  minute=0;
  hour++;
 }
 if(hour==25)
 {
  hour=1;
  day++;
 }
 if(day==getDay(month,year)+1)
 {
  day=1;
  month++;
 }
 if(month==13)
 {
  month=1;
  year++;
 }
 cout<<year<<"-"<<month<<"-"<<day<<"     "<<hour<<":"<<minute<<":"<<second<<endl;
 count(year,month,day);

}
int main()
{
 int year=2000;
 int month=2;
 int day=28;
 int hour=24;
 int minute=59;
 int second=59;
 getNext(second,minute,hour,day,month,year);
}

原文链接: https://www.cnblogs.com/this-543273659/archive/2011/07/31/2122913.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    得到下一秒的日期和时间并计算是第几天

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/29658

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午7:04
下一篇 2023年2月8日 上午7:06

相关推荐