" 下面是一个使用C语言编写的简单进度条代码示例。请注意,这个进度条仅在命令行界面中显示,它不会阻塞程序的执行。
```c
#include <stdio.h>
#include <unistd.h>
void print_progress_bar(int progress, int total) {
int percentage = (progress * 100) / total;
int bar_width = 50;
printf("\rProgress: [");
for (int i = 0; i < bar_width; i++) {
if (i < (bar_width * percentage) / 100) {
printf("=");
} else {
printf(" ");
}
}
printf("] %d%%", percentage);
fflush(stdout);
}
int main() {
int total_items = 100;
int current_item = 0;
while (current_item < total_items) {
printf("Processing item %d of %d\n", current_item + 1, total_items);
print_progress_bar(current_item + 1, total_items);
current_item++;
usleep(100000); // 模拟耗时操作
}
printf("\nProcessing completed.\n");
return 0;
}
```
这个程序创建了一个简单的进度条,它会显示当前处理的任务项以及总任务项。在处理每个任务项时,进度条会根据当前任务项的进度更新。这个进度条代码可以方便地集成到其他程序中,以显示任务进度。"