【水文】简易时钟

/*

    制作的一个能显示时间的简易时钟

*/

 

#include

#include

#include

#include

 

// 带颜色的打印函数

void print_with_color(char *str, int color)

{

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hConsole, color);

    printf("%s", str);

    SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

}

 

 

 

int main(){

    while(1){

        /* 这两个放在外面的话,就不能一直获取当前时间了 */

        time_t t = time(NULL);

        struct tm *now = localtime(&t);

        char str_t[100]; 

        sprintf(str_t, "------------------------\n");

        print_with_color(str_t, FOREGROUND_BLUE | FOREGROUND_INTENSITY);

 

        sprintf(str_t, "| %d/%02d/%02d %02d:%02d:%02d |\n", 

                    now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);

        print_with_color(str_t, FOREGROUND_GREEN | FOREGROUND_INTENSITY);

        

        sprintf(str_t, "------------------------\n");

        print_with_color(str_t, FOREGROUND_BLUE | FOREGROUND_INTENSITY);

        Sleep(1000);

        system("cls");

    }

    return 0;

}

你可能感兴趣的:(C)