返回顶部

linux下使用c++模拟下载进度

#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>

void showProgressBar(double progress) {
    const int barWidth = 70;

    std::cout << "\r[";

    int pos = static_cast<int>(barWidth * progress);
    for (int i = 0; i < barWidth; ++i) {
        if (i < pos) std::cout << "=";
        else if (i == pos) std::cout << ">";
        else std::cout << " ";
    }

    std::cout << "] " << std::setw(3) << static_cast<int>(progress * 100.0) << "% ";
    std::cout << std::flush;
}

int main() {
    // 模拟下载进度
    for (double progress = 0.0; progress <= 1.0; progress += 0.01) {
        showProgressBar(progress);
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    std::cout << std::endl;

    return 0;
}

效果如下,只作为参考,可自行开发

image

image

posted @ 2024-05-15 16:48  十方央丶  阅读(26)  评论(0)    收藏  举报