注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Aspirer's blog

停止维护,新博客地址:http://aspirer.wang/

 
 
 

日志

 
 

输入当前年月日时分秒时间输出下一秒时间  

2009-07-05 21:34:35|  分类: 学习心得 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

输入当前年月日时分秒时间输出下一秒时间 - aspirer - Aspirers blog

 

#include <iostream>
#include <iomanip>
using namespace std;


typedef struct
{
 int second;
 int minute;
 int hour;
 int day;
 int month;
 int year;
}Time;

Time &GetNextSecond(Time &Now)
{
 int daysofmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

 if (60 == ++Now.second)
 {
  Now.second = 0;
  Now.minute++;
 }
 else
 {
  return Now;
 }

 if (60 == Now.minute)
 {
  Now.minute = 0;
  Now.hour++;
 }
 else
 {
  return Now;
 }

 if (24 == Now.hour)
 {
  Now.hour = 0;
  Now.day++;
 }
 else
 {
  return Now;
 }

 if (Now.day > daysofmonth[Now.month - 1])
 {
  if ((2 == Now.month)          //2月,闰年
   && ((Now.year % 4 == 0 && Now.year % 100 != 0)
   || (Now.year % 400 == 0)))
  {
   if (Now.day > 29)
   {
    Now.day = 1;
    Now.month++;
   }
   else
   {
    return Now;
   }
  }
  else if ((2 == Now.month)          //2月,非闰年
   && !((Now.year % 4 == 0 && Now.year % 100 != 0)
   || (Now.year % 400 == 0)))
  {
   if (Now.day > 28)
   {
    Now.day = 1;
    Now.month++;
   }
   else
   {
    return Now;
   }
  }
  else
  {
   Now.day = 1;
   Now.month++;
  }

  if (Now.month > 12)
  {
   Now.month = 1;
   Now.year++;
  }
  else
  {
   return Now;
  }
 }

 return Now; 
}

int main(void)
{
 Time Now;
 cout << "input current time, format : yy-mo-dd-hh-mi-ss." << endl;
 cin >> Now.year >> Now.month >> Now.day >> Now.hour >> Now.minute >> Now.second;
 Now = GetNextSecond(Now);
  printf("Next second time is %04d-%02d-%02d %02d:%02d:%02d\n",
    Now.year, Now.month, Now.day,
    Now.hour, Now.minute, Now.second);


 return 0;
}


#include <stdio.h>

struct DateTime
{
 int year;
 int month;
 int date;
 int hour;
 int minute;
 int second;
}Now;


int main(void)
{
 printf("Input now time:\nfirstly, year:");
 fflush(stdin);
 scanf("%d", &Now.year);
 while (Now.year < 0)
 {
  printf("Input year error ! you input a year:%d.\n"
    "try another year", Now.year);
  printf("(should great than zero!):");
  fflush(stdin);
  scanf("%d", &Now.year);
 }

 printf("secondly, month:");
 fflush(stdin);
 scanf("%d", &Now.month);
 while (Now.month <= 0 || Now.month > 12)
 {
  printf("Input month error ! you input a month:%d.\n"
    "try another month", Now.month);
  printf("(should >= 1 and <= 12!):");
  fflush(stdin);
  scanf("%d", &Now.month);
 }

 printf("thirdly, date:");
 fflush(stdin);
 scanf("%d", &Now.date);
 while (Now.date <= 0 || Now.date > 28)
 {
  //2月闰年
  if (Now.date > 29 && Now.month == 2 &&
   ((Now.year %4 == 0 && Now.year % 100 != 0)
   || Now.year % 400 == 0))
  {
   printf("Input date error ! you input a date:%d.\n"
     "try another date", Now.date);
   printf("(should >= 1 and <= 29!):");
   fflush(stdin);
   scanf("%d", &Now.date);
   if (Now.date <= 29 && Now.date >= 1)
   {
    break;
   }
  }
  //2月非闰年
  else if (Now.date > 28 && Now.month == 2 &&
    !((Now.year %4 == 0 && Now.year % 100 != 0)
    || Now.year % 400 == 0))
  {
   printf("Input date error ! you input a date:%d.\n"
     "try another date", Now.date);
   printf("(should >= 1 and < 29!):");
   fflush(stdin);
   scanf("%d", &Now.date);
   if (Now.date <= 28 && Now.date >= 1)
   {
    break;
   }
  }
  //其它30天月份
  else if (Now.date > 30 &&
     (Now.month == 4 || Now.month == 6
     || Now.month == 9 || Now.month == 11))
  {
   printf("Input date error ! you input a date:%d.\n"
     "try another date", Now.date);
   printf("(should >= 1 and < 31!):");
   fflush(stdin);
   scanf("%d", &Now.date);
   if (Now.date <= 30 && Now.date >= 1)
   {
    break;
   }
  }
  //其它31天月份
  else if (Now.date > 31)
  {
   printf("Input date error ! you input a date:%d.\n"
     "try another month", Now.date);
   printf("(should >= 1 and <= 31!):");
   fflush(stdin);
   scanf("%d", &Now.date);
   if (Now.date <= 31 && Now.date >= 1)
   {
    break;
   }
  }
  //天数在正常范围
  else
  {
   break;
  }
  
 }

 printf("forth, hour:");
 fflush(stdin);
 scanf("%d", &Now.hour);
 while (Now.hour > 23 || Now.hour < 0)
 {
  printf("Input hour error ! you input a hour:%d.\n"
    "try another hour", Now.hour);
  printf("(should >= 0 and <= 23!):");
  fflush(stdin);
  scanf("%d", &Now.hour);  
 }

 printf("fifth, minute:");
 fflush(stdin);
 scanf("%d", &Now.minute);
 while (Now.minute > 59 || Now.minute < 0)
 {
  printf("Input minute error ! you input a minute:%d.\n"
    "try another minute", Now.minute);
  printf("(should >= 0 and <= 59!):");
  fflush(stdin);
  scanf("%d", &Now.minute);  
 }

 printf("last, second:");
 fflush(stdin);
 scanf("%d", &Now.second);
 while (Now.second > 59 || Now.second < 0)
 {
  printf("Input minute error ! you input a minute:%d.\n"
    "try another minute", Now.second);
  printf("(should >= 0 and <= 59!):");
  fflush(stdin);
  scanf("%d", &Now.second);  
 }

 printf("Now time is %04d-%02d-%02d %02d:%02d:%02d\n",
    Now.year, Now.month, Now.date,
    Now.hour, Now.minute, Now.second);

 if (Now.second == 59)
 {
  Now.second = 0;
 }
 else
 {
  ++Now.second;
 }

 if (Now.second == 0 && Now.minute == 59)
 {
  Now.minute = 0;
 }

 if (Now.minute == 0 && Now.hour == 23)
 {
  Now.hour = 0;
 }

 if (Now.hour == 0 && Now.date >= 28)
 {
  //2月
  if (Now.month == 2)
  {
   //闰年且天数为28号以后
   if(Now.date > 28 &&
    ((Now.year %4 == 0 && Now.year % 100 != 0)
    || Now.year % 400 == 0))
   {
    ++Now.month;
    Now.date = 1;
   }
   //非闰年且天数为28
   else if (Now.date == 28 &&
    !((Now.year %4 == 0 && Now.year % 100 != 0)
    || Now.year % 400 == 0))
   {
    ++Now.month;
    Now.date = 1;
   }
  }
  //其它30天月份
  else if (Now.date == 30 &&
     (Now.month == 4 || Now.month == 6
     || Now.month == 9 || Now.month == 11))
  {
   ++Now.month;
   Now.date = 1;
  }
  //12月31天
  else if (Now.date == 31 && Now.month == 12)
  {
   ++Now.year;
   Now.month = 1;
   Now.date = 1;
  }
  //其它月份31天
  else if (Now.date == 31)
  {
   ++Now.month;
   Now.date = 1;
  }
 }

 printf("Next second time is %04d-%02d-%02d %02d:%02d:%02d\n",
    Now.year, Now.month, Now.date,
    Now.hour, Now.minute, Now.second);

 return 0;
}

  评论这张
 
阅读(799)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018