C语言实现变色进度条
C语言实现一个如下的变色进度条,供大家参考,具体内容如下
要求
1、依次多打印一个'#'
2、并后面标注百分比
3、还可以进行变色
实现进度条(未变色)
strip.c
#include<stdio.h> #include<unistd.h> #define M 100 int main() { char ch = '#'; char str[M] = {0}; int i = 0; char symbol[4] = {'|','/','-','\\'}; for(i = 0; i<M; ++i) { <span style="white-space:pre;"> </span> str[i] = '#'; printf("[%-100s][%%%d][%c]",str,i,symbol[i%4]); fflush(stdout); usleep(10000); printf("\n"); system("clear"); } return 0; }
现在让我们实现变色版本
定义颜色头文件 color.h
#define NONE "\033[m" #define RED "\033[0;32;31m" #define LIGHT_RED "\033[1;31m" #define GREEN "\033[0;32;32m" #define LIGHT_GREEN "\033[1;32m" #define BLUE "\033[0;32;34m" #define LIGHT_BLUE "\033[1;34m" #define DARY_GRAY "\033[1;30m" #define CYAN "\033[0;36m" #define LIGHT_CYAN "\033[1;36m" #define PURPLE "\033[0;35m" #define LIGHT_PURPLE "\033[1;35m" #define BROWN "\033[0;33m" #define YELLOW "\033[1;33m" #define LIGHT_GRAY "\033[0;37m" #define WHITE "\033[1;37m"
在strip.c中引入 color.h 并实现变色
#include<stdio.h> #include<unistd.h> #include"color.h" #define M 100 int main() { char ch = '#'; char str[M] = {0}; int i = 0; char symbol[4] = {'|','/','-','\\'}; for(i = 0; i<M; ++i) { int c = i%5; switch(c) { case 0: printf(RED); break; case 1: printf(GREEN); break; case 2: printf(BLUE); break; case 3: printf(YELLOW); break; case 4: printf(BROWN); break; } str[i] = '#'; printf("[%-100s][%%%d][%c]",str,i,symbol[i%4]); fflush(stdout); usleep(100000); printf("\n"); system("clear"); } printf("\033[0m"); return 0; }
实现一个Makefile
strip.exe:strip.c gcc strip.c -o strip.exe -g .PHONY:clean clean: rm -f strip.exe
运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)