彩色进度条(C语言版本)

.h文件

#include<stdio.h>
#include<windows.h>

#define NUM 101
#define LOAD_UP 50
#define LOAD_DOWN 60
#define SLEEP_SLOW 300
#define SLEEP_FAST 70

版本1:(初始版)

//v1
#include "progress.h"
int main()
{
	char bar[NUM] = { '\0' };
	int time = 0;//次数
	const char* load = "-\\|/";
	int len = strlen(load);

	while (time<NUM-1)
	{	

		bar[time] = '=';
		if(time==99)
			bar[time] = '>';
		printf("[%-100s][%d%%][%c]\r", bar,time+1,load[time%len]);//time+1保证百分比从
		time++;
		Sleep(SLEEP_FAST);
		if (time>= LOAD_UP &&time<= LOAD_DOWN)
		{
			Sleep(SLEEP_SLOW);
		}
	}
 printf("\n");

	return 0;
}

版本2:(最终版)

#include "progress.h"
void progress()//time次数
{
	char bar[NUM] ;
	memset(bar, '\0',NUM);
	int time = 0;//次数
	const char* load = "-\\|/";
	int len_load = strlen(load);
	const char* ani = "=>";
	int len_ani = strlen(ani);

	while (time <NUM)
	{
		if (time >= LOAD_UP && time <= LOAD_DOWN)
		{
			Sleep(SLEEP_SLOW);
			printf("\033[1;33m[%-100s]\033[0m\033[1;91m[%d%%]\033[0m\033[1;91m[%c]\033[0m\r", bar, time, load[time % len_load]);//time+1保证百分比从
		}
		else
		{
			printf("\033[1;33m[%-100s]\033[0m\033[1;94m[%d%%]\033[0m\033[1;92m[%c]\033[0m\r", bar, time, load[time % len_load]);//time+1保证百分比从
		}
		bar[time] = ani[time % len_ani];
		time++;
		Sleep(SLEEP_FAST);

	}
	printf("\n");
}

int main()
{
	progress();
	return 0;
}

为什么在最后有printf("\n");原因:关于C的\r回车在不同平台的问题-CSDN博客

效果:

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值