#include <stdio.h>
#include <string.h>
#include <unistd.h>
/* display by dynamic line
* percent: range must at 0~100
*/
void
update_progress_dynamic(int percent)
{
char str[] = {'-', '\\', '|', '/'};
printf("%c %3d%%\r", str[percent%4], percent);
if (percent == 100) {
#ifndef COMPLETE_HIDE
putchar('\n');
#else
printf(" \r");
#endif
}
fflush(stdout);
}
/* display by bar
* percent: range must at 0~100
*/
void
update_progress_bar(int percent)
{
int i;
const int barlen = 50;
putchar('[');
for (i=1; i<=barlen; i++) {
putchar(i*100 <= percent*barlen ? '=' : ' ');
}
putchar(']');
printf(" %3d%%\r", percent);
if (percent == 100) {
#ifndef COMPLETE_HIDE
putchar('\n');
#else
for (i=0; i<barlen+7; i++) {
putchar(' ');
}
putchar('\r');
#endif
}
fflush(stdout);
}
int main()
{
int now, total, percent;
for (total=21, now=1; now<=total; now++) {
percent = (now*100)/total;
update_progress_bar(percent);
//update_progress_dynamic(percent);
usleep(200000);
}
return 0;
}
进度条C实现
最新推荐文章于 2024-02-24 22:15:23 发布