博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
进度条,随机数---demo笔记【原创】
阅读量:6282 次
发布时间:2019-06-22

本文共 10854 字,大约阅读时间需要 36 分钟。

本人学习笔记,参考网上代码完成

makefile

TEST_OBJ := rng_appMAKEOPT := ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-ARM_LINUX_KERNEL := /home/zhangbh/5830/hjf_5830x_7_24/firmware/build_dir/linux-brcm5830_sxxx-prolin2/linux-3.6.5PWD = $(shell pwd)all:    arm-none-linux-gnueabi-gcc -o $(TEST_OBJ) $(TEST_OBJ).c.PHONY: cleanclean:    rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions Module.symvers .Makefile.swp modules.order $(TEST_OBJ)

rng_app.c

#include 
#include
#include
#include
#include
#include
#include
#include
#define RANDOM_NAME "hw_random"int random_fd;typedef struct { char chr; /*tip char*/ char *title; /*tip string*/ int style; /*progress style*/ int max; /*maximum value*/ float offset; char *pro;} progress_t; #define PROGRESS_NUM_STYLE 0#define PROGRESS_CHR_STYLE 1#define PROGRESS_BGC_STYLE 2 void progress_init(progress_t *, char *, int, int);void progress_show(progress_t *, int);void progress_destroy(progress_t *);int random_num(int min, int max);void displayProgress(int progress);FILE *fp = NULL;int main(int argc, char **argv){ long long rand_num = 0; long long max_random = 0; int i, j; long long random_size = 100; int count = 0; long long check_cnt = 0; long long a[4096]; long long check_size = 0; long long b[4096]; unsigned cnt = 0; unsigned flag_b = 0; unsigned char *file_name = "./t.txt"; progress_t bar; //progress_init(&bar, "", 50, PROGRESS_NUM_STYLE); progress_init(&bar, "", 100, PROGRESS_CHR_STYLE); //progress_init(&bar, "", 50, PROGRESS_BGC_STYLE); random_fd = open("/dev/hw_random", O_RDONLY); if (random_fd < 0) { printf("open file %s failed!\r\n", RANDOM_NAME); return -1; } printf("sizeof(long int)=%d\r\n", sizeof(long int)); printf("sizeof(long)=%d\r\n", sizeof(long)); printf("sizeof(long long)=%d\r\n", sizeof(long long)); max_random = atoi(argv[1]); check_size = atoi(argv[2]); fp = fopen(file_name, "a"); if (!fp) { perror("fopen"); progress_destroy(&bar); } //rand_num = random_num(0, 10000); check_cnt = 0; while (check_cnt < check_size) { for (j=0; j
%d%% \b\n", (check_cnt*100)/check_size); // displayProgress((check_cnt*100)/check_size); progress_show(&bar, (check_cnt*100)/check_size/1.0f); } } printf("\n+-Done\n"); printf("\n"); // printf("a[50]=%d\r\n", a[50]); printf("count = %d\r\n", count); printf("check_cnt = %d\r\n", check_cnt); printf("flag_b = %d\r\n", flag_b); for (i=0; i<10; i++) { printf("b[%d]=%ld\r\n", i, b[i]); }#if 0 int i; for ( i = 0; i <= 50; i++ ) { progress_show(&bar, i/50.0f); sleep(1); } printf("\n+-Done\n"); #endif progress_destroy(&bar); return 0;}#if 0int write_to_file(unsigned long data, int tmp){ fprintf(fp, "%ld\n%d\n", msg->user_name, msg->user_data); fflush(fp); return 0;}#endifint random_num(int min, int max){ //static int random_fd; unsigned long int random_value = 0; int bytes_to_read; char *next_random_byte; int ret; assert(max > min); next_random_byte = (char *)&random_value; bytes_to_read = sizeof(random_value); /* 因为是从/dev/random中读取,read可能会被阻塞,一次读取可能只能得到一个字节, * 循环是为了让我们读取足够的字节数来填充random_value. */ do { ret = read(random_fd, next_random_byte, bytes_to_read); bytes_to_read -= ret; next_random_byte += ret; } while (bytes_to_read > 0); return min + (random_value%(max - min + 1));}void displayProgress(int progress){ int k = 0; for (k=0; k<106; k++) putchar('\b');//将当前行全部清空,用以显示最新的进度条状态 int j = 0; for (j=0; j
chr = '#'; bar->title = title; bar->style = style; bar->max = max; bar->offset = 100 / (float)max; bar->pro = (char *) malloc(max+1); if ( style == PROGRESS_BGC_STYLE ) memset(bar->pro, 0x00, max+1); else { memset(bar->pro, 32, max); memset(bar->pro+max, 0x00, 1); }} void progress_show( progress_t *bar, int bit ){ // int val = (int)(bit * bar->max); int val = bit; switch ( bar->style ) { case PROGRESS_NUM_STYLE: printf("\033[?25l\033[31m\033[1m%s%d%%\033[?25h\033[0m\r", bar->title, (int)(bar->offset * val)); fflush(stdout); break; case PROGRESS_CHR_STYLE: memset(bar->pro, '#', val); // printf("\033[?25l\033[31m\033[1m%s[%-s] %d%%\033[?25h\033[0m\r", // bar->title, bar->pro, (int)(bar->offset * val)); printf("\033[?25l\033[31m\033[1m%s[%-s] %d%%\033[?25h\033[0m\r", bar->title, bar->pro, val); fflush(stdout); break; case PROGRESS_BGC_STYLE: memset(bar->pro, 32, val); printf("\033[?25l\033[31m\033[1m%s\033[41m %d%% %s\033[?25h\033[0m\r", bar->title, (int)(bar->offset * val), bar->pro); fflush(stdout); break; }} //destroy the the progress bar.void progress_destroy(progress_t *bar){ free(bar->pro);}

test.c

#include 
#include
#include
#include
#define RESETCOLOR "\033[0m"#define GREEN "\033[0;32m"#define RED "\033[0;31m"#define LIGHT_RED "\033[1;31m" #define YELLOW "\033[1;33m"#define BLUE "\033[0;34m"#define LIGHT_BLUE "\033[1;34m" #define CYAN "\033[0;36m" #define PURPLE "\033[0;35m"#define LIGHT_PURPLE "\033[1;35m"#define BROWN "\033[0;33m"#define WHITE "\033[1;37m"#define LIGHT_GRAY "\033[0;37m" #define DARY_GRAY "\033[1;30m"typedef struct { char chr; /*tip char*/ char *title; /*tip string*/ int style; /*progress style*/ int max; /*maximum value*/ float offset; char *pro;} progress_t; #define PROGRESS_NUM_STYLE 0#define PROGRESS_CHR_STYLE 1#define PROGRESS_BGC_STYLE 2 void progress_init(progress_t *, char *, int, int);void progress_show(progress_t *, float);void progress_destroy(progress_t *);int main(int argc, char *argv[] ){ progress_t bar; // progress_init(&bar, "", 10, PROGRESS_NUM_STYLE); progress_init(&bar, "", 100, PROGRESS_CHR_STYLE); // progress_init(&bar, "", 100, PROGRESS_BGC_STYLE); int i; for ( i = 0; i <= 100; i++ ) { progress_show(&bar, i/100.0f); usleep(1000*50); } printf("\n+-Done\n"); printf("\033c"); printf(YELLOW"D"GREEN"e"RED"n"LIGHT_RED"g"YELLOW" x"BLUE"i"LIGHT_BLUE"a"CYAN"o w"PURPLE"u h"LIGHT_PURPLE"ap"BROWN"py "LIGHT_GRAY"bir"DARY_GRAY"thday!!!"RESETCOLOR"\r\n"); printf(YELLOW"D"GREEN"e"RED"n"LIGHT_RED"g"YELLOW" x"BLUE"i"LIGHT_BLUE"a"CYAN"o w"PURPLE"u h"LIGHT_PURPLE"ap"BROWN"py "LIGHT_GRAY"bir"DARY_GRAY"thday!!!"RESETCOLOR"\r\n"); sleep(1); printf(YELLOW"D"GREEN"e"RED"n"LIGHT_RED"g"YELLOW" x"BLUE"i"LIGHT_BLUE"a"CYAN"o w"PURPLE"u h"LIGHT_PURPLE"ap"BROWN"py "LIGHT_GRAY"bir"DARY_GRAY"thday!!!"RESETCOLOR"\r\n"); printf(YELLOW"D"GREEN"e"RED"n"LIGHT_RED"g"YELLOW" x"BLUE"i"LIGHT_BLUE"a"CYAN"o w"PURPLE"u h"LIGHT_PURPLE"ap"BROWN"py "LIGHT_GRAY"bir"DARY_GRAY"thday!!!"RESETCOLOR"\r\n"); printf(YELLOW"D"GREEN"e"RED"n"LIGHT_RED"g"YELLOW" x"BLUE"i"LIGHT_BLUE"a"CYAN"o w"PURPLE"u h"LIGHT_PURPLE"ap"BROWN"py "LIGHT_GRAY"bir"DARY_GRAY"thday!!!"RESETCOLOR"\r\n"); progress_destroy(&bar); return 0;} /** * initialize the progress bar. * @max = 0 * @val = 0 * * @param style * @param tip words. */void progress_init( progress_t *bar, char *title, int max, int style){ bar->chr = '#'; bar->title = title; bar->style = style; bar->max = max; bar->offset = 100 / (float)max; bar->pro = (char *) malloc(max+1); if ( style == PROGRESS_BGC_STYLE ) memset(bar->pro, 0x00, max+1); else { memset(bar->pro, 32, max); memset(bar->pro+max, 0x00, 1); }} void progress_show( progress_t *bar, float bit ){ int val = (int)(bit * bar->max); switch ( bar->style ) { case PROGRESS_NUM_STYLE: printf("\033[?25l\033[31m\033[1m%s%d%%\033[?25h\033[0m\r", bar->title, (int)(bar->offset * val)); fflush(stdout); break; case PROGRESS_CHR_STYLE: memset(bar->pro, '#', val); printf("\033[?25l\033[31m\033[1m%s[%-s] %d%%\033[?25h\033[0m\r", bar->title, bar->pro, (int)(bar->offset * val)); fflush(stdout); break; case PROGRESS_BGC_STYLE: memset(bar->pro, 32, val); printf("\033[?25l\033[31m\033[1m%s\033[41m %d%% %s\033[?25h\033[0m\r", bar->title, (int)(bar->offset * val), bar->pro); fflush(stdout); break; }} //destroy the the progress bar.void progress_destroy(progress_t *bar){ free(bar->pro);}

xipai.c

#include 
#include
#include
#include
#define CARDSNUM 10 /* 牌数 */ #define NUMXIPAI 1 /* 洗牌次数 */ #define TONGJINUM 10000000 /* 统计次数 */ #define CARDS cards /* 扑克数组 */ #define TONGJI tongji /* 统计数组 */ #define CARDS_F int CARDS[CARDSNUM] /* 扑克数组定义及函数参数 */ #define TONGJI_F long int TONGJI[CARDSNUM][CARDSNUM] /* 统计数组定义及函数参数 */ /* 洗牌函数声明 */void InitCards(CARDS_F);void ShuffleArray_Fisher_Yates(CARDS_F);void PrintCards(CARDS_F);void Puke(CARDS_F); /* 统计函数声明 */void InitTongji(TONGJI_F);void Tongjisum(CARDS_F, TONGJI_F);void PrintTongji(TONGJI_F);void Tongji(CARDS_F, TONGJI_F); int main(void){ time_t t; CARDS_F; TONGJI_F; srand(time(&t)); // Puke(CARDS);// PrintCards(CARDS); Tongji(CARDS, TONGJI); PrintTongji(TONGJI); return 0;} void InitCards(CARDS_F) /*码牌 扑克数组初始化 */{ int i; for(i = 0; i< CARDSNUM; i++) { CARDS[i] = i+1; }} void ShuffleArray_Fisher_Yates(CARDS_F) /* Fisher_Yates算法 */{ int i, j, k; for(i = CARDSNUM; i> 0; i--) { j = (rand()%(i)); k = CARDS[i-1]; CARDS[i-1] = CARDS[j]; CARDS[j] = k; }} void PrintCards(CARDS_F) /* 输出牌 */{ int j; printf("\nYour Cards Is: "); for(j = 0; j < CARDSNUM; j++) { if(!(j%10)) { printf("\n"); } printf("%d ", CARDS[j]); }} void Puke(CARDS_F) /* 洗牌 */{ int i; InitCards(CARDS); for(i = 1; i<= NUMXIPAI; i++) { ShuffleArray_Fisher_Yates(CARDS); } } void InitTongji(TONGJI_F) /* 统计数组初始化 */{ int j, k; for(j = 0; j < CARDSNUM; j++) { for(k = 0; k < CARDSNUM; k++) { TONGJI[j][k] = 0; } }} void Tongjisum(CARDS_F, TONGJI_F) /* 统计扑克牌的出现位置 */{ int j, k; Puke(CARDS); for(j = 0; j < CARDSNUM; j++) { k = CARDS[j]; TONGJI[j][k-1] += 1; }} void PrintTongji(TONGJI_F) /* 输出统计结果 */{ int i, j; printf("\nTongji Result Is: \n"); for(i = 0; i< CARDSNUM; i++) { for(j = 0; j < CARDSNUM; j++) { printf("%ld ", TONGJI[i][j]); } printf("\n"); }} void Tongji(CARDS_F, TONGJI_F) /* 扑克牌的概率统计 */{ long int i; InitTongji(TONGJI); for(i = 0; i< TONGJINUM; i++) { Tongjisum(CARDS, TONGJI); }}

 

转载地址:http://qyiva.baihongyu.com/

你可能感兴趣的文章
再学 GDI+[43]: 文本输出 - 获取已安装的字体列表
查看>>
nginx反向代理
查看>>
操作系统真实的虚拟内存是什么样的(一)
查看>>
hadoop、hbase、zookeeper集群搭建
查看>>
python中一切皆对象------类的基础(五)
查看>>
modprobe
查看>>
android中用ExpandableListView实现三级扩展列表
查看>>
%Error opening tftp://255.255.255.255/cisconet.cfg
查看>>
java读取excel、txt 文件内容,传到、显示到另一个页面的文本框里面。
查看>>
《从零开始学Swift》学习笔记(Day 51)——扩展构造函数
查看>>
python多线程队列安全
查看>>
[汇编语言学习笔记][第四章第一个程序的编写]
查看>>
android 打开各种文件(setDataAndType)转:
查看>>
补交:最最原始的第一次作业(当时没有选上课,所以不知道)
查看>>
Vue实例初始化的选项配置对象详解
查看>>
PLM产品技术的发展趋势 来源:e-works 作者:清软英泰 党伟升 罗先海 耿坤瑛
查看>>
vue part3.3 小案例ajax (axios) 及页面异步显示
查看>>
浅谈MVC3自定义分页
查看>>
.net中ashx文件有什么用?功能有那些,一般用在什么情况下?
查看>>
select、poll、epoll之间的区别总结[整理]【转】
查看>>