#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include “include/aip_common.h”
#include “include/dynamic_array.h”
#include “include/search_functions.h”
#define BUFFER_SIZE 512
/===================================================================================================================================/
/* Function Name: v_s_main_print_int /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: Prints an integer element from void pointer /
/ Arguments: VDP vdp_a_elem : Void pointer to integer element /
/ Return: void /
/===================================================================================================================================*/
static void v_s_main_print_int(VDP vdp_a_elem)
{
printf(“%d”, *(S4 *)vdp_a_elem);
}
/===================================================================================================================================/
/* Function Name: v_s_main_print_float /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: Prints a float element from void pointer /
/ Arguments: VDP vdp_a_elem : Void pointer to float element /
/ Return: void /
/===================================================================================================================================*/
static void v_s_main_print_float(VDP vdp_a_elem)
{
printf(“%.2f”, *(float *)vdp_a_elem);
}
/===================================================================================================================================/
/* Function Name: v_s_main_print_char /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: Prints a character element from void pointer /
/ Arguments: VDP vdp_a_elem : Void pointer to character element /
/ Return: void /
/===================================================================================================================================*/
static void v_s_main_print_char(VDP vdp_a_elem)
{
printf(“‘%c’”, *(S1 *)vdp_a_elem);
}
/===================================================================================================================================/
/* Function Name: v_s_main_print_array /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: Prints contents of dynamic array /
/ Arguments: const ST_DA_ARRAY *stp_a_arr : Pointer to dynamic array structure /
/ void (vfp_a_print_element)(VDP) : Function pointer for element printing /
/ Return: void /
/===================================================================================================================================/
static void v_s_main_print_array(const ST_DA_ARRAY *stp_a_arr, void (*vfp_a_print_element)(VDP))
{
U4 u4_t_main_index;
VDP vdp_t_main_elem;
vdp_t_main_elem = NULL;
printf("["); for (u4_t_main_index = U4_MIN; u4_t_main_index < stp_a_arr->u4_s_size; u4_t_main_index++) { vdp_t_main_elem = stp_a_arr->vdp_a_data + u4_t_main_index * stp_a_arr->u4_s_elem_size; vfp_a_print_element(vdp_t_main_elem); if (u4_t_main_index < stp_a_arr->u4_s_size - 1) { printf(", "); } } printf("]\n");
}
/===================================================================================================================================/
/* Function Name: v_s_main_parse_int_array /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: Parses integer input into dynamic array with strict validation /
/ Arguments: ST_DA_ARRAY stp_a_arr : Pointer to dynamic array structure /
/ Return: void /
/===================================================================================================================================/
static void v_s_main_parse_int_array(ST_DA_ARRAY *stp_a_arr)
{
S1 s1p_t_main_buffer[BUFFER_SIZE];
S1P s1p_t_main_token;
U1 u1_t_main_valid_line = FALSE;
S1P s1p_t_main_end;
S4 s4_t_main_value;
U4 u4_t_main_values[BUFFER_SIZE/2]; // 临时存储有效整数
U4 u4_t_main_count = 0;
do { printf("Enter integers separated by spaces: "); if (fgets(s1p_t_main_buffer, BUFFER_SIZE, stdin) == NULL) { printf(" [!] Input error. Please try again.\n"); continue; } // 移除换行符 S1P s1p_t_main_newline = strchr(s1p_t_main_buffer, '\n'); if (s1p_t_main_newline != NULL) *s1p_t_main_newline = '\0'; u1_t_main_valid_line = TRUE; u4_t_main_count = 0; s1p_t_main_token = strtok(s1p_t_main_buffer, " "); while (s1p_t_main_token != NULL) { s4_t_main_value = strtol(s1p_t_main_token, (char **)&s1p_t_main_end, 10); // 严格验证:必须是纯整数 if (*s1p_t_main_end != '\0' || s1p_t_main_end == s1p_t_main_token) { printf(" [!] Invalid input: '%s'. Only integers allowed.\n", s1p_t_main_token); u1_t_main_valid_line = FALSE; break; } // 范围验证 if (s4_t_main_value > INT_MAX || s4_t_main_value < INT_MIN) { printf(" [!] Integer out of range: '%s'\n", s1p_t_main_token); u1_t_main_valid_line = FALSE; break; } // 暂存有效值 u4_t_main_values[u4_t_main_count++] = (U4)s4_t_main_value; s1p_t_main_token = strtok(NULL, " "); } // 整行有效时添加到数组 if (u1_t_main_valid_line && u4_t_main_count > 0) { for (U4 i = 0; i < u4_t_main_count; i++) { v_g_da_push_back(stp_a_arr, &u4_t_main_values[i]); } } else if (u1_t_main_valid_line) { printf(" [!] No valid integers entered. Please try again.\n"); } else { printf(" [!] Invalid input detected. Please enter integers only.\n"); } } while (stp_a_arr->u4_s_size == 0); // 直到有有效输入
}
/===================================================================================================================================/
/* Function Name: v_s_main_parse_float_array /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: Parses float input into dynamic array with strict validation /
/ Arguments: ST_DA_ARRAY stp_a_arr : Pointer to dynamic array structure /
/ Return: void /
/===================================================================================================================================/
static void v_s_main_parse_float_array(ST_DA_ARRAY *stp_a_arr)
{
S1 s1p_t_main_buffer[BUFFER_SIZE];
S1P s1p_t_main_token;
U1 u1_t_main_valid_line = FALSE;
S1P s1p_t_main_end;
float f_t_main_value;
float f_t_main_values[BUFFER_SIZE/2]; // 临时存储有效浮点数
U4 u4_t_main_count = 0;
do { printf("Enter floating-point numbers separated by spaces: "); if (fgets(s1p_t_main_buffer, BUFFER_SIZE, stdin) == NULL) { printf(" [!] Input error. Please try again.\n"); continue; } // 移除换行符 S1P s1p_t_main_newline = strchr(s1p_t_main_buffer, '\n'); if (s1p_t_main_newline != NULL) *s1p_t_main_newline = '\0'; u1_t_main_valid_line = TRUE; u4_t_main_count = 0; s1p_t_main_token = strtok(s1p_t_main_buffer, " "); while (s1p_t_main_token != NULL) { f_t_main_value = strtof(s1p_t_main_token, (char **)&s1p_t_main_end); // 严格验证:必须是纯浮点数 if (*s1p_t_main_end != '\0' || s1p_t_main_end == s1p_t_main_token) { printf(" [!] Invalid input: '%s'. Only floats allowed.\n", s1p_t_main_token); u1_t_main_valid_line = FALSE; break; } // 暂存有效值 f_t_main_values[u4_t_main_count++] = f_t_main_value; s1p_t_main_token = strtok(NULL, " "); } // 整行有效时添加到数组 if (u1_t_main_valid_line && u4_t_main_count > 0) { for (U4 i = 0; i < u4_t_main_count; i++) { v_g_da_push_back(stp_a_arr, &f_t_main_values[i]); } } else if (u1_t_main_valid_line) { printf(" [!] No valid floats entered. Please try again.\n"); } else { printf(" [!] Invalid input detected. Please enter floats only.\n"); } } while (stp_a_arr->u4_s_size == 0); // 直到有有效输入
}
/===================================================================================================================================/
/* Function Name: v_s_main_parse_char_array /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: Parses character input into dynamic array with strict validation /
/ Arguments: ST_DA_ARRAY stp_a_arr : Pointer to dynamic array structure /
/ Return: void /
/===================================================================================================================================/
static void v_s_main_parse_char_array(ST_DA_ARRAY *stp_a_arr)
{
S1 s1p_t_main_buffer[BUFFER_SIZE];
U4 u4_t_main_index;
U1 u1_t_main_valid_input = FALSE;
do { printf("Enter characters without spaces: "); if (fgets(s1p_t_main_buffer, BUFFER_SIZE, stdin) == NULL) { printf(" [!] Input error. Please try again.\n"); continue; } // 移除换行符 S1P s1p_t_main_newline = strchr(s1p_t_main_buffer, '\n'); if (s1p_t_main_newline != NULL) *s1p_t_main_newline = '\0'; u1_t_main_valid_input = FALSE; for (u4_t_main_index = 0; s1p_t_main_buffer[u4_t_main_index] != '\0'; u4_t_main_index++) { S1 s1_t_main_value = s1p_t_main_buffer[u4_t_main_index]; // 手动验证字符:允许可打印字符(ASCII 33-126),排除空格(32)和控制字符 if (s1_t_main_value >= 33 && s1_t_main_value <= 126) { v_g_da_push_back(stp_a_arr, &s1_t_main_value); u1_t_main_valid_input = TRUE; } // 空格处理:跳过不报错 else if (s1_t_main_value == 32) { // 静默跳过空格 } // 其他无效字符 else { printf(" [!] Invalid character: ASCII code %d (0x%02X)\n", s1_t_main_value, s1_t_main_value); v_g_da_free(stp_a_arr); // 清除部分输入 u1_t_main_valid_input = FALSE; break; } } if (!u1_t_main_valid_input) { printf(" [!] No valid characters entered. Please enter printable non-space characters.\n"); } } while (!u1_t_main_valid_input);
}
/===================================================================================================================================/
/* Function Name: u1_s_main_get_menu_choice /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: 获取用户菜单选择并验证 /
/ Arguments: void /
/ Return: U1 : 用户选择(1-4) /
/===================================================================================================================================*/
static U1 u1_s_main_get_menu_choice(void)
{
S1 s1p_t_buffer[BUFFER_SIZE];
S4 s4_t_choice = 0;
while (1) { printf("\n===== Dynamic Array Program =====\n"); printf("1. Integer Sorting and Binary Search\n"); printf("2. Float Sorting and Binary Search\n"); printf("3. Character Sorting and Binary Search\n"); printf("4. Exit Program\n"); printf("Please select an option (1-4): "); if (fgets(s1p_t_buffer, BUFFER_SIZE, stdin) == NULL) { printf(" [!] Input error. Please try again.\n"); continue; } char *newline = strchr(s1p_t_buffer, '\n'); if (newline) *newline = '\0'; char *end_ptr; errno = 0; s4_t_choice = strtol(s1p_t_buffer, &end_ptr, 10); if (end_ptr == s1p_t_buffer || *end_ptr != '\0' || errno == ERANGE) { printf(" [!] Invalid menu choice. Please enter a number 1-4.\n"); continue; } if (s4_t_choice >= 1 && s4_t_choice <= 4) { return (U1)s4_t_choice; } printf(" [!] Invalid choice. Please select 1-4.\n"); }
}
/===================================================================================================================================/
/* Function Name: v_s_main_init_array_context /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: 根据选择初始化数组上下文 /
/ Arguments: U1 u1_a_choice : 用户选择(1-3)
/* ST_DA_ARRAY stp_a_arr : 动态数组指针
/ void (*vfp_a_print_element)(VDP) : 元素打印函数指针
/ S4 (*s4fp_a_compare)(const VDP, const VDP) : 比较函数指针
/ void (**vfp_a_parse_array)(ST_DA_ARRAY ) : 数组解析函数指针 /
/ Return: void /
/===================================================================================================================================/
static void v_s_main_init_array_context(U1 u1_a_choice, ST_DA_ARRAY *stp_a_arr,
void (**vfp_a_print_element)(VDP),
S4 (**s4fp_a_compare)(const VDP, const VDP),
void (**vfp_a_parse_array)(ST_DA_ARRAY *))
{
switch (u1_a_choice)
{
case 1:
v_g_da_init(stp_a_arr, sizeof(S4));
*vfp_a_print_element = v_s_main_print_int;
*s4fp_a_compare = s4_g_comp_int;
*vfp_a_parse_array = v_s_main_parse_int_array;
break;
case 2:
v_g_da_init(stp_a_arr, sizeof(float));
*vfp_a_print_element = v_s_main_print_float;
*s4fp_a_compare = s4_g_comp_float;
*vfp_a_parse_array = v_s_main_parse_float_array;
break;
case 3:
v_g_da_init(stp_a_arr, sizeof(S1));
*vfp_a_print_element = v_s_main_print_char;
*s4fp_a_compare = s4_g_comp_char;
*vfp_a_parse_array = v_s_main_parse_char_array;
break;
default:
// 不应执行到此
printf(" [!] Internal error: Invalid choice in context init\n");
break;
}
}
/===================================================================================================================================/
/* Function Name: v_s_main_process_array /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: 处理数组的排序、打印和搜索 /
/ Arguments: ST_DA_ARRAY stp_a_arr : 动态数组指针
/ void (vfp_a_print_element)(VDP) : 元素打印函数
/ S4 (s4fp_a_compare)(const VDP, const VDP) : 比较函数 /
/ Return: void /
/===================================================================================================================================/
static void v_s_main_process_array(ST_DA_ARRAY *stp_a_arr,
void (*vfp_a_print_element)(VDP),
S4 (s4fp_a_compare)(const VDP, const VDP))
{
/ 空数组检查 */
if (stp_a_arr->u4_s_size == 0) {
printf(" [!] Array is empty. Operation skipped.\n");
return;
}
printf("Original array: "); v_s_main_print_array(stp_a_arr, vfp_a_print_element); printf("Sorting the array...\n"); v_g_da_bubble_sort(stp_a_arr, s4fp_a_compare); printf("Sorted array: "); v_s_main_print_array(stp_a_arr, vfp_a_print_element);
}
/===================================================================================================================================/
/* Function Name: u1_s_main_get_search_value /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: 获取并验证搜索值 /
/ Arguments: U1 u1_a_type : 数据类型(1=整数, 2=浮点, 3=字符)
/* const ST_DA_ARRAY stp_a_arr : 动态数组指针(用于搜索)
/ S4 (s4fp_a_compare)(const VDP, const VDP) : 比较函数
/ VDP vdp_a_value : 存储搜索值的指针 /
/ Return: U1 : 搜索是否成功 /
/===================================================================================================================================*/
static U1 u1_s_main_get_search_value(U1 u1_a_type, const ST_DA_ARRAY *stp_a_arr,
S4 (*s4fp_a_compare)(const VDP, const VDP),
VDP vdp_a_value)
{
S1 s1p_t_buffer[BUFFER_SIZE];
while (1) { printf("Enter the value to search: "); if (fgets(s1p_t_buffer, BUFFER_SIZE, stdin) == NULL) { printf(" [!] Input error. Please try again.\n"); continue; } // 移除换行符 char *newline = strchr(s1p_t_buffer, '\n'); if (newline) *newline = '\0'; if (u1_a_type == 1) { // 整数 char *end_ptr = NULL; errno = 0; long target = strtol(s1p_t_buffer, &end_ptr, 10); if (end_ptr == s1p_t_buffer) { printf(" [!] No digits found. Please enter an integer.\n"); } else if (*end_ptr != '\0') { printf(" [!] Trailing characters: '%s'. Only integers allowed.\n", end_ptr); } else if (errno == ERANGE) { printf(" [!] Integer out of range for long type.\n"); } else if (target < INT_MIN || target > INT_MAX) { printf(" [!] Integer out of range [%d, %d].\n", INT_MIN, INT_MAX); } else { *(S4 *)vdp_a_value = (S4)target; return TRUE; } } else if (u1_a_type == 2) { // 浮点数 char *end_ptr = NULL; errno = 0; float target = strtof(s1p_t_buffer, &end_ptr); if (end_ptr == s1p_t_buffer) { printf(" [!] No digits found. Please enter a float.\n"); } else if (*end_ptr != '\0') { printf(" [!] Trailing characters: '%s'. Only floats allowed.\n", end_ptr); } else if (errno == ERANGE) { printf(" [!] Float out of representable range.\n"); } else { *(float *)vdp_a_value = target; return TRUE; } } else if (u1_a_type == 3) { // 字符 size_t len = strlen(s1p_t_buffer); if (len == 0) { printf(" [!] No character entered. Please try again.\n"); } else { if (len > 1) { printf(" [!] Multiple characters entered. Using first character: '%c'\n", s1p_t_buffer[0]); } *(S1 *)vdp_a_value = s1p_t_buffer[0]; return TRUE; } } }
}
/===================================================================================================================================/
/* Function Name: main /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: 主函数入口 /
/ Arguments: void /
/ Return: S4 : 程序退出状态 /
/===================================================================================================================================*/
S4 main(void)
{
while (1) {
U1 u1_t_choice = u1_s_main_get_menu_choice();
// 退出处理 if (u1_t_choice == 4) { printf("Exiting program.\n"); break; } // 初始化数组上下文 ST_DA_ARRAY st_t_array; void (*vfp_t_print_element)(VDP) = NULL; S4 (*s4fp_t_compare)(const VDP, const VDP) = NULL; void (*vfp_t_parse_array)(ST_DA_ARRAY *) = NULL; v_s_main_init_array_context(u1_t_choice, &st_t_array, &vfp_t_print_element, &s4fp_t_compare, &vfp_t_parse_array); // 解析用户输入数据 vfp_t_parse_array(&st_t_array); // 处理数组(排序和打印) v_s_main_process_array(&st_t_array, vfp_t_print_element, s4fp_t_compare); // 搜索处理 if (st_t_array.u4_s_size > 0) { union { S4 s4_val; float f_val; S1 s1_val; } u_t_search_value; if (u1_s_main_get_search_value(u1_t_choice, &st_t_array, s4fp_t_compare, (VDP)&u_t_search_value)) { U1 u1_t_found = u4_g_search_binary(&st_t_array, &u_t_search_value, s4fp_t_compare); printf("Value %s in the array!\n", u1_t_found ? "found" : "not found"); } } // 清理资源 v_g_da_free(&st_t_array); } return 0;
}
进行编码规范:
进行编码规范:
1、标准变量类型
禁止使用C语言的标准变量类型。
请使用共通头文件“aip_common.h”中定义了标准类型,如下:
#ifndef AIP_COMMON_H
#define AIP_COMMON_H
typedef unsigned char U1;
typedef unsigned short U2;
typedef unsigned int U4;
typedef long unsigned int U8;
typedef signed char S1;
typedef signed short S2;
typedef signed long S4;
typedef signed long long S8;
typedef U1* U1P;
typedef U2* U2P;
typedef U4* U4P;
typedef U8* U8P;
typedef S1* S1P;
typedef S2* S2P;
typedef S4* S4P;
typedef S8* S8P;
typedef void* VDP;
#define U1_MAX 0xFFU
#define U1_MIN 0x00U
#define S1_MAX 0x7F
#define S1_MIN 0x80
#define U2_MAX 0xFFFFU
#define U2_MIN 0x0000U
#define S2_MAX 0x7FFF
#define S2_MIN 0x8000
#define U4_MAX 0xFFFFFFFFUL
#define U4_MIN 0x00000000UL
#define S4_MAX 0x7FFFFFFFL
#define S4_MIN 0x80000000L
#define U8_MAX 0xFFFFFFFFFFFFFFFFULL
#define U8_MIN 0x0000000000000000ULL
#define S8_MAX 0x7FFFFFFFFFFFFFFFLL
#define S8_MIN 0x8000000000000000LL
#define TRUE 1
#define FALSE 0
#define ST struct
#define EN enum
#endif /* AIP_COMMON_H */
2、变量:
Format type [dt][s][a][module][variable];
Length of Identifier 最多32个字符
[dt]变量类型 标准类型 u1, u2, u4, u8, s1, s2, s4, s8
结构体类型 st
枚举类型 en
共用体类型 MISRA-C中禁止使用
函数指针类型 fp
变量指针类型 u1p, u2p, u4p, s1p, s2p, s4p, s8p, stp
空指针类型 vdp
[s]作用域 ※ 函数域内的自动变量 t
函数阈内的参数变量 a
文件阈内的static变量 s
文件域内外的extern变量 g
[a]数组 ※ 维数=0 无修饰字符
维数=1 p
维数=n p[n]
[module]模块名[variable]变量名
有const修饰 只能使用大写字母
无const修饰 只能使用小写字母
3、函数
Format type [dt][s]_[module]function;
Length of Identifier 最多32个字节
[dt]返回值类型 标准类型 u1, u2, u4, u8, s1, s2, s4, s8
结构体类型 st
枚举类型 en
共用体类型 MISRA-C中禁止使用
变量指针类型 u1p, u2p, u4p, s1p, s2p, s4p, s8p, stp
空指针类型 vdp
[s]作用域 static函数仅在文件内 s
extern函数在文件内外 g
[module]模块名
[function]函数名 大小写字母均可使用
[parameter]形参名 3.3. 命名规则:遵从定义变量的命名格式
※:函数原型声明时,必须定义形参。
※函数内不会修改的参数,必须加上“const”修饰符。
4、注释必须是英文,不要使用“//”注释,不要跨行使用注释。在每一行,分别使用“/”和“/”来注释“/”后面和“/”的前面,插入空格
每个函数前面都要加注释,形如:
/===================================================================================================================================/
/* Function Name: v_g_SYMBOL_saveFile /
/ --------------------------------------------------------------------------------------------------------------------------------- /
/ Description: Saves user-defined symbol pairs to Symbol.txt file after validation /
/ Arguments: const U2 *u2p_g_FILE_folder_path_p : Folder path /
/ const U2 u2p_a_SYMBOL_user_input : User input symbols /
/ Return: void /
/===================================================================================================================================/
5、{每个左大括号都要另起一行
6、所有判断语句,if while等只能是判断语句,不能嵌套函数
7、每个函数最多一个出口
8、返回值是一个变量,或者直接写一个值,不要写公式
9、结构体:
Format typedef struct{
[member]
…
}ST_[module]_[variable];
Length of Identifier 最多32个字符
[module]模块名
[variable]变量名
只能使用大写字母
[member] 成员名称需要按照3.3. 变量的规定。
禁止使用位域类型定义。
不过,Micro Controller Vendor提供的Micro-Controller的控制寄存器定义除外。
10、不要用常数,用魔法数字,如果是全是常数的公式,可以在宏里面定义
最新发布