0
点赞
收藏
分享

微信扫一扫

Linux实现:从倒计时到进度条

书呆鱼 2024-01-30 阅读 10

目录

回车和换行

缓冲区

设计倒计时

进度条(多文件操作)

Version1:进度条

Version2:应用场景+进度条

Processbar.h

Processbar.c

Main.c

Makefile 

Version3:升级彩色进度条


回车和换行

缓冲区

  1 #include<stdio.h>
  2 #include<unistd.h>//sleep
  3 int main()
  4 {
  5   printf("hello linux,hello word");                                                                          
  6   sleep(3);
  7   return 0;
  8 }
  1 #include<stdio.h>
  2 #include<unistd.h>//sleep
  3 int main()
  4 {
  5   printf("hello linux,hello word\n");                                                                          
  6   sleep(3);
  7   return 0;
  8 }

屏幕录制 2024-01-27 194734

//输出流--显示器/终端
extern FILE * stdout;
extern FILE * stderr;
//输入流--键盘
extern FILE * stdin;
//开机默认打开三个流(输入/输出流)
  //修改之后
  1 #include<stdio.h>
  2 #include<unistd.h>//sleep
  3 int main()
  4 {
  5   printf("hello linux,hello word");
  6   fflush(stdout);                                                                                            
  7   sleep(3);                                                                                
  8   return 0;                                                                                
  9 } 

设计倒计时

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 int main()
  4 {
  5   int cnt=10;
  6   while(cnt>=0)
  7   {
  8     printf("倒计时:%2d\r",cnt);//2d  
  9     fflush(stdout);//刷新数据
 10     cnt--;
 11     sleep(1);//休眠
 12   }
 13   printf("\n");                                                                                              
 14   return 0;
 15 }

最终呈现效果: 

屏幕录制 2024-01-28 122350

进度条(多文件操作)

 测试一下:

Version1:进度条

#include"Processbar.h"
  2 //void test()
  3 //{
  4 //  printf("this is a test\n");
  5 //  printf("this is a test\n");
  6 //  printf("this is a test\n");
  7 //  printf("this is a test\n");
  8 //  printf("this is a test\n");
  9 //  printf("this is a test\n");
 10 //}     
                                                                                                     
 11 #define length 101 //包括\0
 12 #define DataType '#'
 13 const char*MoveType = "|/-\\";
 14 void ProcBar()
 15 {
 16   char Bar[length];//缓存空间
 17   memset(Bar,'\0',sizeof(Bar));
 18   int cnt=0;
 19   while(cnt<=100)
 20   {
 21     printf("[%-100s][%3d%%][%c]\r",Bar,cnt,MoveType[cnt%strlen(MoveType)]);
 22     fflush(stdout);
 23     Bar[cnt++]=DataType;
 24     usleep(20000);//0.02s=20000微秒
 25   }
 26   printf("\n");
 27 }

屏幕录制 2024-01-28 151404


 Version2:应用场景+进度条

Processbar.h

  1 #pragma once //防止头文件被重复包含
  2 #include<stdio.h>
  3 #include<unistd.h>
  4 #include<string.h>
  5 //extern 表示此函数/变量在其他文件别处定义
  6 //extern void test();
  7 typedef void(*callbact_t)(double,double);
  8 void ProcBar(double filesize,double currentsize);
  9 // void Download(double filesize,callbact_t cb); 

Processbar.c

    #include"Processbar.h"
 34 #define length 101 //包括\0
 35 #define DataType '#'
 36                                                                                                              
 37 const char*MoveType = "|/-\\";
 38 void ProcBar(double filesize,double currentsize)
 39 {
 40   char Bar[length];//缓存空间
 41   memset(Bar,'\0',sizeof(Bar));
 42   int cnt=0;
 43   double rate=currentsize*100.0/filesize;//占比
 44   int count=(int)rate;
 45   while(cnt <= count)
 46   {
 47    // printf("[%-100s][%.1lf%%][%c]\r",Bar,rate,MoveType[cnt%strlen(MoveType)]);
 48    // fflush(stdout);
 49     Bar[cnt++]=DataType;
 50    // usleep(20000);//0.02s=20000微秒
 51   }
 52     printf("[%-100s][%.1lf%%][%c]\r",Bar,rate,MoveType[cnt%strlen(MoveType)]);
 53     fflush(stdout);
 54 }

Main.c

  1 #include"Processbar.h"
  2 //设置一个场景
  3 //double filesize=100*1024*1024*1.0;//总的文件大小
  4 //double currentsize=0.0;//目前下载的文件数
  5 double bandwidth=1024*1024*1.0;
  6 void Download(double filesize,callbact_t cb)                                                                 
  7 {
  8   double currentsize=0.0;
  9     printf("Download begin,currentsize:%lf\n",currentsize);
 10   while(currentsize <= filesize)
 11   {
 12     cb(filesize,currentsize);
 13     usleep(20000);
 14     currentsize+=bandwidth;
 15   }
 16      printf("\nDownload done,currentsize:%lf\n",currentsize);
 17 }
 18 int main()
 19 {
 20   Download(100*1024*1024*1.0,ProcBar);
 21  //ProcBar();
 22  //test();
 23   return 0;
 24 }

Makefile 


  1 Processbar:Processbar.c Main.c
  2   @gcc -o $@ $^
  3 .PHONY:clean
  4 clean:
  5   @rm -f Processbar 

屏幕录制 2024-01-29 140123


Version3:升级彩色进度条

#include<stdio.h>
#include<unistd.h>
#include<string.h>

int main()
{
    int k=0;
     int j=0;
     int color[]={4,3,2,5,6};
     char bar[102];
     memset(bar,0,sizeof(bar));
     const char* str="|/-\\";
      for(;k<=100;++k)
     {
         printf("\033[3%dm[%-100s]\033[0m\033[33m[%d%%]\033[0m[%c]\r",                  
             color[j], bar, k, str[k % 4]);
        fflush(stdout);
        bar[k]='*';
        //每20%变色一次
        if(k%20==0)
        {
            ++j;
        }
        usleep(7000);
     }
     printf("\n");
     return 0;
 }

🙂感谢大家的阅读,若有错误和不足,欢迎指正

举报

相关推荐

0 条评论