計算日差
15027 ワード
2つの日付の間に何日の差があるかを計算します.
しかし、私はそれが一人が生まれてから今日まで何日生きたかを計算するのに使えるとしか思えません.
非常に未熟なコードで、乱れているところもあります.
そして入力を判断するのに苦労しましたが、完璧ではありません.
しかし、私はそれが一人が生まれてから今日まで何日生きたかを計算するのに使えるとしか思えません.
非常に未熟なコードで、乱れているところもあります.
そして入力を判断するのに苦労しましたが、完璧ではありません.
- #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<<"*********************************************
"<<endl;
-
- //
- cout<<" , :yyyy mm dd"<<endl;
- cin>>brithYear>>brithMonth>>brithDay;
- cout<<"
, :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<<"
! (Enter) 。"<<endl;
- getch();
- return -1;
- }
- if(brithYear==nowYear)
- {
- if(brithMonth>nowMonth)
- {
- cout<<"
! (Enter) 。"<<endl;
- getch();
- return -1;
- }
- }
- if(brithYear==nowYear)
- {
- if(brithMonth==nowMonth)
- {
- if(brithDay>nowDay)
- {
- cout<<"
! (Enter) 。"<<endl;
- getch();
- return -1;
- }
- }
- }
- if(brithDay>brithMonthDay[brithMonth]||nowDay>nowMonthDay[nowMonth])
- {
- cout<<"
! (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<<"
"<<allDayLive<<" 。"<<endl;
- cout<<"
(Enter) 。"<<endl;
-
- getch();
-
- return 0;
- }