基于C++編寫一個進度條的示例代碼
實現(xiàn)一個命令行進度條,使用線程,不會換行歐。支持自定義進度條的條的字符,可以暫停和繼續(xù)。
在寫的過程中還遇到一個錯誤,之前多線程寫的少不知道,貼出來給大家看一下:
terminate called without an active exception
這是線程異常終止了,在我的代碼里就是線程沒結(jié)束主線程結(jié)束了,就直接拋錯了。解決方法就是加個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(){//設(shè)置顏色-藍底白字
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),159);
}
void SetPos(int x,int y){//設(shè)置光標處與控制臺的位置
HANDLE Handle;
COORD pos={y,x};
Handle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Handle,pos);
}
int main(){
SetConsoleTitleA("作者網(wǎng)址:https://blog.csdn.net/qq_56187979?spm=1001.2100.3001.5343");
system("mode con cols=100 lines=30");//設(shè)置控制臺大小,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)旋轉(zhuǎn)符號
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); //此設(shè)置為恢復(fù)默認,即黑色背景,高亮文字。
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); //設(shè)置控制臺字體&背景顏色
gotoxy(x + strlen(Title)+times, y);//光標移動到文字后面得位置
cout << '_';
times++;
persent = (i / 5) * 5;
}
//美觀顯示
SetConsoleTextAttribute(Console, FOREGROUND_INTENSITY); //此設(shè)置為恢復(fù)默認,即黑色背景,高亮文字。
gotoxy(x + strlen(Title) + 20, y);//光標移動到文字后面的位置
cout << persent << '%';//顯示百分比,跳轉(zhuǎn)規(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();
}到此這篇關(guān)于基于C++編寫一個進度條的示例代碼的文章就介紹到這了,更多相關(guān)C++進度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++的try塊與異常處理及調(diào)試技術(shù)實例解析
這篇文章主要介紹了C++的try塊與異常處理及調(diào)試技術(shù)實例解析,有助于讀者加深對try塊調(diào)試技術(shù)的認識,需要的朋友可以參考下2014-07-07
opencv3/C++關(guān)于移動對象的輪廓的跟蹤詳解
今天小編就為大家分享一篇opencv3/C++關(guān)于移動對象的輪廓的跟蹤詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Cocos2d-x保存用戶游戲數(shù)據(jù)之XML文件是否存在問題判斷方法
這篇文章主要介紹了Cocos2d-x保存用戶游戲數(shù)據(jù)之XML文件是否存在問題判斷方法,請注意代碼中包含大量注釋,需要的朋友可以參考下2014-09-09

