diff --git a/CMakeLists.txt b/CMakeLists.txt index 838de4c..f3d3f2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.27) project(HW_Lib CXX C) set(CMAKE_C_STANDARD 23) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") #设置c++的编译选项 +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++23") #设置c++的编译选项 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99") #设置c的编译选项 include_directories( lib/inc diff --git a/lib/inc/log/log.h b/lib/inc/log.h similarity index 100% rename from lib/inc/log/log.h rename to lib/inc/log.h diff --git a/lib/inc/tool.h b/lib/inc/tool.h index e04f0e6..e177c77 100644 --- a/lib/inc/tool.h +++ b/lib/inc/tool.h @@ -47,6 +47,7 @@ typedef enum { // 定义枚举类型Type_t,包含不同数据类型 float *:6,double *:10, \ default: ((void)0)) + #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) // 计算数组的元素个数 #define in , // 定义逗号为in #define _foreach(e, a) for(size_t e = 0; e < ARRAY_SIZE(a); e++) // 实现foreach宏,遍历数组a,e为当前元素下标 @@ -82,7 +83,7 @@ typedef enum { // 定义枚举类型Type_t,包含不同数据类型 * @param I_Max: [输入] 数据最大值 * @param O_Min: [输入] 需求最小值 * @param O_Max: [输入] 需求最小值 - * @retval 需求处理值 + * @return 需求处理值 * @example Mapping(128,0,4096,0.0,3.3) //0.10 * ADC采集引脚电压 **/ @@ -111,6 +112,14 @@ float Mapping(float val, float I_Min, float I_Max, float O_Min, float O_Max); **/ void BufPrint(char *name, void *buf, Type_t type, unsigned int len, unsigned char frame); +/** + * @brief 字符串转浮点数 + * @param str: [输入] 输入的字符串 + * @return float 转换后的浮点数 + * @example Str2Float("3.14"); + **/ +float Str2Float(char *str); + /** * @brief 数组内容打印 * @param arr: [输入] 标识名数组 diff --git a/lib/src/tool.cpp b/lib/src/tool.cpp index 0b4023f..8aaca93 100644 --- a/lib/src/tool.cpp +++ b/lib/src/tool.cpp @@ -42,4 +42,32 @@ void BufPrint(char *name, void *buf, Type_t type, unsigned int len, unsigned cha } } printf("\n"); -} \ No newline at end of file +} + +float Str2Float(char *str) { + int integer = 0; + double decimal = 0; + int sign = 1; + int decimalFlag = 0; + int decimalWeight = 1; + if (*str == '-') { + sign = -1; + str++; + } + while (*str != '\0') { + if (*str == '.') { + decimalFlag = 1; + str++; + continue; + } + if (!decimalFlag) { + integer = integer * 10 + (*str - '0'); + } else { + decimal = decimal * 10 + (*str - '0'); + decimalWeight *= 10; + } + str++; + } + decimal /= decimalWeight; + return float(sign * (integer + decimal)); +} diff --git a/main.c b/main.c index 9d6bc92..308bdda 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,7 @@ #include "t_iic.h" #include "t_task.h" #include "t_arg.h" +#include "tool.h" #include void Test(char *name, void (*pFunction)()) { @@ -24,11 +25,27 @@ void Test(char *name, void (*pFunction)()) { printf("\n------< %s END >------\n", name); } +#define END "\n" +#define TYPE_F(v) _Generic((v), \ +char :"[char]-> "#v"=%c" END, \ +short :"[short]-> "#v"=%d" END, \ +int :"[int]-> "#v"=%d" END, \ +float :"[float]-> "#v"=%.2f" END , \ +double :"[double]-> "#v"=%.2f" END, \ +default: "[err]-> "#v"=%p" END) +#define POUT(s) printf(TYPE_F(s) ,s) + int main() { - srand((unsigned)time(NULL)); - Test("SPI", Test_spi); - Test("IIC", Test_iic); - Test("ArgPase", Test_argpase); - Test("Task", Test_task); + srand((unsigned) time(NULL)); + int i = 1; + POUT((++i) + (++i)); + + char str[] = "123.456"; + float result = Str2Float(str); + printf("Result: %.3f\n", result); +// Test("SPI", Test_spi); +// Test("IIC", Test_iic); +// Test("ArgPase", Test_argpase); +// Test("Task", Test_task); return 0; }