帮我发现一些问题,打印出来总是不对:#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);
}
}
}
}