.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博客