Files
sambu 578a770737 chore(M0): 建立 CLion/CMake 工程骨架与测试框架
- CMake 多目标工程:shs_core 静态库 + smart_home 可执行 + shs_tests 测试
- CMakePresets.json:debug/release/asan/coverage 四个预设(debug 告警即错误)
- 自研零依赖测试框架 tests/framework/test_framework.h + 运行器(套件过滤、CTest 集成)
- 工具:tools/header_check.sh 头文件独立编译检查(接口冻结门)
- 已实现公共层:errors / string_utils / num_utils,main 启动自检
- 实测:debug 预设 0 告警、ctest 3/3 通过(45 项断言)
2026-09-14 19:34:18 +08:00

51 lines
2.0 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file num_utils.h
* @brief 数值计算与进制转换工具
*
* 教材对应:项目二 —— 数据类型、运算符与表达式、类型转换。
* - 整型/浮点型取值范围与截断行为
* - 隐式与显式类型转换(整型与浮点互转)
* - 位运算与进制转换(十六进制字符串 <-> 字节数组)
*/
#ifndef SHS_NUM_UTILS_H
#define SHS_NUM_UTILS_H
#include "shs/common.h"
#include "shs/errors.h"
/** 取整边界(类型转换教学点:double -> int 的小数截断与四舍五入差异) */
int shs_int_clamp(int value, int lo, int hi);
double shs_double_clamp(double value, double lo, double hi);
int shs_round_to_int(double value);
int shs_floor_to_int(double value);
int shs_abs_int(int value);
/** 最大值 / 最小值 / 平均值(数组 + 指针 + 循环) */
int shs_min_of(const int *values, int count);
int shs_max_of(const int *values, int count);
double shs_average_of(const int *values, int count);
/** 幂运算:循环实现(项目四 for 循环) */
long shs_pow_int(int base, int exp);
/** 阶乘:递归实现(项目六 递归调用) */
long shs_factorial(int n);
/** 最大公约数:递归欧几里得算法(项目六 递归调用) */
int shs_gcd(int a, int b);
/** 字符串 -> 数值(带合法性校验,项目三 分支判断) */
shs_err_t shs_parse_int(const char *text, int *out);
shs_err_t shs_parse_double(const char *text, double *out);
shs_err_t shs_parse_bool(const char *text, bool *out);
/** 百分比换算:value/total*100,total<=0 时返回 0.0(除零保护) */
double shs_percent(int value, int total);
/** 字节数组 <-> 十六进制字符串(项目二 进制/ASCII;项目七 指针遍历) */
shs_err_t shs_bytes_to_hex(const uint8_t *data, size_t len, char *out, size_t out_size);
int shs_hex_to_bytes(const char *hex, uint8_t *out, size_t out_size);
/** 字符串哈希(位运算综合运用,用于日志/缓存键) */
uint32_t shs_hash_string(const char *text);
#endif /* SHS_NUM_UTILS_H */