計算兩個日期之間相差多少天。
但是我只能想到它可以用來計算一個人從出生到今天活了多少天……
非常不成熟的代碼,有些地方比較亂。
並且爲了判斷輸入花費了不少的功夫,但還是不完美。
- #include <iostream>
- #include <conio.h>
- #define LEAPYEAR 366//閏年天數
- #define COMMONYEAR 365//平年天數
- #define LEAPFEBRUARY 29//閏年二月天數
- using namespace std;
- int main(int argc,char *argv[])
- {
- int brithYear,brithMonth,brithDay,signBrithYear=0; //出生日期和它的平閏狀態
- int nowYear,nowMonth,nowDay,signNowYear=0; //今天的日期和今年的平閏狀態
- long long allDayLive=0;//相差的天數
- int tempYear,tempMonth;
- //每個月的天數
- int brithMonthDay[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
- int nowMonthDay[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
- //限定到公元2200年只是一個提醒,實際上沒有這個限制
- cout<<"*********************************************"<<endl;
- cout<<"警告!請不要輸入數字或空格以外的任何其他字符!"<<endl;
- cout<<"注意!年份的有效範圍爲公元1年到公元2200年!"<<endl;
- cout<<"*********************************************\n"<<endl;
- //輸入數據
- cout<<"請輸入你的出生日期,格式:yyyy mm dd"<<endl;
- cin>>brithYear>>brithMonth>>brithDay;
- cout<<"\n請輸入今天的日期,格式:yyyy mm dd"<<endl;
- cin>>nowYear>>nowMonth>>nowDay;
- //判斷出生年與今年的平閏狀態,修正二月的天數
- if(brithYear%100!=0&&brithYear%4==0||brithYear%400==0)
- {
- signBrithYear=1;
- brithMonthDay[2]=LEAPFEBRUARY;
- }
- if(nowYear%100!=0&&nowYear%4==0||nowYear%400==0)
- {
- signNowYear=1;
- nowMonthDay[2]=LEAPFEBRUARY;
- }
- //判斷輸入的日期是否規範有效
- if(
- brithYear<=0||
- brithMonth<=0||
- brithDay<=0||
- nowYear<=0||
- nowMonth<=0||
- nowDay<=0||
- brithYear>9999||//限定最大年份
- nowYear>9999||
- brithMonth>12||
- nowMonth>12||
- brithYear>nowYear
- )
- {
- cout<<"\n輸入錯誤!按(Enter)退出程序。"<<endl;
- getch();
- return -1;
- }
- if(brithYear==nowYear)
- {
- if(brithMonth>nowMonth)
- {
- cout<<"\n輸入錯誤!按(Enter)退出程序。"<<endl;
- getch();
- return -1;
- }
- }
- if(brithYear==nowYear)
- {
- if(brithMonth==nowMonth)
- {
- if(brithDay>nowDay)
- {
- cout<<"\n輸入錯誤!按(Enter)退出程序。"<<endl;
- getch();
- return -1;
- }
- }
- }
- if(brithDay>brithMonthDay[brithMonth]||nowDay>nowMonthDay[nowMonth])
- {
- cout<<"\n輸入錯誤!按(Enter)退出程序。"<<endl;
- getch();
- return -1;
- }
- //感覺計算方法不是特別好,可能有許多可以優化的地方
- //如果兩個日期不同年
- if(brithYear!=nowYear)
- {
- //計算中間相隔的整年的天數
- for(tempYear=brithYear+1;tempYear<nowYear;tempYear++)
- {
- if(tempYear%100!=0&&tempYear%4==0||tempYear%400==0)
- {
- allDayLive+=LEAPYEAR;
- }
- else
- {
- allDayLive+=COMMONYEAR;
- }
- }
- //計算出生那年過的天數
- if(brithMonth==1)
- {
- if(signBrithYear==1)
- {
- allDayLive+=LEAPFEBRUARY;
- }
- }
- for(tempMonth=brithMonth+1;tempMonth<=12;tempMonth++)
- {
- allDayLive+=brithMonthDay[tempMonth];
- }
- if(brithMonth==2)
- {
- if(signBrithYear==1)
- {
- allDayLive+=(LEAPFEBRUARY-brithDay);
- }
- }
- else
- {
- allDayLive+=(brithMonthDay[brithMonth]-brithDay);
- }
- //計算今天年過的天數
- for(tempMonth=1;tempMonth<nowMonth;tempMonth++)
- {
- allDayLive+=nowMonthDay[tempMonth];
- }
- allDayLive+=nowDay;
- }
- //如果兩個日期年份相同
- else
- {
- if(brithMonth==nowMonth)
- {
- allDayLive+=nowDay-brithDay;
- }
- else
- {
- for(tempMonth=brithMonth+1;tempMonth<nowMonth;tempMonth++)
- {
- allDayLive+=brithMonthDay[tempMonth];
- }
- allDayLive+=brithMonthDay[brithMonth]-brithDay;
- allDayLive+=nowDay;
- }
- }
- //輸出結果
- cout<<"\n從出生到現在你活了 "<<allDayLive<<" 天。"<<endl;
- cout<<"\n按(Enter)退出程序。"<<endl;
- getch();
- return 0;
- }