Think Stats--(资料、环境、调试)

本文档介绍了Think Stats资源,包括数据源、数据解读以及数据基本操作。重点解析了Female Pregnancy文件的主要字段,如caseid、prglength、outcome和birthord等,并探讨了怀孕周期和怀孕结果。

1 数据源

http://thinkstats.com/nsfg.html

1.1 数据解读

调查的在线资料地址:http://www.icpsr.umich.edu/nsfg6 。浏览左
侧导航栏中调查的各部分,大致了解一下其中的内容。还可以在
http://cdc.gov/nchs/data/nsfg/nsfg_2002_questionnaires.htm 上阅读调
查问卷的内容

1.2 数据基本操作

http://thinkstats.com/survey.py下载脚本,放到数据同目录下,将数据解压,同时修改脚本中文件名,去除.gz结尾,可以统计如下结果
Number of respondents 7643
Number of pregnancies 13593

1.3 书网址

http://greenteapress.com/thinkstats/index.html


2 数据解读

2.1 Female Pregnancy

帮我发现一些问题,打印出来总是不对:#include "common.h" /*同步到下一秒开始*/ void sync_to_next_second(){ struct timeval tv; gettimeofday(&tv,NULL); usleep(1000000-tv.tv_usec); } /*生成随机数据*/ void generate_data(char *data,int size){ for(int i=0; i<size; i++){ data[i]=rand()%256; } } /*计算CRC32位校验值*/ uLong calculate_crc(const char *data,int size){ return crc32(0L,(const Bytef*)data,size); } void add_stat_entryA(Globalstats *stats, time_t timestamp, int bytes, int is_send, int success){ if(stats->count >= stats->capacity){ stats->capacity*=2; stats->stats=realloc(stats->stats, stats->capacity*sizeof(Commstat)); if(stats->stats == NULL){ perror("统计数组扩展失败"); exit(EXIT_FAILURE); } } Commstat *entry=&stats->stats[stats->count++]; entry->timestamp=timestamp; entry->bytes=bytes; entry->is_send=is_send; entry->success=success; strcpy(entry->process_num,"进程A"); if(is_send){ stats->total_send+=bytes; stats->send_count++; }else{ stats->total_recv+=bytes; stats->recv_count++; if(success==1){ stats->success_count++; }else if(success==0){ stats->fail_count++; } } } void add_stat_entryB(Globalstats *stats, time_t timestamp, int bytes, int is_send, int success){ if(stats->count >= stats->capacity){ stats->capacity*=2; stats->stats=realloc(stats->stats, stats->capacity*sizeof(Commstat)); if(stats->stats == NULL){ perror("统计数组扩展失败"); exit(EXIT_FAILURE); } } Commstat *entry=&stats->stats[stats->count++]; entry->timestamp=timestamp; entry->bytes=bytes; entry->is_send=is_send; entry->success=success; strcpy(entry->process_num,"进程B"); if(is_send){ stats->total_send+=bytes; stats->send_count++; }else{ stats->total_recv+=bytes; stats->recv_count++; if(success==1){ stats->success_count++; }else if(success==0){ stats->fail_count++; } } } int compareByTime(const void *a, const void *b){ const Commstat *sa=(const Commstat *)a; const Commstat *sb=(const Commstat *)b; return (sa->timestamp > sb->timestamp)- (sa->timestamp < sb->timestamp); } void sort_globalstats_commstats(Globalstats *global_stats) { if (global_stats->stats == NULL || global_stats->count <= 0) { // 如果数组为空或没有元素,则无需排序 return; } qsort(global_stats->stats, global_stats->count, sizeof(Commstat), compareByTime); } void print_stats(Globalstats *stats){ printf("\n===== 通信统计 =====\n"); printf("总发送次数:%d\n",stats->send_count); printf("总接收次数:%d\n",stats->recv_count); printf("总发送字节:%d\n",stats->total_send); printf("总接收字节:%d\n",stats->total_recv); printf("校验成功次数:%d\n",stats->success_count); printf("校验失败次数:%d\n",stats->fail_count); float success_rate=0.0; if(stats->recv_count>0){ success_rate=100.0*stats->success_count/stats->recv_count; } printf("成功率:%.2f%%\n",success_rate); sort_globalstats_commstats(stats); for (int i=0; i<stats->count; i++){ Commstat *entry=&stats->stats[i]; char time_buf[100]; struct tm *tm_info=localtime(&entry->timestamp); strftime(time_buf, sizeof(time_buf), "%H:%M:%S", tm_info); if(entry->is_send){ printf("[%s] %s发送 %d 字节数据\n", time_buf, entry->process_num, entry->bytes); }else{ if(entry->bytes>0){ if(entry->success==1){ char* status="成功"; printf("[%s] %s接收 %d 字节数据 (%s)\n", time_buf, entry->process_num, entry->bytes, status); } else if(entry->success==0){ const char* status="失败"; printf("[%s] %s接收 %d 字节数据 (%s)\n", time_buf, entry->process_num, entry->bytes, status); }else{ const char* status="未知"; printf("[%s] %s接收 %d 字节数据 (%s)\n", time_buf, entry->process_num, entry->bytes, status); } }else{ const char* status=(entry->success ==1 )? "成功":"失败"; printf("[%s] %s收到校验结果 (%s)\n", time_buf, entry->process_num, status); } } } }
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值