This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char *argv[]) //使用Command line argument list | |
{ | |
int total_days, year, month, i, j, k, m, n, mon[13]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //宣告陣列&整數變數 | |
year = atoi(argv[1]); //將年份存進year | |
if (year < 1900) { | |
printf("年份錯誤,請重新輸入。\n"); | |
} | |
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { //判斷閏年與否 | |
mon[2] = 29; | |
} | |
else { | |
mon[2] = 28; | |
} | |
total_days = (year - 1900) + (year - 1901) / 4; /*以1900年1月1日為星期一當基準,計算所輸入年份1月1日的位置*/ | |
if (argc == 2) { //如果只輸入年份 | |
for (k = 1; k < 13; k++) { //使用迴圈印出1到12月 | |
total_days += mon[k - 1]; //該年欲印出的月份之前所經過總天數 | |
printf("\n%4d年%2d月\n 日 一 二 三 四 五 六\n", year, k); | |
for (i = 0; i < ((total_days + 1) % 7); i++) { //計算空白天數並印出 | |
printf(" "); | |
} | |
for (j = 1; j <= mon[k]; j++) { //印出日期 | |
printf("%3d", j); | |
if ((total_days + j + 1) % 7 == 0) { //碰到星期六換行 | |
printf("\n"); | |
} | |
} | |
printf("\n"); | |
} | |
} | |
if (argc == 3) { //如果輸入年份及月份 | |
if (strcmp(argv[2], "Jua") == 0) { //將特定字串對應至特定數字 | |
month = 1; | |
} | |
else if (strcmp(argv[2], "Feb") == 0) { | |
month = 2; | |
} | |
else if (strcmp(argv[2], "Mar") == 0) { | |
month = 3; | |
} | |
else if (strcmp(argv[2], "Apr") == 0) { | |
month = 4; | |
} | |
else if (strcmp(argv[2], "May") == 0) { | |
month = 5; | |
} | |
else if (strcmp(argv[2], "Jun") == 0) { | |
month = 6; | |
} | |
else if (strcmp(argv[2], "Jul") == 0) { | |
month = 7; | |
} | |
else if (strcmp(argv[2], "Aug") == 0) { | |
month = 8; | |
} | |
else if (strcmp(argv[2], "Sep") == 0) { | |
month = 9; | |
} | |
else if (strcmp(argv[2], "Oct") == 0) { | |
month = 10; | |
} | |
else if (strcmp(argv[2], "Nov") == 0) { | |
month = 11; | |
} | |
else if (strcmp(argv[2], "Dec") == 0) { | |
month = 12; | |
} | |
else { //將數字月份存進month | |
month = atoi(argv[2]); | |
} | |
for (n = 0; n < month; n++) { | |
total_days += mon[n]; //該年欲印出的月份之前所經過總天數 | |
} | |
printf("\n%4d年%2d月\n 日 一 二 三 四 五 六\n", year, month); | |
for (i = 0; i < ((total_days + 1) % 7); i++) { //計算空白天數並印出 | |
printf(" "); | |
} | |
for (j = 1; j <= mon[n]; j++) { //印出日期 | |
printf("%3d", j); | |
if ((total_days + j + 1) % 7 == 0) { //碰到星期六換行 | |
printf("\n"); | |
} | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
並無參考萬年曆公式,所以可能有點難參透0.0
而且有未知的bug...
沒有留言:
張貼留言