C程式設計百例之第四例

來源:互聯網
上載者:User

標籤:include   原始碼   c程式設計   return   

題目:輸入某年某月某日,判斷這一天是這一年的第幾天?

1.程式分析:以3月5日為例,應該先把前兩個月的加起來,然後再加上5天即本年的第幾天,特殊情況,閏年且輸入月份大於3時需考慮多加一天。

2.程式原始碼:

#include <stdio.h>

int main(void)

 int day, month, year, countDay, leap;
 printf("\nPlease input year, month and day\n");
 scanf("%d %d %d", &year, &month, &day);
 if((year % 4 == 0 && year % 100 != 0) || (year % 400 ==0))
  leap = 1;
 else
  leap = 0;
 switch(month)
 {
  case 1:
   countDay = 0;
   if(day <= 0 || day > 31)
    return 1;
   break;
  case 2:
   countDay = 31;
   if(leap != 1)
   {
    if(day <= 0 || day > 28)
     return 1;
   }
   else
   {
    if(day <= 0 || day > 29)
     return 1;
   }
   break;
  case 3:
   countDay = 59;
   if(day <= 0 || day > 31)
    return 1;
   break;
  case 4:
   countDay = 90;
   if(day <= 0 || day > 30)
    return 1;
   break;
  case 5:
   countDay = 120;
   if(day <= 0 || day > 31)
    return 1;
   break;
  case 6:
   countDay = 151;
   if(day <= 0 || day > 30)
    return 1;
   break;
  case 7:
   countDay = 181;
   if(day <= 0 || day > 31)
    return 1;
   break;
  case 8:
   countDay = 212;
   if(day <= 0 || day > 31)
    return 1;
   break;
  case 9:
   countDay = 243;
   if(day <= 0 || day > 30)
    return 1;
   break;
  case 10:
   countDay = 273;
   if(day <= 0 || day > 31)
    return 1;
   break;
  case 11:
   countDay = 304;
   if(day <= 0 || day > 30)
    return 1;
   break;
  case 12:
   countDay = 334;
   if(day <= 0 || day > 31)
    return 1;
   break;
  default:
   printf("Input month error!\n");
   return 1;
   break;
 }
 countDay = countDay + day + 1;
 printf("This is the %dth of this years!\n", countDay);
 return 0;
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.