【Linux系统】第六节—简易倒计时+进度条(详解)

【本节课知识点画板】前奏知识点1、回车和换行回⻋概念换⾏概念⽼式打字机的例⼦ &&

【Linux系统】第六节—简易倒计时+进度条(详解)
【Linux系统】第六节—简易倒计时+进度条(详解)

2、⾏缓冲区

什么现象?

代码语言:JavaScript代码运行次数:0运行复制

#include <stdio.h>int main(){    printf("hello bite! ");    sleep(3);    return 0;}

什么现象??

代码语言:javascript代码运行次数:0运行复制

#include <stdio.h>int main(){    printf("hello bite!");    sleep(3);    return 0;}

什么现象???

代码语言:javascript代码运行次数:0运行复制

#include <stdio.h>int main(){    printf("hello bite!");    fflush(stdout);    sleep(3);    return 0;}

五、练手—倒计时小程序代码语言:javascript代码运行次数:0运行复制

#include<stdio.h>#include<unistd.h>int main(){    int cnt = 10;    while(cnt >= 0)    {        printf("%-2d ",cnt);        fflush(stdout);        sleep(1);        cnt--;    }    printf(" ");    return 0;}

六、进度条process.h代码语言:javascript代码运行次数:0运行复制

#pragma once#include<stdio.h>void FlushProcess(); 

process.c代码语言:javascript代码运行次数:0运行复制

define SIZE 101  #define STYLE '#'    //v2:根据进度,动态刷新进度条  void FlushProcess(double total,double current)  {      const char* lable = "|/-";      int len = strlen(lable);      static index = 0;      char buffer[SIZE];      memset(buffer,0,sizeof(buffer));        double rate = current*100/total;      int num = (int)rate;        int i = 0;      for(;i < num;i++)      {          buffer[i] = STYLE;      }      printf("[%-100s][%.1lf%%][%c] ",buffer,rate,lable[index++]);      fflush(stdout);      index %= len;        if(num >= 100)      {          printf(" ");      }    }//v1:展示进度条的基本功能  void process()  {      int rate = 0;      char buffer[SIZE];      memset(buffer,0 , sizeof(buffer));      const char* lable="|/-";      int len = strlen(lable);        while(rate <= 100)      {          printf("[%-100s][%d%%][%c] ",buffer,rate,lable[rate%len]);          fflush(stdout);          buffer[rate] = STYLE;           rate++;          usleep(50000);      }        printf(" ");  }

main.c代码语言:javascript代码运行次数:0运行复制

#include"process.h"  #include<stdlib.h>  #include<time.h>        double total = 1024.0;      double speed[] = {1.0,0.5,0.2,0.1,0.01,0.02};            void download()      {          double current = 0.0;          srand(time(NULL));                while(current <= total)          {             FlushProcess(total,current);                    if(current == total)                  break;                    //下载代码              int random = rand()%6;              usleep(5000);              current += speed[random];              if(current >= total)              {                  current = total;               }                      }            }                                                                                                                                                                                      int main()      {          download();                return 0;    }

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享