代码: 全选
#include <stdio.h>
#include <unistd.h>
/* 打印进度条 */
void DisplayProgress(long current, long total) {
int width = 50; /* 进度条宽度为 50 个字符 */
int i;
/* 清空行 */
for (i = 0; i < width + 7; i++) {
putchar('\b');
}
int LoopTimes = (int)((double)current / total * width);
putchar('[');
for (i = 0; i < LoopTimes; i++)
putchar('#');
for (i = 0; i < width - LoopTimes; i++)
putchar('-');
printf("] ");
printf("%3.0f%%", (double)current / total * 100);
}
int main(void)
{
int start = 1, end = 285;
int i;
for (i = start; i <= end; i++) {
DisplayProgress(i, end);
usleep(100000);
}
printf(" Done!\n");
return 0;
}