要创建一个可复用的C语言计数器,我们可以采用模块化设计,将计数器的实现与使用分离,方便再多个项目中复用
#pragma once
/*
可复用的C语言计数器模块
提供计数增减,重制,显示等功能,支持边界限制
*/
#ifndef COUNTER_H
#define COUNTER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//计数器结构体(不透明类型,外部无法直接访问内部成员)
typedef struct Counter Counter;
//创建计数器
//参数:名称,初始值,最小值,最大值
//返回:计数器指针,失败返回NULL
Counter* counter_create(const char* name, int initial, int min_val, int max_val);
//增加计数
//参数:计数器指针,增加的数量(必须为正数)
void counter_increment(Counter* counter, int amount);
//减少计数
//参数:计数器指针,减少的数量(必须为正数)
void counter_decrement(Counter* counter, int value);
//重制计数器到指定值
//参数:计数器指针,目标值
void counter_reset(Counter* counter, int value);
//获取当前计数值
//参数:计数器指针
//返回:当前值
int counter_get_value(const Counter* counter);
//显示计数器状态
//参数:计数器指针,输出文件指针(如stdout)
void counter_display(const Counter* counter, FILE* output);
//销毁计数器,释放内存
//参数:计数器指针
void counter_destroy(Counter* counter);
#endif //COUNTER_H
/*
计数器模块实现
*/
#define _CRT_SECURE_NO_WARNINGS
#include "counter.h"
//计数器结构体内部定义(仅在实现文件中可见)
struct Counter
{
int current;
int min;
int max;
char* name;
};
//创建计数器
Counter* counter_create(const char* name, int initial, int min_val, int max_val)
{
//检测参数合法性
if (!name || min_val > max_val)
{
return NULL;
}
//分配结构体内存
Counter* counter = (Counter*)malloc(sizeof(Counter));
if (!counter)
{
return NULL;
}
//分配并复制名称
counter->name = (char*)malloc(strlen(name) + 1);
if (!counter->name)
{
free(counter);
return NULL;
}
strcpy(counter->name, name);
// 设置边界值
counter->min = min_val;
counter->max = max_val;
// 设置初始值,确保在合法范围内
counter->current = initial;
if (counter->current < counter->min) {
counter->current = counter->min;
}
if (counter->current > counter->max) {
counter->current = counter->max;
}
return counter;
}
// 增加计数
void counter_increment(Counter* counter, int amount) {
if (!counter || amount <= 0) {
return;
}
int new_val = counter->current + amount;
// 检查是否超过最大值
counter->current = (new_val > counter->max) ? counter->max : new_val;
}
// 减少计数
void counter_decrement(Counter* counter, int amount) {
if (!counter || amount <= 0) {
return;
}
int new_val = counter->current - amount;
// 检查是否低于最小值
counter->current = (new_val < counter->min) ? counter->min : new_val;
}
// 重置计数器
void counter_reset(Counter* counter, int value) {
if (!counter) {
return;
}
counter->current = value;
// 确保在合法范围内
if (counter->current < counter->min) {
counter->current = counter->min;
}
if (counter->current > counter->max) {
counter->current = counter->max;
}
}
// 获取当前计数值
int counter_get_value(const Counter* counter) {
if (!counter) {
return 0; // 实际应用中可根据需要返回特定错误值
}
return counter->current;
}
// 显示计数器状态
void counter_display(const Counter* counter, FILE* output) {
if (!counter || !output) {
return;
}
fprintf(output, "%s: 当前值 = %d (范围: %d - %d)\n",
counter->name,
counter->current,
counter->min,
counter->max);
}
// 销毁计数器
void counter_destroy(Counter* counter) {
if (counter) {
free(counter->name); // 先释放名称
free(counter); // 再释放结构体
}
}
通过结构体指针和使用结构体指针的方法来模拟类的实现 实现可重复用
/*
* 计数器模块使用示例
*/
#include "counter.h"
int main() {
// 创建两个不同的计数器
Counter* download_counter = counter_create("下载计数器", 0, 0, 1000);
Counter* error_counter = counter_create("错误计数器", 0, 0, 100);
if (!download_counter || !error_counter) {
printf("创建计数器失败!\n");
return 1;
}
printf("=== 计数器使用示例 ===\n\n");
// 使用下载计数器
printf("初始状态:\n");
counter_display(download_counter, stdout);
counter_display(error_counter, stdout);
printf("\n下载了5个文件:\n");
counter_increment(download_counter, 5);
counter_display(download_counter, stdout);
printf("\n又下载了3个文件:\n");
counter_increment(download_counter, 3);
counter_display(download_counter, stdout);
// 使用错误计数器
printf("\n发生了2个错误:\n");
counter_increment(error_counter, 2);
counter_display(error_counter, stdout);
printf("\n错误计数器重置:\n");
counter_reset(error_counter, 0);
counter_display(error_counter, stdout);
printf("\n当前下载计数: %d\n", counter_get_value(download_counter));
// 释放资源
counter_destroy(download_counter);
counter_destroy(error_counter);
return 0;
}