鱼C论坛

 找回密码
 立即注册
查看: 892|回复: 2

[已解决]百分比加载

[复制链接]
发表于 2018-11-1 19:05:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在windows下,可以用:
  1. #include <iostream>
  2. #include <windows.h>

  3. using namespace std;

  4. int main(void)
  5. {
  6.         cout << "加载中...   ";
  7.         for(int i = 0; i <=100; i++)
  8.         {
  9.                 cout.width(3);
  10.                 cout << i << "%";
  11.                 Sleep(20);
  12.                 cout << "\b\b\b\b";
  13.         }
  14.         cout << '\n';

  15.         return 0;
  16. }
复制代码


在linux下,使用:
  1. #include <iostream>
  2. #include <unistd.h>

  3. using namespace std;

  4. int main(void)
  5. {
  6.         for(int i = 0; i <= 100; i++)
  7.         {
  8.                 cout.width(3);
  9.                 cout << i << "%";
  10.                 usleep(500);
  11.                 cout << "\b\b\b\b";
  12.         }
  13.         cout << '\n';
  14.        
  15.         return 0;
  16. }
复制代码



在linux下无法实现类似windows下的动态效果,应该如何调整 ?
最佳答案
2018-11-1 20:45:34
  1. #include <stdio.h>
  2. #include <unistd.h>

  3. int main ()
  4. {
  5.         int i;
  6.     for(i=1;i<10;i++)
  7.     {
  8.         printf("\33[2K\r");
  9.         printf("安装进度:%d%%",i);
  10.         fflush(stdout);
  11.         usleep(1000*1000);
  12.     }
  13.     printf("\n");
  14.     return 0;
  15. }


  16. #include <stdio.h>
  17. #include <iostream>
  18. #include <unistd.h>

  19. using namespace std;

  20. int main(void)
  21. {
  22.         for(int i = 0; i <= 100; i++)
  23.         {
  24.                 cout << "\33[2K\r";
  25.                 cout << i << "%";
  26.                 fflush(stdout);
  27.                 usleep(1000*1000);
  28.         }
  29.         cout << '\n';
  30.         
  31.         return 0;
  32. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-11-1 19:12:51 | 显示全部楼层
测试是在linux的下Terminal下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-1 20:45:34 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <unistd.h>

  3. int main ()
  4. {
  5.         int i;
  6.     for(i=1;i<10;i++)
  7.     {
  8.         printf("\33[2K\r");
  9.         printf("安装进度:%d%%",i);
  10.         fflush(stdout);
  11.         usleep(1000*1000);
  12.     }
  13.     printf("\n");
  14.     return 0;
  15. }


  16. #include <stdio.h>
  17. #include <iostream>
  18. #include <unistd.h>

  19. using namespace std;

  20. int main(void)
  21. {
  22.         for(int i = 0; i <= 100; i++)
  23.         {
  24.                 cout << "\33[2K\r";
  25.                 cout << i << "%";
  26.                 fflush(stdout);
  27.                 usleep(1000*1000);
  28.         }
  29.         cout << '\n';
  30.         
  31.         return 0;
  32. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-17 05:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表