凱撒日期

所謂的凱撒日期就是輸入一個日期輸出這一天是這一年中的第幾天。

這是今天在論壇幫人寫的一個小程序,好久沒寫c了,寫個簡單的練練,記錄下。

  1. #include <stdio.h> 
  2. #include <stdbool.h> 
  3. int main(){ 
  4.     int year;    //the year 
  5.     int mon;    //the month 
  6.     int day;    //the day  
  7.     int dayOfYear = 0;   //day of the year 
  8.     bool leapYear;   //if the year is a leap year?? 
  9.      
  10.       printf("Please enter the data(YYYY-MM-DD): "); 
  11.       scanf("%d-%d-%d",&year,&mon,&day); 
  12.       while((mon < 1 || mon > 12)||(day < 0 || day > 31)){   //check the input 
  13.             printf("Enter error!Please enter again!\n\n"); 
  14.             printf("Please enter the data(YYYY-MM-DD): "); 
  15.             scanf("%d-%d-%d",&year,&mon,&day); 
  16.       }  
  17.  
  18.      
  19.      
  20.     if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0)){//if the year is a leap year,then leapYear=true 
  21.         leapYear = true
  22.     } 
  23.     else
  24.         leapYear = false
  25.     } 
  26.      
  27.      
  28.     if(leapYear){   //if the year is a leap year 
  29.         if(mon == 1){ 
  30.             dayOfYear == day; 
  31.         } 
  32.         else if(mon == 2){ 
  33.             dayOfYear = 31 + day; 
  34.         } 
  35.         else if(mon > 2) { 
  36.             dayOfYear += 60; 
  37.             dayOfYear += day; 
  38.             for(int i = 3;i< mon;i++){ 
  39.                 if((i == 1)||(i == 3)||(i == 5)||(i == 7)||(i == 8)||(i == 10)||(i == 12)){ 
  40.                   dayOfYear += 31; 
  41.                   } 
  42.                 else if((i == 4)||(i == 6)||(i == 9)||(i == 11)){ 
  43.                     dayOfYear += 30; 
  44.                 } 
  45.           } 
  46.              
  47.         } 
  48.          
  49.     } 
  50.      
  51.     else if(!leapYear){//if the year is not a leap year 
  52.         if(mon == 1){ 
  53.             dayOfYear == day; 
  54.         } 
  55.         else if(mon == 2){ 
  56.             dayOfYear = 31 + day; 
  57.         } 
  58.         else if(mon > 2) { 
  59.             dayOfYear += 59; 
  60.             dayOfYear += day; 
  61.             for(int i = 3;i< mon;i++){ 
  62.                 if((i == 1)||(i == 3)||(i == 5)||(i == 7)||(i == 8)||(i == 10)||(i == 12)){ 
  63.                   dayOfYear += 31; 
  64.                   } 
  65.                 else if((i == 4)||(i == 6)||(i == 9)||(i == 11)){ 
  66.                     dayOfYear += 30; 
  67.                 } 
  68.           } 
  69.              
  70.         } 
  71.     } 
  72.      
  73.     printf("The day of the year is: %d\n",dayOfYear); 
  74. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章