基于C++編寫一個進度條的示例代碼
更新時間:2023年06月29日 08:19:35 作者:咩~~
這篇文章主要為大家詳細介紹了如何利用C++實現(xiàn)一個命令行進度條,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下
實現(xiàn)一個命令行進度條,使用線程,不會換行歐。支持自定義進度條的條的字符,可以暫停和繼續(xù)。
在寫的過程中還遇到一個錯誤,之前多線程寫的少不知道,貼出來給大家看一下:
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++實現(xiàn)進度條的代碼,希望對大家有所幫助
方法一:
#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] = { '-', '\\', '|', '/' }; //動態(tài)旋轉符號 int i, j, x = 0, y = 2; //坐標 HANDLE Console; char Title[256] = "進度:"; float persent = 0; int times = 0;//用來計算次數(shù) 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 << '%';//顯示百分比,跳轉規(guī)律為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++進度條內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Cocos2d-x保存用戶游戲數(shù)據(jù)之XML文件是否存在問題判斷方法
這篇文章主要介紹了Cocos2d-x保存用戶游戲數(shù)據(jù)之XML文件是否存在問題判斷方法,請注意代碼中包含大量注釋,需要的朋友可以參考下2014-09-09