Linux下使用gcc编程初体验,实现日历程序

近期刚刚放弃了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!");
}

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!

结果展示


观察上述命令执行后的结果有两种方式。
一是到相应的目录下查看文件的具体信息,另外一个是使用命令行查看相关目录下的具体的信息。


第一种方式:
这里写图片描述
我们可以看到相关的四个文件:

  • 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

我们同样可以得到上面这四个文件。


小总结


我本人也是刚刚接触Ubuntu,所以对这个操作系统还不是很熟悉。所以难免有些地方讲的不恰当或者不正确。本文也是为了提示那些和我一样在Ubuntu下的新手练习如何编程而写的指导性的博文。如果您发现了文章中有错误的地方,还望不吝赐教,也好让我们共同进步!


2018年4月10日15:59:18
最近越来越发现,C实在是太重要了。正好今天没什么事,看到windows自带的那个日期控件挺好看,就着手用C实现下~。
C日历实现效果图

模仿自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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰 戈 尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值