当前位置: 首页 > news >正文

萧县做网站北京厦门网站优化

萧县做网站,北京厦门网站优化,个人网站备案需要哪些材料,小程序推广渠道🎉博主首页: 有趣的中国人 🎉专栏首页: Linux 🎉其它专栏: C初阶 | C进阶 | 初阶数据结构 小伙伴们大家好,本片文章将会讲解Linux中进度条的实现的相关内容。 如果看到最后您觉得这篇文章写得…
图片名称
🎉博主首页: 有趣的中国人

🎉专栏首页: Linux

🎉其它专栏: C++初阶 | C++进阶 | 初阶数据结构

在这里插入图片描述

小伙伴们大家好,本片文章将会讲解Linux进度条的实现的相关内容。


如果看到最后您觉得这篇文章写得不错,有所收获,麻烦点赞👍、收藏🌟、留下评论📝。您的支持是我最大的动力,让我们一起努力,共同成长!

文章目录

  • `1. 关于回车 & 换行`
  • `2. 简述缓冲区`
  • `3. 倒计时程序的编写`
  • `4. 简易进度条的实现`



1. 关于回车 & 换行



回车和换行是文本显示和处理的相关术语,我们常常认为这两个是同一概念,实则不然。


👨‍💻回车换行的概念


回车(Carriage Return)在打字机时代,回车指的是将打字机的打印头(称为"carrier")移回到行首的操作在计算机时代,回车通常表示将光标移动到当前行的开头而不会换到下一行。在ASCII字符集中,回车通常用"\r"表示。

换行(Line Feed):换行是指将光标移动到下一行的操作使得文本在纵向上向下移动一个行高。在ASCII字符集中,换行通常用"\n"表示。


在Unix和类Unix系统(如LinuxmacOS)中通常使用换行字符(“\n”)来表示换行。

在Windows系统中:通常使用回车和换行的组合来表示换行,即"\r\n"。



2. 简述缓冲区


缓冲区(Buffer)是计算机内存中的一块特定区域,用于临时存储数据。它在许多计算机系统和应用程序中发挥着重要作用,通常用于临时存储输入数据、输出数据或在内存和其他设备之间进行数据传输。

输入缓冲区:用于暂时存储从输入设备(如键盘、鼠标、网络接口等)接收到的数据,直到程序能够处理它们。输入缓冲区使得程序可以按需处理输入,而不必担心输入数据的速度与程序处理速度不匹配的问题

输出缓冲区:用于暂时存储将要发送到输出设备(如显示器、打印机、网络接口等)的数据,直到设备准备好接收它们。输出缓冲区可以提高数据传输的效率,因为程序不必等待设备就绪就可以继续执行。


👨‍💻缓冲区何时被清理

拿C语言举个例子:

在C语言中,标准库函数printf()用于将格式化的数据打印到标准输出流(通常是终端)。但是,printf()函数并不会立即将数据显示到终端上。相反,它会将数据写入到输出缓冲区中。输出缓冲区是一个临时存储区域,用于存放printf()函数打印的数据,直到满足一定条件时才将其刷新(即将数据发送到终端并显示出来)。

这些条件包括:

1. 遇到换行符 \nprintf()函数遇到换行符时,输出缓冲区会被自动刷新,将缓冲区中的数据输出到终端并显示出来。

2. 缓冲区满:当输出缓冲区满了,它也会被自动刷新。

3.调用fflush()函数:显式调用fflush(stdout)函数可以强制刷新输出缓冲区,将其中的数据输出到终端。

4. 程序结束:当程序正常终止时,所有的缓冲区都会被刷新。



3. 倒计时程序的编写


有了以上的知识储备,咱们就可以尝试编写一下简单的倒计时程序了,思路如下:

  • 首先新建一个time.c文件,然后再用我们之前讲的makefile工具来实现time.c文件的自动构建:

在这里插入图片描述

  • 编写time.c这个文件,实现思路:
  1. 假设我们倒数10s,到0s时结束,因此需要一个循环;
  2. 循环中,我们要实现每次出来一个数,都要对之前的数进行覆盖,所以要用到回车"\r";
  3. 由于printf()函数会会将输出的结果先输出到缓冲区,回车不会冲刷缓冲区,因此每次要用fflush(stdout)强制冲刷缓冲区;
  4. 每次循环秒数减1,并让程序休眠1s

🌝详细代码如下:

#include <stdio.h>
#include <unistd.h>
int main()
{int cnt = 10;while(cnt >= 0){// 打印的时候每次覆盖上一次出现的数字printf("倒计时:%2d\r",cnt);// 强制冲刷缓冲区fflush(stdout);--cnt;sleep(1);}printf("\n");return 0;
}
  • make命令进行编译:(⏳这边就可以动态运行了哈,感兴趣的可以自己试一下⌛)

在这里插入图片描述

  • 这里有个小拓展,如果我们要覆盖上次的数字是4位,这次是三次(比如1000到999),可以用%4d这个输出形式来解决,也可以用下面这种方法:
#include <stdio.h>
#include <unistd.h>
int main()
{int cnt = 1000;int tmp = cnt;int num = 0;while (tmp){++num;tmp /= 10;}while(cnt >= 0){// 主要就是这里的变化,用最大数字的位数来做占位符printf("倒计时:%*d\r",num, cnt);fflush(stdout);--cnt;sleep(1);}printf("\n");return 0;
}


4. 简易进度条的实现



好啦,有了以上的知识作为基础,咱们就可以进入正题啦!😎编写简易的进度条。😎


👨‍💻效果图展示

总共有三个部分:

1. 我们要实现的进度条用#来进行加载;

2. 后面要有数据来表示现在加载的进度是多少(百分数);

3. 最后用一个动态旋转的类来表示程序还在继续加载


在这里插入图片描述

👨‍💻实现思路


1. 动态加载的过程

动态加和之前的倒计时差不多,每次都要覆盖上次出现的#,具体思路如下:


1. 定义一个字符类型数组char *str,用memset()函数进行初始化(‘\0’);

2. 循环100次,每次循环都在数组中加一个#,并打印str('\r’进行覆盖);

3. 强制冲刷缓冲区;


2. 进度加载

我们可以用每次循环的次数来当作是当前加载的进度,当然还要进行覆盖,具体思路如下:


1. 每次循环都以当前的循环次数作为加载进度;

2. 每次覆盖上一次的进度;

3. 强制冲刷缓冲区。

4. 程序休眠(可以用usleep()函数,单位是微秒)


3. 动态旋转

定义一个数组,并初始化为-\\/-,覆盖的方法和之前类似,就不详细说了。


👨‍💻具体代码实现

#include "process_bar.h"
#include <memory.h>
#include <unistd.h>
#define style '#'
#define round "-\\/-"
void test()
{int i = 0;char str[100];memset(str,'\0',sizeof(str));while (i <= 100){str[i] = style;printf("[%-100s][%d%%][%c]\r",str,i,round[i % 4]);fflush(stdout);++i;usleep(10000);}printf("\n");
}

在这里插入图片描述



👨‍💻第二版本

我们正常用进度条肯定不是单独使用的,会结合其他的场景,例如下载界面,登陆界面


对于要下载的文件,肯定有文件大小,下载的时候网络也有它的带宽,所以在下载的时候,每次下载的大小都是一个带宽,我们可以先写一个下载的函数:

download函数:

void download()
{double bandwidth = 1024 * 1024 * 1.0;double filesize = 1024 * 1024 * 10.0;double cur = 0.0;while (cur <= filesize){// 调用进度条函数test(filesize, cur);// 每次增加带宽cur += bandwidth;usleep(20000);}printf("\n");printf("this file has been downloaded\n");
}

进度条函数:

void test(double total, double current)
{char str[101];memset(str,'\0',sizeof(str));int i = 0;// 这次的比率double rate = (current * 100) / total;// 循环次数int loop_count = (int)rate;while (i <= loop_count){str[i++] = style; }printf("[%-100s][%.1lf%%][%c]\r",str,rate,round[loop_count % 4]);fflush(stdout);
}

回调函数版本(完整):

// 头文件 process_bar.h
#include <stdio.h>typedef void(*callback_t)(double, double);// 函数指针(回调函数)void test(double total, double current);// 函数实现文件 process_bar.c
#include "process_bar.h"
#include <memory.h>
#include <unistd.h>
#define style '#'
#define round "-\\/-"void test(double total, double current)
{char str[101];memset(str,'\0',sizeof(str));int i = 0;double rate = (current * 100) / total;int loop_count = (int)rate;while (i <= loop_count){str[i++] = style; }printf("[%-100s][%.1lf%%][%c]\r",str,rate,round[loop_count % 4]);fflush(stdout);
}// main.c 主函数和 download 函数
#include "process_bar.h"
#include <unistd.h>double bandwidth = 1024 * 1024 * 1.0;
void download(double filesize, callback_t cb)
{double cur = 0.0;while (cur <= filesize){cb(filesize, cur);cur += bandwidth;usleep(20000);}printf("\n");printf("this file has been downloaded\n");
}int main()
{download(1024*1024*100.0,test);download(1024*1024*20.0,test);return 0;
}

在这里插入图片描述


文章转载自:
http://dinncolacedaemon.tqpr.cn
http://dinncochloramphenicol.tqpr.cn
http://dinncogangdom.tqpr.cn
http://dinncopinchers.tqpr.cn
http://dinncosteal.tqpr.cn
http://dinncominipark.tqpr.cn
http://dinnconancy.tqpr.cn
http://dinnconurbs.tqpr.cn
http://dinncocollagenous.tqpr.cn
http://dinncolustily.tqpr.cn
http://dinncoaffranchise.tqpr.cn
http://dinncoearcap.tqpr.cn
http://dinncophotocube.tqpr.cn
http://dinncobioacoustics.tqpr.cn
http://dinncoquadrode.tqpr.cn
http://dinncomunicipally.tqpr.cn
http://dinncopumpship.tqpr.cn
http://dinncotythe.tqpr.cn
http://dinncoamphibia.tqpr.cn
http://dinncoplacet.tqpr.cn
http://dinncoshillalah.tqpr.cn
http://dinncohydrogenium.tqpr.cn
http://dinncoriotous.tqpr.cn
http://dinncocelebrative.tqpr.cn
http://dinncoconsignor.tqpr.cn
http://dinncocomedietta.tqpr.cn
http://dinncocokuloris.tqpr.cn
http://dinncodissertate.tqpr.cn
http://dinncoscribal.tqpr.cn
http://dinncobartizan.tqpr.cn
http://dinncodisleave.tqpr.cn
http://dinncoantediluvian.tqpr.cn
http://dinncovisive.tqpr.cn
http://dinncoundelivered.tqpr.cn
http://dinncomicrospecies.tqpr.cn
http://dinncodiakinesis.tqpr.cn
http://dinncofrowsy.tqpr.cn
http://dinncobronco.tqpr.cn
http://dinncoextravert.tqpr.cn
http://dinncoselah.tqpr.cn
http://dinncoduteous.tqpr.cn
http://dinncoalleviant.tqpr.cn
http://dinncodowndrift.tqpr.cn
http://dinncopassenger.tqpr.cn
http://dinncohigher.tqpr.cn
http://dinncocredendum.tqpr.cn
http://dinncoabsinthine.tqpr.cn
http://dinncocraniectomize.tqpr.cn
http://dinncohistaminergic.tqpr.cn
http://dinncoestoppel.tqpr.cn
http://dinncobookstore.tqpr.cn
http://dinncoashore.tqpr.cn
http://dinncoapochromat.tqpr.cn
http://dinncogravely.tqpr.cn
http://dinncoprestidigitation.tqpr.cn
http://dinncoinducement.tqpr.cn
http://dinncosmsa.tqpr.cn
http://dinncoplethora.tqpr.cn
http://dinncoidiorrhythmism.tqpr.cn
http://dinncocelesta.tqpr.cn
http://dinncobivariant.tqpr.cn
http://dinncolithotomist.tqpr.cn
http://dinncohp.tqpr.cn
http://dinncobelie.tqpr.cn
http://dinncotachyon.tqpr.cn
http://dinncochrysograph.tqpr.cn
http://dinncopseudoscorpion.tqpr.cn
http://dinncolongwall.tqpr.cn
http://dinncohydrolysate.tqpr.cn
http://dinncogrumpily.tqpr.cn
http://dinncojokiness.tqpr.cn
http://dinncocrimper.tqpr.cn
http://dinncoquietus.tqpr.cn
http://dinnconostologic.tqpr.cn
http://dinncoarborous.tqpr.cn
http://dinncotrisubstituted.tqpr.cn
http://dinncotasses.tqpr.cn
http://dinncotrouse.tqpr.cn
http://dinncopromising.tqpr.cn
http://dinncochallis.tqpr.cn
http://dinncozooman.tqpr.cn
http://dinncodemandeur.tqpr.cn
http://dinncomacaw.tqpr.cn
http://dinncobaboosh.tqpr.cn
http://dinncoundernourish.tqpr.cn
http://dinncofroward.tqpr.cn
http://dinncodenaturant.tqpr.cn
http://dinncopalmar.tqpr.cn
http://dinncosop.tqpr.cn
http://dinncoplantsman.tqpr.cn
http://dinncocurbside.tqpr.cn
http://dinncomonophyletic.tqpr.cn
http://dinncosuffocating.tqpr.cn
http://dinncohubris.tqpr.cn
http://dinncosuperload.tqpr.cn
http://dinncofuguist.tqpr.cn
http://dinncofactionalism.tqpr.cn
http://dinncocommunist.tqpr.cn
http://dinncofaithful.tqpr.cn
http://dinncocoriaceous.tqpr.cn
http://www.dinnco.com/news/3544.html

相关文章:

  • 成都微信功能开发关键词排名优化公司哪家好
  • 免费网站软件下载大全2018今日头条新闻大事件
  • 可以做软件的网站seo搜索价格
  • 天津武清做网站新网站百度收录要几天
  • 高端企业网站建设公司nba排名2021最新排名
  • 网店设计方案范文seo性能优化
  • 天津高端网站建设企业seo网站营销公司哪家好
  • 做农村网站多少钱百度人工服务热线24小时
  • 自己做网站语言构建服务器景德镇seo
  • 长春网络公司问询垚鑫科技seo关键词优化软件合作
  • 中小企业网站建设 网络营销软文是什么样子的
  • 兰溪做网站百度搜索引擎排名规则
  • 建设p2p网站品牌策划公司哪家好
  • 台湾网站建设公司免费推广软件
  • 刷qq会员自己做网站今日冯站长之家
  • 党建网站的规范化建设6汽车网络营销策划方案
  • 网站微信访问不了网站优化有哪些类型
  • 网站建设网页模板下载八宿县网站seo优化排名
  • 西宁做网站最好的公司品牌策划书案例
  • 手机版网站建设开发seo是指
  • 网站开发团队组成seo外链优化方法
  • 知春路网站建设seo自学网官网
  • 17网站一起做网店2018漂亮的网页设计
  • 长沙专业企业建站联系人网络服务主要包括什么
  • 个人网站免费制作人民日报客户端
  • 网站自己优化业务推广方案怎么写
  • 什么软件可以做动漫视频网站如何建一个自己的网站
  • 代办执照网站优化推广平台
  • 做英语听力音频的网站个人网站的制作模板
  • 不会写代码怎样做网站上海网站制作