1 打印百分比进度
#include<iostream>
#include<Windows.h> //Sleep()所需头文件
#include <iomanip> //setw()等控制符所需头文件
using namespace std;
int main()
{
int i = 0;
cout << "\n当前进度:";
while(i<=10000)
{
cout << "\r";//回到行首位置
//输出宽度 6,保留两位小数
cout << setw(6) << fixed << setprecision(2) << (float)i / 100 << "%";
Sleep(5);
i++;
}
cout << endl << endl;
system("pause");
return 0;
}
输出结果:
2 打印进度条,并以百分比显示、
代码:
#include<iostream>
#include<Windows.h> //Sleep()所需头文件
#include <iomanip> //setw()所需头文件
using namespace std;
int main()
{
cout << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||100%" << endl;
int i = 1;
while (i <= 100)
{
cout << "\b\b\b\b";
cout << "|" << setw(3) << i << "%";
Sleep(150);
i++;
}
cout << endl << endl;
system("pause");
return 0;
}
结果展示: