本人学习笔记,参考网上代码完成
makefile
TEST_OBJ := rng_app MAKEOPT := 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.5 PWD = $(shell pwd) all: arm-none-linux-gnueabi-gcc -o $(TEST_OBJ) $(TEST_OBJ).c .PHONY: clean clean: rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions Module.symvers .Makefile.swp modules.order $(TEST_OBJ)
rng_app.c
#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <assert.h> #include <string.h> #include <stdlib.h> #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<random_size; j++) { a[j] = random_num(0, max_random); for (i=0; i<j; i++) { if (a[i] == a[j]) { b[cnt] = a[i]; cnt++; count++; // printf("a[%d]=%d\r\n", j, a[j]); // printf("a[%d]=%d\r\n", i, a[i]); } if (b[0] == a[j]) { flag_b++; } } } check_cnt++; if ((check_cnt*100)%check_size == 0) { // printf("===============>%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 0 int write_to_file(unsigned long data, int tmp) { fprintf(fp, "%ld\n%d\n", msg->user_name, msg->user_data); fflush(fp); return 0; } #endif int 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<progress; j++) putchar('+');//打印进度条上已经完成的部分,用‘+’表示 for (j=1; j<=100-progress; j++) putchar('-');//打印进度条上还有多少没有完成的 fprintf(stdout, " %3d%%",progress); fflush(stdout); } 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, 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 <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #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 <stdio.h> #include <time.h> #include <ctype.h> #include <stdlib.h> #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); } }
时间: 2024-09-25 08:07:43