hello 好久不见!



最近冷静下来了,当自己的实力还撑不起自己的野心和欲望时,此刻就需要静下来学习

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

目录

【本节课知识点画板】

前奏知识点

1、回车和换行

2、⾏缓冲区

五、练手—倒计时小程序

六、进度条

process.h

process.c

main.c


【本节课知识点画板】

前奏知识点

1、回车和换行

  • 回⻋概念
  • 换⾏概念
  • ⽼式打字机的例⼦
  • \r&&\n

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

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

2、⾏缓冲区

什么现象?

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

什么现象??

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

什么现象???

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

五、练手—倒计时小程序

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

六、进度条

process.h

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

process.c

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]\r",buffer,rate,lable[index++]);fflush(stdout);index %= len;if(num >= 100){printf("\n");}}//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]\r",buffer,rate,lable[rate%len]);fflush(stdout);buffer[rate] = STYLE; rate++;usleep(50000);}printf("\n");}

main.c

#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;  }

完——

告诉自己,要多回顾,多练习,多敲代码,不要害怕思考


盛夏的告别——

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

至此结束——

我是云边有个稻草人

期待与你的下一次相遇

再见!