首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

使用 C 語言實現互動式命令列日曆程式

     編輯:關於C語言

簡介

這是一個純 C 語言編寫的命令列日曆程式,支援輸入年份與月份後顯示該月曆表,並提供互動式操作:切換上下月、自訂年月、輸出至檔案以及退出程式。程式採用標準輸入輸出,並利用 conio.h 的 getch() 實現無緩衝按鍵讀取,配合 system("cls") 清屏,帶來流暢的終端機體驗。

核心功能

  • 閏年判斷:依據格里曆規則(4 整除但不整除 100,或整除 400)判斷閏年,二月天數自動調整。
  • 日期計算:以 1900 年 1 月 1 日為基準,累算總天數並取模 7 得到該月 1 號屬星期幾,進而正確排版日曆。
  • 互動式選單:支援 > 下個月、< 上個月、? 自訂年月、# 退出。
  • 檔案輸出:同步將日曆寫入 abc.txt,方便留存或列印。

關鍵函數解析

Leap(int year)

回傳 1 表示閏年,0 表示平年。邏輯為 (year%4==0 && year%100!=0) || (year%400==0)。

Month(int month)

依月份回傳天數,二月固定 28 天,閏年調整由呼叫端處理。

print(int k, int t, int day)

負責繪製日曆表頭(星期一至日)、日期格子與分隔線。參數 k 為 1 號對應的星期索引,day 為該月總天數。同時輸出至螢幕與檔案。

完整原始碼

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

int a[31] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};

int Leap(int y) {
    return (y%4==0 && y%100!=0) || (y%400==0);
}

int Month(int m) {
    switch(m) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;
        case 2: return 28;
        case 4: case 6: case 9: case 11: return 30;
        default: printf("月份錯誤\\n"); return 0;
    }
}

void print(int k, int t, int day) {
    FILE *fp = fopen("abc.txt", "w");
    if (!fp) { printf("開檔失敗\\n"); exit(1); }
    int i;
    printf("\\n日 一 二 三 四 五 六\\n");
    fprintf(fp, "\\n日 一 二 三 四 五 六\\n");
    printf("-------------------------------------------------------------\\n");
    fprintf(fp, "---------------------------------------------------------\\n");
    for (i=1; i<=k; i++) { printf(" "); fprintf(fp, " "); }
    for (i=0; i<day; i++) {
        printf("%-8d", a[i]);
        fprintf(fp, "%-8d", a[i]);
        if (k==6) {
            printf("\\n-------------------------------------------------------------\\n");
            fprintf(fp, "\\n-----------------------------------------------------------\\n");
            t=0;
        }
        if (t!=0 && t%7==0) {
            printf("\\n-------------------------------------------------------------\\n");
            fprintf(fp, "\\n-----------------------------------------------------------\\n");
        }
        k++; t++;
    }
    printf("\\n--------------------------\\n 選項\\n > 下一個月\\n < 上一個月\\n ? 自訂年月\\n # 退出\\n--------------------------\\n");
    fclose(fp);
}

int main() {
    int m, n, i, total, day, k, t=-6, j=0;
    printf("請輸入年份:"); scanf("%d", &m);
    printf("請輸入月份:"); scanf("%d", &n); getchar();
    do {
        system("cls");
        printf("%d年%d月:\\n", m, n);
        total = 365*(m-1);
        for (i=1; i<m; i++) if (Leap(i)) total++;
        for (i=1; i<n; i++) total += Month(i);
        day = Month(n);
        if (Leap(m) && n==2) day++;
        if (total>1) total--;
        k = (total==0) ? (total+1)%7 : (total+2)%7;
        print(k, t, day);
        printf("請選擇:");
        switch(getch()) {
            case '>': if (n==12) { m++; n=1; } else n++; break;
            case '<': if (n==1) { m--; n=12; } else n--; break;
            case '?': printf("請輸入年份:"); scanf("%d", &m);
                      printf("請輸入月份:"); scanf("%d", &n); break;
            case '#': j=1; break;
        }
        if (j) break;
        putchar(10);
    } while(1);
    return 0;
}

編譯與執行提示

程式使用 conio.h 與 system("cls"),屬 Windows 平台專用。若在 Linux/macOS 編譯,請將 system("cls") 改為 system("clear"),並以 getchar() 替代 getch()(需自行處理緩衝)。

延伸應用

可進一步加入農曆顯示、節氣標記、行事曆記事功能,或移植至圖形介面(如 ncurses、Qt)提升使用體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved