[ create a new paste ] login | about

Link: http://codepad.org/MbqIW42n    [ raw code | output | fork ]

C, pasted on Aug 20:
#include <stdio.h>

#define YEAR 2010
#define MONTH 6

typedef enum {
    w_sun, w_mon, w_tue, w_wed, w_thu, w_fri, w_sat
} wday_t;
typedef struct {
    int s_period;    // 開始時限
    int e_period;    // 終了時限
} period_t;

typedef struct {
    char *lname;        // 授業名(NULLの場合は無効なデータを表す)
char *sname;        // 授業略称(NULLの場合はlnameをsnameとしても使う)
    wday_t wday;        // 開講曜日
    period_t period;    // 開講時限
    char *room;         // 講義室
} course_t;

course_t course[] = {
    // 2年科目
    {"物理学実験", "物理実験", w_mon, {5, 7}, "?"},
    {"電気回路", NULL, w_wed, {5, 6}, "D3505"},
    {"計算機ハードウェア", "ハード", w_thu, {1, 2}, "D4103"},
    {"上級プログラミング演習I", "上級プロI", w_thu, {3, 4}, "電算室"},
    {"計算機言語論I", "言語論I", w_thu, {7, 8}, "D4703"},
    {"人工知能I及び演習", "人工知能I", w_fri, {3, 5}, "D5202"},
    {"工業数学II", NULL, w_fri, {9, 10}, "D5902"},
    // 再履修
    {"離散数学", NULL, w_mon, {3, 4}, "D1301"},
    {"情報科学基礎及び初級プログラミング演習", "初級プロ", w_wed, {1, 4}, "電算室"},
    // 無効データ
    {NULL, NULL, w_sun, {0, 0}, NULL},
};

char *wday_name[] = {
    "日", "月", "火", "水", "木", "金", "土",
};

// year年month月の最後の日付を求める
int get_last_mday(int year, int month);
// year年month月mday日の曜日を求める
int get_wday(int year, int month, int mday);
// wday曜日のs_period時限に始まる講義を取得
course_t get_course(int wday, int s_period);
// 表の横線を表示する
void insert_line(void);

int main(void){
int year, month;
    int mday, last_mday, period;
    wday_t wday;
course_t course = {"dummy", NULL, w_sun, {0, 0}, NULL};
char *room[10] = {0};

    year = YEAR;
    month = MONTH;

    last_mday = get_last_mday(year, month);
    wday = get_wday(year, month, 1);

    printf("-------------------- %4d年%2d月の予定表 --------------------\n", year, month);
    insert_line();
    for (mday = 1 ; mday <= last_mday ; mday++){
printf("%2d/%2d(%s)|", month, mday, wday_name[wday]);
        for (period = 1 ; period <= 9 ; period += 2){
            if (course.wday == wday &&
                period >= course.period.s_period && period <= course.period.e_period)
        {
                // このコマの半分で終了
                if (period == course.period.e_period){
                    printf("---->     |");
                }
                // このコマの最後で終了
                else if (period+1 == course.period.e_period){
                    printf("--------->|");
                }
                // 次のコマ以降に続く
                else {
                    printf("----------|");
                }
            }
            else {
                course = get_course(wday, period);
                if (course.lname != NULL){
                    room[period] = course.room;
                    if (course.sname == NULL){
                        printf("%-10s|", course.lname);
                    }
                    else {
                        printf("%-10s|", course.sname);
                    }
                }
                else {
                    room[period] = "";
                    printf("          |");
                }
            }
        }
        puts("");
        printf("         |");
        for (period = 1 ; period <= 9 ; period += 2){
            printf("%-10s|", room[period]);
        }
        puts("");
        insert_line();
        wday = (wday+1) % 7;
    }

    for (wday = w_mon ; wday < w_sun ; wday++){
        printf("%s\n", wday_name[wday]);
    }


    return 0;
}


int get_last_mday(int year, int month){
    if (month == 2){
        // 閏年の判定
        // 400で割り切れるか、4で割り切れ100で割り切れなければ閏年
        if (((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))){
            return 29;
        }
		else {
            return 28;
        }
    }
else if (month == 4 || month == 6 || month == 9 || month == 11){
        return 30;
    }
    return 31;
}

// ツェラーの公式による曜日の計算
int get_wday(int year, int month, int mday){
    int w, r;
    // 1月、2月は前年の13月、14月として扱う
    if (month == 1 || month == 2){
        year --;
        month = month + 12;
    }
    w = year + year/4 - year/100 + year/400 + (13*month+8)/5 + mday;
    r = w % 7;
    return r;
}

// wday曜日のs_period時限に開始される授業を返す
course_t get_course(int wday, int s_period){
    int i = 0;
    while (course[i].lname != NULL){
        if (course[i].wday == wday && course[i].period.s_period == s_period){
            return course[i];
        }
        i++;
    }
    // 該当する授業がないときは、lnameがNULLであるようなcourse_t型の値を返す
    return course[i];
}

void insert_line(void){
    int period;
    printf("---------+");
    for (period = 1 ; period <= 9 ; period += 2){
        printf("----------+");
    }
    puts("");
}


Output:
-------------------- 2010年 6月の予定表 --------------------
---------+----------+----------+----------+----------+----------+
 6/ 1(火)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/ 2(水)|初級プロ|--------->|電気回路|          |          |
         |電算室 |          |D3505     |          |          |
---------+----------+----------+----------+----------+----------+
 6/ 3(木)|ハード |上級プロI|          |言語論I|          |
         |D4103     |電算室 |          |D4703     |          |
---------+----------+----------+----------+----------+----------+
 6/ 4(金)|          |人工知能I|---->     |          |工業数学II|
         |          |D5202     |          |          |D5902     |
---------+----------+----------+----------+----------+----------+
 6/ 5(土)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/ 6(日)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/ 7(月)|          |離散数学|物理実験|---->     |          |
         |          |D1301     |?         |          |          |
---------+----------+----------+----------+----------+----------+
 6/ 8(火)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/ 9(水)|初級プロ|--------->|電気回路|          |          |
         |電算室 |          |D3505     |          |          |
---------+----------+----------+----------+----------+----------+
 6/10(木)|ハード |上級プロI|          |言語論I|          |
         |D4103     |電算室 |          |D4703     |          |
---------+----------+----------+----------+----------+----------+
 6/11(金)|          |人工知能I|---->     |          |工業数学II|
         |          |D5202     |          |          |D5902     |
---------+----------+----------+----------+----------+----------+
 6/12(土)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/13(日)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/14(月)|          |離散数学|物理実験|---->     |          |
         |          |D1301     |?         |          |          |
---------+----------+----------+----------+----------+----------+
 6/15(火)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/16(水)|初級プロ|--------->|電気回路|          |          |
         |電算室 |          |D3505     |          |          |
---------+----------+----------+----------+----------+----------+
 6/17(木)|ハード |上級プロI|          |言語論I|          |
         |D4103     |電算室 |          |D4703     |          |
---------+----------+----------+----------+----------+----------+
 6/18(金)|          |人工知能I|---->     |          |工業数学II|
         |          |D5202     |          |          |D5902     |
---------+----------+----------+----------+----------+----------+
 6/19(土)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/20(日)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/21(月)|          |離散数学|物理実験|---->     |          |
         |          |D1301     |?         |          |          |
---------+----------+----------+----------+----------+----------+
 6/22(火)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/23(水)|初級プロ|--------->|電気回路|          |          |
         |電算室 |          |D3505     |          |          |
---------+----------+----------+----------+----------+----------+
 6/24(木)|ハード |上級プロI|          |言語論I|          |
         |D4103     |電算室 |          |D4703     |          |
---------+----------+----------+----------+----------+----------+
 6/25(金)|          |人工知能I|---->     |          |工業数学II|
         |          |D5202     |          |          |D5902     |
---------+----------+----------+----------+----------+----------+
 6/26(土)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/27(日)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/28(月)|          |離散数学|物理実験|---->     |          |
         |          |D1301     |?         |          |          |
---------+----------+----------+----------+----------+----------+
 6/29(火)|          |          |          |          |          |
         |          |          |          |          |          |
---------+----------+----------+----------+----------+----------+
 6/30(水)|初級プロ|--------->|電気回路|          |          |
         |電算室 |          |D3505     |          |          |
---------+----------+----------+----------+----------+----------+


Create a new paste based on this one


Comments: