C语言:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
int main()
{
printf("正在安装......");;
for(int i = 0; i <= 100; i++)
{
printf("%3d%%", i);
Sleep(50);
printf("\b\b\b\b");
}
printf("\n安装完成, 请按任意键退出\n");
getch();
return 0;
}
C++:
#include <iostream>
#include <conio.h>
#include <windows.h>
int main()
{
std::cout<< "正在安装......";
for(int i = 0; i <= 100; i++)
{
std::cout.width(3);
std::cout << i << "%";
Sleep(50);
std::cout<< "\b\b\b\b";
}
getch();
return 0;
}
关健应是那个 printf("%3d"); 与 std::cout.width(3); 的写法与后面的 \b 退格。