用c++编写一个定义一个类Histogram,该类在指点为Histogram构造函数参数发某些区间内保持数字计数,提供打印直方图的功能,处理超出范围的值
时间: 2024-05-05 14:21:05 浏览: 17
以下是用C语言编写的Histogram类的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_BINS 10
typedef struct {
int min;
int max;
int count;
} Bin;
typedef struct {
int num_bins;
int bin_width;
Bin bins[MAX_BINS];
} Histogram;
Histogram* Histogram_new(int min, int max, int num_bins) {
Histogram* hist = malloc(sizeof(Histogram));
hist->num_bins = num_bins;
hist->bin_width = (max - min) / num_bins;
for (int i = 0; i < num_bins; i++) {
hist->bins[i].min = min + i * hist->bin_width;
hist->bins[i].max = hist->bins[i].min + hist->bin_width - 1;
hist->bins[i].count = 0;
}
return hist;
}
void Histogram_delete(Histogram* hist) {
free(hist);
}
void Histogram_add(Histogram* hist, int value) {
if (value < hist->bins[0].min || value > hist->bins[hist->num_bins - 1].max) {
printf("Value %d out of range\n", value);
return;
}
for (int i = 0; i < hist->num_bins; i++) {
if (value >= hist->bins[i].min && value <= hist->bins[i].max) {
hist->bins[i].count++;
break;
}
}
}
void Histogram_print(Histogram* hist) {
printf("Histogram:\n");
for (int i = 0; i < hist->num_bins; i++) {
printf("%2d-%2d: ", hist->bins[i].min, hist->bins[i].max);
for (int j = 0; j < hist->bins[i].count; j++) {
printf("*");
}
printf("\n");
}
}
```
在这个实现中,Histogram类包含一个num_bins成员变量来指定直方图中有多少个bin,一个bin_width成员变量来计算每个bin的宽度,以及一个bins数组来存储每个bin的最小值、最大值和计数。
Histogram_new函数用于创建一个新的Histogram实例,并初始化bins数组中的每个bin。如果min和max之间的距离不能被num_bins整除,则最后一个bin将比其他bin更宽。
Histogram_delete函数用于释放为Histogram实例分配的内存。
Histogram_add函数用于将一个值添加到直方图中。如果值超出了直方图范围,则函数将打印一条错误消息并返回。
Histogram_print函数用于打印直方图。对于每个bin,函数打印出最小值和最大值,然后在下一行上打印出与该bin对应的星号数。
以下是使用Histogram类的示例代码:
```c
int main() {
Histogram* hist = Histogram_new(0, 9, 5);
Histogram_add(hist, 2);
Histogram_add(hist, 5);
Histogram_add(hist, 7);
Histogram_add(hist, 10);
Histogram_print(hist);
Histogram_delete(hist);
return 0;
}
```
这个示例创建了一个直方图,表示0到9之间的数字,分为5个bin,每个bin的宽度为2。然后,它将值2、5和7添加到直方图中,并尝试将值10添加到直方图中(这将超出范围)。最后,它打印直方图并释放直方图实例的内存。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)