1. 程式人生 > >Linux下使用gcc編程初體驗,實現日歷程序

Linux下使用gcc編程初體驗,實現日歷程序

高亮 string 回車 home lee span ubuntu下 \n sleep

近期剛剛放棄了Windows,投入了Ubuntu 的懷抱。今天就拿一個小小的案例來做一下C語言的編譯和運行流程。額,順便說一句。本文適合那些Linux新手,不適合老鳥哈。


看完本文可以學到什麽?


  • 程序員編碼神器Vim的簡單使用
  • 自帶編譯器gcc的使用
  • 執行編譯完成的程序

vim的簡單使用


關於vim的使用,這裏面的學問可謂是太深了,所以我就簡單的寫一些在這裏用到的一些命令了。

首先:打開終端terminal。使用cd命令定位到我們將要操作的一個文件夾,我本人的是/home/mark/code/c/目錄。然後就可以輸入vim Hello.c.這樣終端就會跳轉到一個vim的編輯界面。
這時我們看到的是命令模式,我們要想對Hello.c文件進行編輯的話,就必須使用到插入模式。按下a

即可在光標位置進行編輯了。


在這裏我就輸入下面一段文字:

#include<stdio.h>

int main(){
    printf("Hello World!\n");
    printf("Hello C\n");
    printf("This is compilered by GCC in Ubuntu!");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

gcc的簡單的使用


下面我就來談一談本文用到的gcc的幾個參數:

  • gcc -E // 預編譯命令,可以將源文件進行預編譯,生成.i結尾的預處理文件
  • gcc -c // 將預處理文件編譯成目標代碼(可執行) 以.o結尾
  • gcc -o // 這個命令一般會添加在上面命令的後邊,意思是前兩個命令完成後的結果輸出到哪個文件中。

下面我們就來看一看本文的gcc處理:

mark@mark-pc:~/Code/C$ vim Hello.c
mark@mark-pc:~/Code/C$ gcc -E Hello.c -o Hello.i
mark@mark-pc:~/Code/C$ gcc -c Hello.i -o Hello.o
mark@mark-pc:~/Code/C$ gcc Hello.c -o Hello
mark@mark-pc:~/Code/C$ ./Hello
Hello World!
Hello C
This is compilered by GCC
in Ubuntu!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

結果展示


觀察上述命令執行後的結果有兩種方式。
一是到相應的目錄下查看文件的具體信息,另外一個是使用命令行查看相關目錄下的具體的信息。


第一種方式:
技術分享圖片
我們可以看到相關的四個文件:

  • Hello 可執行文件
  • Hello.c 源文件
  • Hello.i 預編譯(預處理)文件
  • Hello.o 目標代碼

第二種方式時使用命令行進行:

mark@mark-pc:/$ cd /home/mark/Code/C
mark@mark-pc:~/Code/C$ ls
Hello  Hello.c  Hello.i  Hello.o
  • 1
  • 2
  • 3
  • 4

我們同樣可以得到上面這四個文件。


小總結


我本人也是剛剛接觸Ubuntu,所以對這個操作系統還不是很熟悉。所以難免有些地方講的不恰當或者不正確。本文也是為了提示那些和我一樣在Ubuntu下的新手練習如何編程而寫的指導性的博文。如果您發現了文章中有錯誤的地方,還望不吝賜教,也好讓我們共同進步!


2018年4月10日15:59:18
最近越來越發現,C實在是太重要了。正好今天沒什麽事,看到windows自帶的那個日期控件挺好看,就著手用C實現下~。
技術分享圖片

技術分享圖片


由第一張圖可以看出,這裏用到了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;
}
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173

function.h

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct tm* get_curdate();
void sleep(int seconds);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

constraints.h

#define WEEKDAY 7
#define ROW_NUM 4
  • 1
  • 2

編譯命令:

gcc *.c -o calender
  • 1

測試命令:

./calender
  • 1

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow

Linux下使用gcc編程初體驗,實現日歷程序