制作控制台进度条?(Windows)
Making a console progress bar? (Windows)
所以我有一个函数(或者更确切地说,我稍后将把它变成一个函数)在控制台窗口中随机显示%进度;这样的:
#include <iostream>
#include <time.h>
#include <cmath>
#include <windows.h>
using namespace std;
int main()
{
srand(time(0));
int x = 0;
for(int i = 0; i<100; i++){
int r = rand() % 1000;
x++;
cout << "r" << x << "% completed." << flush;
if(i < 43){
Sleep(r/6);
}else if(i > 43 && i < 74){
Sleep(r/8);
}else if(i < 98){
Sleep(r/5);
}else if(i > 97 && i != 99){
Sleep(2000);
}
}
cout << endl << endl << "Operation completed successfully.n" << flush;
return 0;
}
问题是,我希望输出是这样的:
1% completed
|
(后来…)
25% completed
|||||||||||||||||||||||||
我该怎么做呢?
提前感谢!
打印字符'r'
很有用。它将光标置于行首。
因为你不能再访问前一行,你可以像这样:
25% completed: ||||||||||||||||||
每次迭代后:
int X;
...
std::cout << "r" << percent << "% completed: ";
std::cout << std::string(X, '|');
std::cout.flush();
还可以使用:便携式基于文本的控制台操纵器
我觉得这样更好看:
#include <iostream>
#include <iomanip>
#include <time.h>
#include <cmath>
#include <windows.h>
#include <string>
using namespace std;
string printProg(int);
int main()
{
srand(time(0));
int x = 0;
cout << "Working ..." << endl;
for(int i = 0; i<100; i++){
int r = rand() % 1000;
x++;
cout << "r" << setw(-20) << printProg(x) << " " << x << "% completed." << flush;
if(i < 43){
Sleep(r/6);
}else if(i > 43 && i < 74){
Sleep(r/8);
}else if(i < 98){
Sleep(r/5);
}else if(i > 97 && i != 99){
Sleep(1000);
}
}
cout << endl << endl << "Operation completed successfully.n" << flush;
return 0;
}
string printProg(int x){
string s;
s="[";
for (int i=1;i<=(100/2);i++){
if (i<=(x/2) || x==100)
s+="=";
else if (i==(x/2))
s+=">";
else
s+=" ";
}
s+="]";
return s;
}
使用graphics.h或使用更高级的WinBGI库。下载它并将库文件和graphics.h文件放在项目中的适当位置。然后使用名为gotoxy(int x, int y)的函数,其中x和y位于字符位置(而不是像素)。假设你的控制台窗口位于笛卡尔二维轴系统的第四象限。但是x和y通常从1到n开始(取决于控制台窗口的大小)。你只需要在每次出现这样的进度时清除屏幕
system("cls");
as CLS是用于windows的命令。否则,对于linux/Mac使用
system("clear");
现在这个函数在stdlib.h头文件中。之后,您可以轻松地更新进度条并在其中的任何地方写入内容。但是您正在使用的进度条是不连续的。更有效的方法是使用
# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█'):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print('r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = 'r')
# Print New Line on Complete
if iteration == total:
print()
#
# Sample Usage
#
from time import sleep
# A List of Items
items = list(range(0, 57))
l = len(items)
# Initial call to print 0% progress
printProgressBar(0, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
for i, item in enumerate(items):
# Do stuff...
sleep(0.1)
# Update Progress Bar
printProgressBar(i + 1, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
# Sample Output
Progress: |█████████████████████████████████████████████-----| 90.0% Complete
相关文章:
- C++程序不会从 Windows 控制台运行
- 如何从Windows控制台调用.exe(C++)以在不同的目录(或任何目录)中创建文件夹?
- Windows7+:如何构建不需要任何额外/外部运行时库的C++ Windows 控制台应用程序?
- Windows 控制台代码页 866
- 何时调用 Windows 控制台应用程序需要 CoInitialize
- 使用具有 UTF8 编码的源文件将 UTF8 符号打印到 Windows 控制台
- 打印到 Windows 控制台度数 (°) 和立方体符号 (³)
- 为什么我的Windows控制台关闭事件处理程序超时
- 子进程 c++ 的 Windows 控制台信号处理
- 处理运行的Windows控制台应用程序中的拖放文件
- Windows 控制台应用 (C++) 具有错误的退出代码
- 如何通过C++Windows控制台修改光标
- 如何将消息从一个 Windows 控制台应用程序发送到另一个应用程序
- 基于Windows控制台的游戏
- 这是否可以挂接子进程发送到 Windows 控制台的消息
- Windows 控制台应用在新控制台而不是同一控制台中打开
- 如何获取Windows控制台高度
- 调整 Windows 控制台和 MFC 静态库的大小
- 如何将国际字符发送到 Windows 控制台
- Qt:无法使用QProcess启动Windows控制台