從鍵盤輸入年、月、日。在屏幕輸出此日期是該年的第幾天。
源程序:
#include<stdio.h> int Leap(int year) //判斷輸入年份是否為閏年{ if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { return 1; } else { return 0; }} int number(int year, int month, int day){ int sum = 0; int a[12] = { 31, 29, 31, 30.31, 30, 31, 31, 30, 31, 30, 31}; //閏年每月的天數(shù) int b[12] = { 31, 28, 31, 30.31, 30, 31, 31, 30, 31, 30, 31}; //平年每月對應(yīng)的天數(shù) switch (Leap(year)) { case 1: for (int i = 0; i < month - 1; i++) { sum += a[i]; } break; case 0: for (int i = 0; i < month - 1; i++) { sum += b[i]; } break; } sum += day; return sum;}int main(){ int year, month, day; int sum = 0; printf("請輸入一個(gè)日期:\n"); scanf("%d%d%d", &year, &month, &day); sum = number(year, month, day); printf("\n"); printf("該日期為%-4d年第%-3d天\n", year, sum); return 0;}