基于C++编写一个进度条的示例代码

Nita ·
更新时间:2024-09-20
· 1684 次阅读

实现一个命令行进度条,使用线程,不会换行欧。支持自定义进度条的条的字符,可以暂停和继续。

在写的过程中还遇到一个错误,之前多线程写的少不知道,贴出来给大家看一下:

terminate called without an active exception

这是线程异常终止了,在我的代码里就是线程没结束主线程结束了,就直接抛错了。解决方法就是加个join

class ProgressBar{ private: class Logic{ public: static int barLen; static int curLen; static string str; static condition_variable _cv; static char ch; void operator()(){ unique_lock<mutex> lock(barMutex); for(int i=0;i<=barLen;i++){ _cv.wait(lock,[]()->bool{ return !ProgressBar::_pause; }); str[i]=ch; cout<<"\r|"<<str<<"| "<<(int)i*100/barLen<<"%"; Sleep(200); } } }; public: static void Start(const int _barLen = 100, const char _ch = '='){ ProgressBar::Logic::barLen=_barLen; ProgressBar::Logic::ch=_ch; ProgressBar::_pause=false; ProgressBar::Logic::str=string(_barLen,' '); ProgressBar::run = thread(Logic()); } // static void Start(){run.join();} static void Pause(){ ProgressBar::_pause=true; } static void Continue(){ ProgressBar::_pause=false; Logic::_cv.notify_one(); } public: static bool _pause; static mutex barMutex; static thread run; }; int ProgressBar::Logic::barLen = 100; int ProgressBar::Logic::curLen = 0; thread ProgressBar::run; string ProgressBar::Logic::str = ""; bool ProgressBar::_pause = false; char ProgressBar::Logic::ch = '='; condition_variable ProgressBar::Logic::_cv; mutex ProgressBar::barMutex; int main(){ // ProgressBar::Init(); ProgressBar::Start(50,'+'); Sleep(2000); ProgressBar::Pause(); Sleep(5000); ProgressBar::Continue(); ProgressBar::run.join(); return 0; }

方法补充

除了上文的方法,小编还为大家整理了其他C++实现进度条的代码,希望对大家有所帮助

方法一:

#include<windows.h> #include<iostream> #include<conio.h> using namespace std; void color(){//设置颜色-蓝底白字 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),159); } void SetPos(int x,int y){//设置光标处与控制台的位置 HANDLE Handle; COORD pos={y,x}; Handle=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(Handle,pos); } int main(){ SetConsoleTitleA("作者网址:https://blog.csdn.net/qq_56187979?spm=1001.2100.3001.5343"); system("mode con cols=100 lines=30");//设置控制台大小,30行100列 for(int i=35;i<=64;i++){ SetPos(20,i); cout<<"_"; SetPos(21,i); cout<<"_"; Sleep(30); } //先输出两条横线 color(); for(int i=0;i<=29;i++){ SetPos(21,35+i); cout<<"_"; Sleep(30);//停止30秒,可在此时进行文件加载 } //再次以蓝背景的形式覆盖输出 char ch; ch=getch(); //输入任意键退出 return 0; }

如果电脑运行不了这个程序的话,请检查C++版本是否达到6.0,或者是在运行不了的话,可点击

方法二:控制台显示进度条

#include "stdafx.h" #include<iostream> #include "windows.h" using namespace std; //光标移动到指定位置 void gotoxy(int x, int y) { HANDLE Console; COORD Loc; Loc.X = x; Loc.Y = y; Console = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(Console, Loc); return; } int _tmain(int argc, _TCHAR* argv[]) { char Sign[4] = { '-', '\\', '|', '/' }; //动态旋转符号 int i, j, x = 0, y = 2; //坐标 HANDLE Console; char Title[256] = "进度:"; float persent = 0; int times = 0;//用来计算次数 Console = GetStdHandle(STD_OUTPUT_HANDLE);//获取控制台句柄 gotoxy(x, y);//光标移动到指定位置 SetConsoleTextAttribute(Console, FOREGROUND_INTENSITY); //此设置为恢复默认,即黑色背景,高亮文字。 cout << Title; //用20个点来占位 for (i = 0; i < 20; ++i) { cout << '.'; } //修改 '.' 为 '_' for (i = 0; i <= 100; ++i) { if (i % 5 == 0) { SetConsoleTextAttribute(Console, FOREGROUND_GREEN | BACKGROUND_GREEN); //设置控制台字体&背景颜色 gotoxy(x + strlen(Title)+times, y);//光标移动到文字后面得位置 cout << '_'; times++; persent = (i / 5) * 5; } //美观显示 SetConsoleTextAttribute(Console, FOREGROUND_INTENSITY); //此设置为恢复默认,即黑色背景,高亮文字。 gotoxy(x + strlen(Title) + 20, y);//光标移动到文字后面的位置 cout << persent << '%';//显示百分比,跳转规律为5,10,15,20…… cout << Sign[i % 4] << Sign[i % 4] << Sign[i % 4]; cout << "——(*^_^*)——"; cout << Sign[i % 4] << Sign[i % 4] << Sign[i % 4]; Sleep(100); //控制程序运行速度 } getchar(); }

到此这篇关于基于C++编写一个进度条的示例代码的文章就介绍到这了,更多相关C++进度条内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



c+ 进度条 示例 C++

需要 登录 后方可回复, 如果你还没有账号请 注册新账号