实验目的:揭开关于你生世的秘密
实验内容:(揭开关于你生世的秘密)
* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:揭开关于你生世的秘密
* 作 者:薛广晨
* 完成日期:2011 年 12 月 19 日
* 版 本号:x1.0
* 对任务及求解方法的描述部分
* 输入描述:定义一个表示时间(包括年、月、日、时、分、秒)的结构体
* 问题描述:(1)输入一个时间(注意各部分数据的取值范围)将输入的时间保存在一个结构体变量中;
(2)输出该日在本年中是第几天(注意闰年问题);
(3)输出这是这一天中的第几秒;
(4)输出这是这一年中的第几秒;(不要认为这个数荒唐,在计算中中需要取随机数时需要一个不会重复的“种子数”,这个秒数是常用的。)
(5)求你输入的时间d 天后是哪年哪月哪日,将结果保存在一个结构体变量中输出;(你的
万天日期靠这个功能了)
(6)求你输入的时间s 秒后是何日何时,将结果保存在一个结构体变量中输出;
程序头部的注释结束(此处也删除了斜杠)
#include<iostream>
using namespace std;
//定义Date结构体
struct Date
{
int year;
int month;
int day;
int hours;
int minute;
int second;
};
Date your_Date;
void cin_Date();
int store_day;
bool judge_year(int year);//声明判断年的函数
void cout_sth();//声明输出函数
void retreat_day(int day1,int select);
void retreat_second();
int main()
{
cin_Date();
judge_year(your_Date.year);
cout_sth();
int day1;
cout<<"请输入后退的天数:"<<endl;
cin>>day1;
retreat_day(day1,1);
retreat_second();
system("PAUSE");
return 0;
}
void cin_Date()
{
int num;
do
{
cout<<"请输入您的出生年份:"<<endl;
cin>>num;
your_Date.year=num;
}while(num>2012||num<1900);
do
{
cout<<"请输入您的出生月份:"<<endl;
cin>>num;
your_Date.month=num;
}while(num<1||num>12);
do
{
cout<<"请输入您的出生日期:"<<endl;
cin>>num;
your_Date.day=num;
}while(num<1||num>31);
do
{
cout<<"请输入您的出生时:"<<endl;
cin>>num;
your_Date.hours=num;
}while(num<1||num>12);
do
{
cout<<"请输入您的出生分:"<<endl;
cin>>num;
your_Date.minute=num;
}while(num<1||num>60);
do
{
cout<<"请输入您的出生秒:"<<endl;
cin>>num;
your_Date.second=num;
}while(num<1||num>60);
}
//定义判断是否为闰年的函数
bool judge_year(int year)
{
bool leap;
if(year%4==0)
{
if(year%1000==0)
{
if(year%400==0)
leap=true;
else leap=false;
}
else
leap=true;
}
else
leap=false;
return leap;
}
//定义输出函数
void cout_sth()
{
int day=0,month=1;
while(month<your_Date.month)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
day=day+31;
month++;
continue;
}
if(month==4||month==6||month==9||month==11)
{
day=day+30;
month++;
continue;
}
if(month==2)
{
if(judge_year(your_Date.year))
day=day+29;
else
day=day+28;
month++;
continue;
}
}
day=day+your_Date.day;
store_day=day;
cout<<"该日在本年中是第"<<day<<"天。"<<endl;
int second=0;
second=(your_Date.hours*60+your_Date.minute)*60+your_Date.second;
cout<<"这是这一天中的第"<<second<<"秒。"<<endl;
second=second+(day-1)*24*60*60;
cout<<"这是这一年中的第"<<second<<"秒。"<<endl;
}
//定义void retreat_day()函数;
void retreat_day(int day1,int select)
{
int day,month;
bool pass;
Date store;
store=your_Date;
day=store_day+day1;
do{
pass=false;
if(judge_year(store.year))
{
if(day-366>0)
{
pass=true;
store.year+=1;
day-=366;
}
}
else
{
if(day-365>0)
{
pass=true;
store.year+=1;
day-=365;
}
}
}while(pass);
if(day==0)day=1;
month=1;
do{
pass=false;
if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day-31>0)
{
pass=true;
day-=31;
month++;
}
if((month==4||month==6||month==9||month==11)&&day-30>=0)
{
pass=true;
day-=30;
month++;
}
if(month==2)
{
if(judge_year(store.year))
{
if(day-29>0)
{
pass=true;
day-=29;
month++;
}
}
else
{
if(day-28>=0)
{
pass=true;
day-=28;
month++;
}
}
}
}while(pass);
store.month=month;
if(select==1)
{
cout<<"过"<<day1<<"天后是"<<store.year<<"年"<<store.month<<"月"<<day<<"日。"<<endl;
}
else
{
cout<<"过"<<select<<"秒后是"<<store.year<<"年"<<store.month<<"月"<<day<<"日"<<endl;
}
}
//定义retreat_second();
void retreat_second()
{
Date store;
int day,second;
cout<<"请输入后退的秒数:"<<endl;
cin>>second;
day=second/(24*3600);
retreat_day(day,second);
second=second%(24*3600);
store.hours=second/3600;
second=second%60;
store.minute=second/60;
second=second%3600;
cout<<store.hours<<"时"<<store.minute<<"分"<<second<<"秒。"<<endl;
}
经验积累:
1.要有耐心
上机感言:这个程序太长了,有点乱,感觉很麻烦,头晕脑涨啊。