基于C++編寫一個進(jìn)度條的示例代碼
實(shí)現(xiàn)一個命令行進(jìn)度條,使用線程,不會換行歐。支持自定義進(jì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;
}方法補(bǔ)充
除了上文的方法,小編還為大家整理了其他C++實(shí)現(xiàn)進(jìn)度條的代碼,希望對大家有所幫助
方法一:
#include<windows.h>
#include<iostream>
#include<conio.h>
using namespace std;
void color(){//設(shè)置顏色-藍(lán)底白字
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),159);
}
void SetPos(int x,int y){//設(shè)置光標(biāo)處與控制臺的位置
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秒,可在此時進(jìn)行文件加載
}
//再次以藍(lán)背景的形式覆蓋輸出
char ch;
ch=getch();
//輸入任意鍵退出
return 0;
}如果電腦運(yùn)行不了這個程序的話,請檢查C++版本是否達(dá)到6.0,或者是在運(yùn)行不了的話,可點(diǎn)擊
方法二:控制臺顯示進(jìn)度條
#include "stdafx.h"
#include<iostream>
#include "windows.h"
using namespace std;
//光標(biāo)移動到指定位置
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; //坐標(biāo)
HANDLE Console;
char Title[256] = "進(jìn)度:";
float persent = 0;
int times = 0;//用來計(jì)算次數(shù)
Console = GetStdHandle(STD_OUTPUT_HANDLE);//獲取控制臺句柄
gotoxy(x, y);//光標(biāo)移動到指定位置
SetConsoleTextAttribute(Console, FOREGROUND_INTENSITY); //此設(shè)置為恢復(fù)默認(rèn),即黑色背景,高亮文字。
cout << Title;
//用20個點(diǎn)來占位
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);//光標(biāo)移動到文字后面得位置
cout << '_';
times++;
persent = (i / 5) * 5;
}
//美觀顯示
SetConsoleTextAttribute(Console, FOREGROUND_INTENSITY); //此設(shè)置為恢復(fù)默認(rèn),即黑色背景,高亮文字。
gotoxy(x + strlen(Title) + 20, y);//光標(biāo)移動到文字后面的位置
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); //控制程序運(yùn)行速度
}
getchar();
}到此這篇關(guān)于基于C++編寫一個進(jìn)度條的示例代碼的文章就介紹到這了,更多相關(guān)C++進(jìn)度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
純c語言實(shí)現(xiàn)面向?qū)ο蠓治雠c示例分享
采用C語言實(shí)現(xiàn)的關(guān)鍵是如何運(yùn)用C語言本身的特性來實(shí)現(xiàn)多態(tài)、繼承面、封裝的面向?qū)ο蟮奶卣?最近給出了例子,大家可以參考使用2014-01-01
教你在VS2022?MFC程序中調(diào)用CUDA代碼的方法
這篇文章主要介紹了在VS2022?MFC程序中調(diào)用CUDA代碼,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
C++類與對象深入之靜態(tài)成員與友元及內(nèi)部類詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對我們對C++的靜態(tài)成員,友元,內(nèi)部類知識點(diǎn)做出總結(jié),整理出來一篇博客供我們一起復(fù)習(xí)和學(xué)習(xí),如果文章中有理解不當(dāng)?shù)牡胤?還希望朋友們在評論區(qū)指出,我們相互學(xué)習(xí),共同進(jìn)步2022-06-06
VSCode添加頭文件(C/C++)的實(shí)現(xiàn)示例
這篇文章主要介紹了VSCode添加頭文件(C/C++)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

