計算日期差

計算兩個日期之間相差多少天。

但是我只能想到它可以用來計算一個人從出生到今天活了多少天……

非常不成熟的代碼,有些地方比較亂。

並且爲了判斷輸入花費了不少的功夫,但還是不完美。

  1. #include <iostream> 
  2. #include <conio.h>  
  3.   
  4. #define LEAPYEAR 366//閏年天數  
  5. #define COMMONYEAR 365//平年天數  
  6. #define LEAPFEBRUARY 29//閏年二月天數  
  7.   
  8. using namespace std;  
  9.   
  10. int main(int argc,char *argv[]) 
  11.     int brithYear,brithMonth,brithDay,signBrithYear=0; //出生日期和它的平閏狀態  
  12.     int nowYear,nowMonth,nowDay,signNowYear=0; //今天的日期和今年的平閏狀態  
  13.     long long allDayLive=0;//相差的天數  
  14.     int tempYear,tempMonth; 
  15.     //每個月的天數  
  16.     int brithMonthDay[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};  
  17.     int nowMonthDay[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 
  18.       
  19.     //限定到公元2200年只是一個提醒,實際上沒有這個限制  
  20.     cout<<"*********************************************"<<endl; 
  21.     cout<<"警告!請不要輸入數字或空格以外的任何其他字符!"<<endl; 
  22.     cout<<"注意!年份的有效範圍爲公元1年到公元2200年!"<<endl;   
  23.     cout<<"*********************************************\n"<<endl; 
  24.       
  25.     //輸入數據    
  26.     cout<<"請輸入你的出生日期,格式:yyyy mm dd"<<endl; 
  27.     cin>>brithYear>>brithMonth>>brithDay; 
  28.     cout<<"\n請輸入今天的日期,格式:yyyy mm dd"<<endl; 
  29.     cin>>nowYear>>nowMonth>>nowDay;  
  30.       
  31.     //判斷出生年與今年的平閏狀態,修正二月的天數  
  32.     if(brithYear%100!=0&&brithYear%4==0||brithYear%400==0)  
  33.     { 
  34.         signBrithYear=1; 
  35.         brithMonthDay[2]=LEAPFEBRUARY; 
  36.     } 
  37.     if(nowYear%100!=0&&nowYear%4==0||nowYear%400==0)  
  38.     { 
  39.         signNowYear=1; 
  40.         nowMonthDay[2]=LEAPFEBRUARY; 
  41.     } 
  42.       
  43.     //判斷輸入的日期是否規範有效  
  44.     if
  45.         brithYear<=0|| 
  46.         brithMonth<=0|| 
  47.         brithDay<=0|| 
  48.         nowYear<=0|| 
  49.         nowMonth<=0|| 
  50.         nowDay<=0|| 
  51.         brithYear>9999||//限定最大年份  
  52.         nowYear>9999|| 
  53.         brithMonth>12|| 
  54.         nowMonth>12|| 
  55.         brithYear>nowYear 
  56.     ) 
  57.     { 
  58.         cout<<"\n輸入錯誤!按(Enter)退出程序。"<<endl; 
  59.         getch(); 
  60.         return -1; 
  61.     }  
  62.     if(brithYear==nowYear) 
  63.     { 
  64.         if(brithMonth>nowMonth) 
  65.         { 
  66.             cout<<"\n輸入錯誤!按(Enter)退出程序。"<<endl; 
  67.             getch(); 
  68.             return -1; 
  69.         } 
  70.     } 
  71.     if(brithYear==nowYear) 
  72.     { 
  73.         if(brithMonth==nowMonth) 
  74.         { 
  75.             if(brithDay>nowDay) 
  76.             { 
  77.                 cout<<"\n輸入錯誤!按(Enter)退出程序。"<<endl; 
  78.                 getch(); 
  79.                 return -1; 
  80.             } 
  81.         } 
  82.     } 
  83.     if(brithDay>brithMonthDay[brithMonth]||nowDay>nowMonthDay[nowMonth]) 
  84.     { 
  85.         cout<<"\n輸入錯誤!按(Enter)退出程序。"<<endl; 
  86.         getch(); 
  87.         return -1;   
  88.     } 
  89.       
  90.     //感覺計算方法不是特別好,可能有許多可以優化的地方  
  91.     //如果兩個日期不同年  
  92.     if(brithYear!=nowYear) 
  93.     {  
  94.         //計算中間相隔的整年的天數 
  95.         for(tempYear=brithYear+1;tempYear<nowYear;tempYear++) 
  96.         { 
  97.             if(tempYear%100!=0&&tempYear%4==0||tempYear%400==0) 
  98.             { 
  99.                 allDayLive+=LEAPYEAR; 
  100.             } 
  101.             else 
  102.             { 
  103.                 allDayLive+=COMMONYEAR; 
  104.             } 
  105.         }  
  106.       
  107.         //計算出生那年過的天數  
  108.         if(brithMonth==1) 
  109.         { 
  110.             if(signBrithYear==1) 
  111.             { 
  112.                 allDayLive+=LEAPFEBRUARY; 
  113.             } 
  114.         } 
  115.         for(tempMonth=brithMonth+1;tempMonth<=12;tempMonth++) 
  116.         { 
  117.             allDayLive+=brithMonthDay[tempMonth]; 
  118.         } 
  119.         if(brithMonth==2) 
  120.         { 
  121.             if(signBrithYear==1) 
  122.             { 
  123.                 allDayLive+=(LEAPFEBRUARY-brithDay); 
  124.             } 
  125.         } 
  126.         else 
  127.         { 
  128.             allDayLive+=(brithMonthDay[brithMonth]-brithDay); 
  129.         } 
  130.       
  131.         //計算今天年過的天數 
  132.         for(tempMonth=1;tempMonth<nowMonth;tempMonth++) 
  133.         { 
  134.             allDayLive+=nowMonthDay[tempMonth]; 
  135.         } 
  136.         allDayLive+=nowDay; 
  137.     } 
  138.     //如果兩個日期年份相同  
  139.     else 
  140.     { 
  141.         if(brithMonth==nowMonth) 
  142.         { 
  143.             allDayLive+=nowDay-brithDay; 
  144.         } 
  145.         else 
  146.         { 
  147.             for(tempMonth=brithMonth+1;tempMonth<nowMonth;tempMonth++) 
  148.             { 
  149.                 allDayLive+=brithMonthDay[tempMonth]; 
  150.             } 
  151.             allDayLive+=brithMonthDay[brithMonth]-brithDay; 
  152.             allDayLive+=nowDay; 
  153.         } 
  154.     } 
  155.       
  156.     //輸出結果 
  157.     cout<<"\n從出生到現在你活了 "<<allDayLive<<" 天。"<<endl; 
  158.     cout<<"\n按(Enter)退出程序。"<<endl;  
  159.       
  160.     getch(); 
  161.       
  162.     return 0; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章