C++用结构体实现:要求输入年、月、日、时、分、秒。输出该时间的下一秒

例子:
输入2007年12月31日23时59分59秒
输出2008年1月1日0时0分0秒
用结构体实现,这是我的程序....
但是输出的结果不正确

#include <iostream>
using namespace std;
struct datatime
{
int year;
int month;
int day;
int hour;
int minute;
int second;
};
struct datatime data_1;
struct datatime *p;
int main()
{
int days[13]={365,31,28,31,30,31,30,31,31,30,31,30,31};
p=&data_1;
cout<<"请依次输入年、月、日、时、分、秒:"<<endl;
cin>>p->year>>p->month>>p->day>>p->month>>p->hour>>p->day;
if(p->second<59)
p->second++;
else
{
p->second=0;
cout<<p->second<<endl;
if(p->minute<59)
p->minute++;
else
{
p->minute=0;
if(p->hour<23)
p->hour++;
else
{
p->hour=0;
if(2==p->month&&(p->year%400==0)||(p->year%4==0&&p->year%400!=0))
days[p->month]=29;
if(p->day<days[p->month])
p->day++;
else
{
p->day=1;
if(p->month<12)
p->month++;
else
{
p->month=1;
p->year++;
}
}
}
}
}
cout<<p->year<<" "<<p->month<<" "<<p->day<<" "<<p->hour<<" "<<p->minute<<" "<<p->second<<endl;
system ("pause");
return 0;
}

赋值语句变量名字有问题:

 cin>>p->year>>p->month>>p->day>>p->month>>p->hour>>p->day;
                                   月改时             日改秒

改成:

 cin>>p->year>>p->month>>p->day>>p->hour>>p->minute>>p->second;

 试过了吗,还有什么问题

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-26
cin>>p->year>>p->month>>p->day>>p->month>>p->hour>>p->day;
最后一个应该是秒。。其他问题没发现,你先看看追问

改了,还是不行

第2个回答  2013-09-26
#include<stdio.h>
#include<stdlib.h>

int year;
int month;
int day;
int hour;
int minute;
int second;

void InputTime();
void UpdateTime();
void OutputTime();
int TimeCheck();
int LeapYear();
int DayBound();//返回一个月的日期上限

void InputTime()
{
system("clear");
printf("请按提示输入一个时间\n");
printf("请输入年份\n");
scanf("%d",&year);
printf("请输入月份\n");
scanf("%d",&month);
printf("请输入日期\n");
scanf("%d",&day);
printf("请输入小时数\n");
scanf("%d",&hour);
printf("请输入分钟数\n");
scanf("%d",&minute);
printf("请输入秒数\n");
scanf("%d",&second);
}

int TimeCheck()
{
if(month >0 && month <=12)//月份检查
{
if(day >0 && day <= DayBound())//日期检查
{
if(hour >=0 && hour <=23)//小时检查
{
if(minute >=0 && minute <=59)//分钟检查
{
if(second >=0 && second <=59)//秒数检查
return 1;
else
return 0;
}
else
return 0;
}
else
return 0;
}
else
return 0;
}
else
return 0;
}

int DayBound() //返回一个月的日期上限
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;break;
case 4:
case 6:
case 9:
case 11:return 30;break;
case 2:{
switch(LeapYear())
{
case 1:return 29;break;
case 0:return 28;break;
}
}
}
}

int LeapYear() //判断闰年的函数
{
if(0 == year%4)
{
if(0 == year%100)
{
if(0 == year%400)
return 1;
else
return 0;
}
else
return 1;
}
else
return 0;
}

void UpdateTime()
//得到输入时间的下一秒
{
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
day++;
if(day > DayBound())
{
day =1;
month++;
if(month==13)
{
month=1;
year++;
}
}
}
}
}
}

void OutputTime()//输出所得的时间
{
printf("所输入的时间的下一秒是:\n");
printf("%d年 %d月 %d日 %02d:%02d:%02d\n",year,month,day,hour,minute,second);
}

int main()
{
do
{
InputTime();
} while(0 ==TimeCheck()); //检查输入时间的合法性
UpdateTime(); //得到输入时间的下一秒
OutputTime(); //输出得到的结果
return 0;
}
相似回答