C日曆

weixin_33716557發表於2018-04-10
8196049-87d7a4207af59806.png
C日曆實現效果圖
8196049-f9e776aae1c1845d.png
模仿自windows10自帶的日期控制元件

由第一張圖可以看出,這裡用到了struct tm資料結構,進而貫穿整個操作。

關鍵點在於:

  • 閏年下2月的計算
  • 上月末,本月,下月初等日期的輸出。

Main.c

#include<stdio.h>
#include<stdlib.h>
#include<locale.h>
#include "function.h"
#include "constraints.h"

int get_year_month_days(int year, int month) {
    int days; // 獲取當前月對應的天數
    if(month == 2) {
        if((year % 400 == 0) || (year%4 == 0 && year % 100 != 0)) {
            days = 29;
        }else{
            days = 28;
        }
    }else if(month == 4 || month == 6 || month == 9 || month == 11) {
        days = 30;
    }else {
        days = 31;
    }
    return days;
}

void print_lastmonth(struct tm *tm_now) {
    int last_month_cols, this_month_cols;
    int index; // 用於控制輸出上個月的日期
    int lastmonthlastday = get_year_month_days(1900+tm_now->tm_year, tm_now->tm_mon);
    //printf("Last month days:%d\n", lastmonthlastday);
    this_month_cols = (tm_now->tm_mday - tm_now->tm_wday)%7;
    last_month_cols = WEEKDAY - this_month_cols;
    //printf("%d:%d", last_month_cols, this_month_cols);
    // 列印上個月剩餘的日期 
    for(index=0; index < last_month_cols; index++) {
        printf("\33[33m%.2d  \33[0m", lastmonthlastday-last_month_cols + index + 1);
    }
    // 列印本月開始的日期
    for(index=1; index <= this_month_cols; index++) {
        printf("\33[32m%.2d  \33[0m", index);
    }
    // 上個月混合輸出結束
    printf("\n");
    
}

void print_thismonth(struct tm *tm_now) {
    // 本月總是佔據中間4行,所以只需要找出第二行開始的第一個日期即可
    int this_month_begin_day, last_month_cols, this_month_cols,next_month_cols;
    int thismonthdays = get_year_month_days(tm_now->tm_year + 1900, tm_now->tm_mon + 1);
    int index = (tm_now->tm_mday - tm_now->tm_wday) % WEEKDAY + 1;
    int rowindex, colindex;
    int tempdate;
    int lastmonthlastday = get_year_month_days(1900+tm_now->tm_year, tm_now->tm_mon);
    //printf("Last month days:%d\n", lastmonthlastday);
    this_month_cols = (tm_now->tm_mday - tm_now->tm_wday)%7;
    last_month_cols = WEEKDAY - this_month_cols;
    // 列印上個月剩餘的日期 
    for(index=0; index < last_month_cols; index++) {
        printf("\33[33m%.2d  \33[0m", lastmonthlastday-last_month_cols + index + 1);
    }
    // 列印本月開始的日期
    for(index=1; index <= this_month_cols; index++) {
        printf("\33[32m%.2d  \33[0m", index);
    }
    // 上個月混合輸出結束
    printf("\n");
    // 列印本月四行的資料;需要注意的是本月總天數可能會小於4行,因此要提前退出
    for(rowindex=0; rowindex < ROW_NUM; rowindex++) {
        // 列印每一列的資料
        for(colindex=0; colindex < WEEKDAY; colindex++) {
            tempdate = index + rowindex * WEEKDAY + colindex;
            if(tempdate == tm_now->tm_mday) {
                // 高亮顯示當天日期
                printf("\33[36m\33[1m\33[8m%.2d  \33[0m", tempdate);
            }else{
                if(rowindex*WEEKDAY + colindex + index <= thismonthdays) {
                    printf("\33[32m%.2d  \33[0m", tempdate);
                }else{
                    goto NEXTMONTH;
                }
            }
        }
        // 每行結束記得換行
        printf("\n");
    }
    NEXTMONTH: printf("");
    printf("[INDEX]%d\n", tempdate);
    // 下個月內容輸出
    this_month_cols = (thismonthdays - last_month_cols) % WEEKDAY;
    next_month_cols = WEEKDAY - this_month_cols;
    // 列印本月剩餘日期內容
    for(index=0; index < this_month_cols; index++) {
        // 判斷當前首個日期是否大於本月天數,大於的話需要提前終止輸出
        printf("\33[32m%.2d  \33[0m", thismonthdays - this_month_cols + index + 1);
    }
    // 列印下月初始日期內容
    for(index=1; index <= next_month_cols; index++) {
        printf("\33[33m%.2d  \33[0m", index);
    }
    // 尾行列印結束
    printf("\n");
}

void print_month(struct tm *tm_now) {
    int last_month_cols, this_month_head_cols, this_month_tail_cols, next_month_cols;
    int lastmonthdays = get_year_month_days(1990 + tm_now->tm_year, tm_now->tm_mon);
    int thismonthdays = get_year_month_days(1990 + tm_now->tm_year, tm_now->tm_mon + 1);
    //printf("Lastmonthdays: %d, thismonthdays: %d\n", lastmonthdays, thismonthdays);
    int tempdate;// 用於記錄本月日期輸出到了哪一天
    int index, changeline=0;
    int current_weekday;
    if(tm_now->tm_wday == 0) {
        current_weekday = WEEKDAY;
    }else{
        current_weekday = tm_now->tm_wday;
    }
    //printf("CURRENT WEEKDAY: %d\n", current_weekday);
    this_month_head_cols = (tm_now->tm_mday - current_weekday) % WEEKDAY;
    last_month_cols = WEEKDAY - this_month_head_cols;
    this_month_tail_cols = (thismonthdays - this_month_head_cols) % WEEKDAY;
    next_month_cols = WEEKDAY - this_month_tail_cols;
    //printf("[%d-%d-%d-%d]\n", last_month_cols, this_month_head_cols, this_month_tail_cols, next_month_cols);
    //printf("%2.d-%.2d-%.2d\n", tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday);
    printf("%s\n", "一  二  三  四  五  六  日");
    // 列印首行:上個月日期內容 + 本月月初日期內容
    for(index=1; index <= last_month_cols; index++) {
        printf("\33[33m%.2d  \33[0m", lastmonthdays - last_month_cols + index);
    }
    for(index=1; index <= this_month_head_cols; index++) {
        tempdate = index;
        printf("\33[32m%.2d  \33[0m", index);
    }
    // 首行輸出完畢,記得回車換行
    //printf("\t[tempdate=>%d, currentdate: %d]\n", tempdate, tm_now->tm_mday);
    printf("\n");
    // 輸出本月中間幾行的日期資料
    for(index=tempdate+1, changeline=1; index <= thismonthdays; index++, changeline++, tempdate++) {
        if(index == tm_now->tm_mday) {
            // 高亮當天日期
            printf("\33[35m\33[4m%.2d\33[0m", index);printf("  ");// 後面這倆空格是為了防止下劃線溢位
        }else{
            printf("\33[32m%.2d  \33[0m", index);
        }
        if(changeline == WEEKDAY) {
            changeline = 0;
            printf("\n");
        }
    }
    // 輸出下個月的日期資料
    for(index=1; index <= next_month_cols; index++) {
        printf("\33[33m%.2d  \33[0m", index);
    }
    printf("\n");
        
}

int main() {

    struct tm *tm_now;
    //while(1) {
        tm_now = get_curdate();
        // tm_now->tm_year = 118;
        // tm_now->tm_mon = 4;
        // tm_now->tm_mday = 28;
        //printf("\t%d-%d-%d -%d\n", tm_now->tm_year+1900, tm_now->tm_mon + 1, tm_now->tm_mday, tm_now->tm_wday);
        printf("|  %d-%.2d-%.2d %.2d:%.2d:%.2d  |\n", tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
        printf("============================\n");
        print_month(tm_now);
        printf("============================\n");
        // 休眠一秒後重新整理整個螢幕實現,動態更改時間的效果
        //sleep(1);
        //system("clear");
    //}
    return 0;
}

function.h

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct tm* get_curdate();
void sleep(int seconds);

function.c

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "function.h"

struct tm* get_curdate() {
    time_t now;
    struct tm *tm_now;
    //char curdate[10];
    time(&now);
    tm_now = localtime(&now);
    return tm_now;
}
void sleep(int seconds) {
    time_t tm = time(NULL);
    time_t tm2 = tm;
    while(difftime(tm2, tm) < seconds) {
        tm2 = time(NULL);
    }
}

constraints.h

#define WEEKDAY 7
#define ROW_NUM 4

編譯命令:

gcc *.c -o calender

測試命令:

./calender

相關文章