进度条,随机数---demo笔记【原创】

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

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

进度条,随机数---demo笔记【原创】的相关文章

Javascript+CSS3实现进度条效果_javascript技巧

进度条在很多web中都能用的到,本文就是介绍了进度条效果,具体代码如下: 一:css2 属性clip实现网页进度条: 在实现之前,我们先来介绍一下clip属性,因为这个属性在css2.1中很少使用到,所以我们有必要来了解一下: 浏览器支持程度:所有主流浏览器都支持 clip 属性. Clip属性在w3c官网是这样描述的:通过对元素进行裁剪来控制元素的可视区域,默认情况下 元素是不进行任何裁剪的. Clip裁剪的语法如下:  .xx {clip:rect(<top>, <right>

小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(七)Progress View进度条 学习笔记

小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(七)Progress View进度条 学习笔记 1 import UIKit 2 3 class ViewController: UIViewController{ 4 5 @IBOutlet weak var progress: UIProgressView! 6 7 let operationQueue = NSOperationQueue() 8 9 var 完成进度: Int = 0{ 10 didSet(oldValu

php+ajax实现带进度条的上传图片功能【附demo源码下载】_php技巧

本文实例讲述了php+ajax实现带进度条的上传图片功能.分享给大家供大家参考,具体如下: 运行效果图如下: 代码如下: <?php if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK) { ############ Edit settings ############## $UploadDirectory = 'F:

Java上传文件进度条的实现方法(附demo源码下载)_java

本文实例讲述了Java上传文件进度条的实现方法.分享给大家供大家参考,具体如下: 东西很简单,主要用到commons-fileupload,其中有一个progressListener的接口,该接口可以实现实时更新已上传文件的大小,有了这个还说什么呢? 这里给出代码: package lc.progress; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import lc.

Core Animation一些Demo总结 (动态切换图片、大转盘、图片折叠、进度条等动画效果)_Android

前一篇总结了Core Animation的一些基础知识,这一篇主要是Core Animation 的一些应用,涉及到CAShapeLayer.CAReplicatorLayer等图层的知识. 先看效果图: 1.切换图片: 2.彩票转盘 3.图片折叠 4.进度条旋转 5.粒子效果 一.切换图片 看起来很复杂的动画,通过少量的计算和编码就可以简单的实现.要做到这一步,必须是需要研究iOS开发中的Core Animation和Core Graphics框架的.日常工作中,对于很多东西不求甚解,只是拿过

Android学习笔记(24):进度条组件ProgressBar及其子类

ProgressBar作为进度条组件使用,它还派生了SeekBar(拖动条)和RatingBar(星级评分条).   ProgressBar支持的XML属性: Attribute Name Related Method Description style   设置ProgressBar指定风格 android:indeterminate   设置为true时,进度条不显示进度 android:indeterminateBehavior   indeterminate模式下,当进度条达到最大值时的

Bootstrap基本组件学习笔记之进度条(15)_javascript技巧

Bootstrap提供了各式各样的进度条. 看下面的例子: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="

我的Android进阶之旅------&amp;gt;Android自定义View实现带数字的进度条(NumberProgressBar)

今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢daimajia的开源奉献! 第一步.效果展示 图1.蓝色的进度条 图2.红色的进度条 图3.多条颜色不同的进度条 图4.多条颜色不同的进度条 版权声明:本文为[欧阳鹏]原创文章,欢迎转载,转载请注明出处! [http://blog.csdn.net/ouyang_peng/article/deta

基于uploadify.js实现多文件上传和上传进度条的显示

uploadify是JQuery的一个插件,主要实现文件的异步上传功能,可以自定义文件大小限制.文件类型.是否自动上传等属性,可以显示上传的进度条.官网地址是http://www.uploadify.com/,进入官网首页(不截图了,其实也没啥看的),可以看到uploadify的标语"Upload files like magic",意思是说使用uploadify上传文件如魔法一般.目前一共分为两个版本,Flash版和HTML5版,不过HTML5版是收费的,如图: 这里我们下载Flas