欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C++編寫(xiě)一個(gè)進(jìn)度條的示例代碼

 更新時(shí)間:2023年06月29日 08:19:35   作者:咩~~  
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)一個(gè)命令行進(jìn)度條,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下

實(shí)現(xiàn)一個(gè)命令行進(jìn)度條,使用線程,不會(huì)換行歐。支持自定義進(jìn)度條的條的字符,可以暫停和繼續(xù)。

在寫(xiě)的過(guò)程中還遇到一個(gè)錯(cuò)誤,之前多線程寫(xiě)的少不知道,貼出來(lái)給大家看一下:

terminate called without an active exception

這是線程異常終止了,在我的代碼里就是線程沒(méi)結(jié)束主線程結(jié)束了,就直接拋錯(cuò)了。解決方法就是加個(gè)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)度條的代碼,希望對(duì)大家有所幫助

方法一:

#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)處與控制臺(tái)的位置
    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è)置控制臺(tái)大小,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秒,可在此時(shí)進(jìn)行文件加載
	}
	//再次以藍(lán)背景的形式覆蓋輸出
	char ch;
	ch=getch();
	//輸入任意鍵退出
	return 0;
}

如果電腦運(yùn)行不了這個(gè)程序的話,請(qǐng)檢查C++版本是否達(dá)到6.0,或者是在運(yùn)行不了的話,可點(diǎn)擊

方法二:控制臺(tái)顯示進(jìn)度條

#include "stdafx.h"
#include<iostream>
#include "windows.h"
using namespace std;
//光標(biāo)移動(dòng)到指定位置
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] = { '-', '\\', '|', '/' }; //動(dòng)態(tài)旋轉(zhuǎn)符號(hào)
	int i, j, x = 0, y = 2; //坐標(biāo)
	HANDLE Console;
	char Title[256] = "進(jìn)度:";
	float persent = 0;
	int times = 0;//用來(lái)計(jì)算次數(shù)
	Console = GetStdHandle(STD_OUTPUT_HANDLE);//獲取控制臺(tái)句柄
	gotoxy(x, y);//光標(biāo)移動(dòng)到指定位置
	SetConsoleTextAttribute(Console, FOREGROUND_INTENSITY);  //此設(shè)置為恢復(fù)默認(rèn),即黑色背景,高亮文字。
	cout << Title;
	//用20個(gè)點(diǎn)來(lái)占位
	for (i = 0; i < 20; ++i)
	{
		cout << '.';
	}
	//修改 '.' 為 '_'
	for (i = 0; i <= 100; ++i)
	{
		if (i % 5 == 0)
		{
			SetConsoleTextAttribute(Console, FOREGROUND_GREEN | BACKGROUND_GREEN);  //設(shè)置控制臺(tái)字體&背景顏色
			gotoxy(x + strlen(Title)+times, y);//光標(biāo)移動(dòng)到文字后面得位置
			cout << '_';
			times++;
			persent = (i / 5) * 5;
		}
		//美觀顯示
		SetConsoleTextAttribute(Console, FOREGROUND_INTENSITY);  //此設(shè)置為恢復(fù)默認(rèn),即黑色背景,高亮文字。
		gotoxy(x + strlen(Title) + 20, y);//光標(biāo)移動(dòng)到文字后面的位置
		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++編寫(xiě)一個(gè)進(jìn)度條的示例代碼的文章就介紹到這了,更多相關(guān)C++進(jìn)度條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論