我正在为学校编写一个编译器项目,我需要将预先计算的值表放入我的代码中。问题是它不可读。我该如何解决这个问题?
到目前为止,我通过创建一个
const int matrix[13][13]
并用它的含义注释每个元素来做到这一点,但它太长了,以至于如果不拆分一行就看不到。
编辑:这就是它目前的样子:
const Table_contents_T table[13][13] = {
/* + */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value, /* <,>,<=,... */ precalculated_value,
/* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value,
/* i */ precalculated_value, /* b */ precalculated_value,
/* $ */ precalculated_value},
/* - */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value, /* <,>,<=,... */ precalculated_value,
/* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value,
/* $ */ precalculated_value},
/* * */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value, /* <,>,<=,... */ precalculated_value,
/* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value,
/* $ */ precalculated_value},
/* / */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value, /* <,>,<=,... */ precalculated_value,
/* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value,
/* $ */ precalculated_value},
/* == */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value,
/* <,>,<=,... */ precalculated_value, /* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value, /* $ */ precalculated_value},
/* <,>,<=,... */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value,
/* <,>,<=,... */ precalculated_value, /* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value, /* $ */ precalculated_value},
/* <and_or> */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value,
/* <,>,<=,... */ precalculated_value, /* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value, /* $ */ precalculated_value},
/* ! */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value,
/* <,>,<=,... */ precalculated_value, /* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value, /* $ */ precalculated_value},
/* ( */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value,
/* <,>,<=,... */ precalculated_value, /* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value, /* $ */ precalculated_value},
/* ) */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value, /* <,>,<=,... */ precalculated_value,
/* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value,
/* $ */ precalculated_value},
/* i */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value, /* <,>,<=,... */ precalculated_value,
/* <and_or> */ precalculated_value, /* ! */ precalculated_value,
/* ( */ precalculated_value, /* ) */ precalculated_value,
/* i */ precalculated_value, /* b */ precalculated_value,
/* $ */ precalculated_value},
/* b */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value,
/* <,>,<=,... */ precalculated_value, /* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value, /* $ */ precalculated_value},
/* $ */
{/* + */ precalculated_value, /* - */ precalculated_value,
/* * */ precalculated_value, /* / */ precalculated_value,
/* == */ precalculated_value,
/* <,>,<=,... */ precalculated_value, /* <and_or> */ precalculated_value,
/* ! */ precalculated_value, /* ( */ precalculated_value,
/* ) */ precalculated_value, /* i */ precalculated_value,
/* b */ precalculated_value, /* $ */ precalculated_value},
};
您是否有用作表索引的数字 0..12 的枚举? (如果没有,为什么不呢?)然后您可以使用指定的初始值设定项将注释替换为“名称”:
const Table_Contents_T table[13][13] =
{
[EN_PLUS] = { [EN_PLUS] = precalculated_value_0_0,
[EN_MINUS] = precalculated_value_0_1,
…
[EN_DOLLAR] = precalculated_value_0_12,
},
[EN_MINUS] = { [EN_PLUS] = precalculated_value_1_0,
[EN_MINUS] = precalculated_value_1_1,
…
[EN_DOLLAR] = precalculated_value_1_12,
},
…
[EN_DOLLAR] = { [EN_PLUS] = precalculated_value_12_0,
[EN_MINUS] = precalculated_value_12_1,
…
[EN_DOLLAR] = precalculated_value_12_12,
},
};
布局上有多个选项,但对齐枚举值和等号将使代码具有合理的可读性。 除了它们是预先计算的值之外,还不清楚预先计算的值是什么样的——它们是数字、另一个枚举的元素、表达式还是其他东西? 主要关心的是预先计算的值是否具有几乎一致的长度,或者它们是否具有截然不同的长度。
C99 中添加了指定的初始值设定项。 四分之一个世纪后,您不应该再使用任何旧标准(即 C90)。 C23 终于作为 ISO/IEC 9899:2024 发布。