进度条函数 c 语言,用C++实现一个命令行进度条的示例代码
时间: 2024-03-07 07:48:42 浏览: 100
C语言进度条函数示例代码:
```
#include <stdio.h>
void progress_bar(int current, int total) {
int i;
int percentage = current * 100 / total;
printf("[");
for (i = 0; i < 50; i++) {
if (i < percentage / 2) {
printf("=");
} else {
printf(" ");
}
}
printf("] %d%%\r", percentage);
fflush(stdout);
}
int main() {
int i;
for (i = 0; i <= 100; i++) {
progress_bar(i, 100);
sleep(1);
}
printf("\n");
return 0;
}
```
C++语言进度条示例代码:
```
#include <iostream>
#include <chrono>
#include <thread>
void progress_bar(int current, int total) {
int i;
int percentage = current * 100 / total;
std::cout << "[";
for (i = 0; i < 50; i++) {
if (i < percentage / 2) {
std::cout << "=";
} else {
std::cout << " ";
}
}
std::cout << "] " << percentage << "%\r";
std::cout.flush();
}
int main() {
int i;
for (i = 0; i <= 100; i++) {
progress_bar(i, 100);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << std::endl;
return 0;
}
```
阅读全文