标签:
定义一个表示时间(包括年、月、日、时、分、秒)的结构体,然后完成下面的功能。(6)求你输入的时间s秒后是何日何时,将结果保存在一个结构体变量中输出;
/*
* Copyright (c) 2014,烟台大学计算机学院
* All right reserved.
* 作者:邵帅
* 文件:temp.cpp
* 完成时间:2014年12月20日
* 版本号:v1.0
*/
#include<iostream>
using namespace std;
struct date
{
int year;
int month;
int day;
int hour;
int min;
int sec;
};
bool year(int y);
int day(int y, int m, int d);
int sec(int h, int m, int s);
int afterday(int y, int m, int d,int n);
int aftersec(int y, int m, int d,int h, int m1, int s,int n);
int main()
{
date date;
int days, secs,allsecs;
int after,after1;
cout << "请输入日期:";
cin >> date.year >> date.month >> date.day;
cout << "请输入时间:";
cin >> date.hour >> date.min >> date.sec;
days = day(date.year, date.month, date.day);
cout << date.year << "年" << date.month << "月" << date.day << "日是这一年的第" << days<< "天。" << endl;
secs = sec(date.hour, date.min, date.sec);
allsecs=((day(date.year,date.month,date.day))*86400)+secs;
cout <<date.hour << ":" << date.min << ":" << date.sec << "是今天的第" << secs << "秒," ;
cout << "今年的第" <<allsecs << "秒。" << endl;
cout<<"请输入天数:";
cin>>after;
cout<<after<<"天后的日期为:";
afterday(date.year,date.month,date.day,after);
cout<<endl;
cout<<"请输入秒数:";
cin>>after1;
cout<<after1<<"秒后的日期为:";
aftersec(date.year, date.month, date.day,date.hour, date.min, date.sec,after1);
}
int day(int y, int m, int d) // 一年中的第几天
{
int sum;
switch (m)
{
case 1:
sum = d;
break;
case 2:
sum = d + 31;
break;
case 3:
sum = d + 59;
break;
case 4:
sum = d + 90;
break;
case 5:
sum = d + 120;
break;
case 6:
sum = d + 151;
break;
case 7:
sum = d + 181;
break;
case 8:
sum = d + 212;
break;
case 9:
sum = d + 243;
break;
case 10:
sum = d + 273;
break;
case 11:
sum = d + 304;
break;
case 12:
sum = d + 334;
break;
}
if (year(y))
{
if (m > 2)
sum--;
}
return sum;
}
int sec(int h, int m, int s) // 一天中的第几秒
{
int secs;
secs = h * 3600 + m * 60 + s;
return secs;
}
int afterday(int y, int m, int d,int n) //d天后的日期
{
int mon[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; //每月天数
while(n--) //循环经过每一天,
{
d++;
if(d>mon[m-1]) //过了一个月
{
m++;
d=1;
}
if(m>12) //过了一年
{
y++;
if(year(y))
mon[1]=29;
else mon[1]=28;
m=1;
}
}
cout<<y<<"年"<<m<<"月"<<d<<"日";
}
int aftersec(int y, int m, int d,int h, int m1, int s,int n) //过了n秒
{
int d1=0;
while (n--)
{
s++;
if (s>60)
{
m1++;
s=1;
}
if (m1>60)
{
h++;
m1=1;
}
if (h>24)
{
d1++;
h=1;
}
}
afterday(y,m,d,d1);
cout<<h<<":"<<m1<<":"<<s<<""<<endl;
}
bool year(int y) //布尔型,判断是否是闰年
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0 && y % 100 == 0))
return true;
else
return false;
}
过程中复杂的是(5)和(6),如何正确处理d天后、s秒后的日期很重要。
int afterday(int y, int m, int d,int n) //d天后的日期
{
int mon[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; //每月天数
while(n--) //循环经过每一天,
{
d++;
if(d>mon[m-1]) //过了一个月
{
m++;
d=1;
}
if(m>12) //过了一年
{
y++;
if(year(y))
mon[1]=29;
else mon[1]=28;
m=1;
}
}
cout<<y<<"年"<<m<<"月"<<d<<"日";
}int aftersec(int y, int m, int d,int h, int m1, int s,int n) //过了n秒
{
int d1=0;
while (n--)
{
s++;
if (s>60)
{
m1++;
s=1;
}
if (m1>60)
{
h++;
m1=1;
}
if (h>24)
{
d1++;
h=1;
}
}
afterday(y,m,d,d1);
cout<<h<<":"<<m1<<":"<<s<<""<<endl;
}可以使用Bool型参数。
bool year(int y) //布尔型,判断是否是闰年
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0 && y % 100 == 0))
return true;
else
return false;
}@ Mayuko
标签:
原文地址:http://blog.csdn.net/mayuko2012/article/details/42043007