有人可以解释这一行

人气:383 发布:2022-09-15 标签: c arduino

问题描述

K_count = (K_count < (byte)(CharacterMask[0][(customKey - '0') - 1][0]) ? ++K_count : 1);

有这的简单code,我无法理解如何precisely工作的?

It is a part of this simple code, and i cant understand how precisely does it work?

推荐答案

这基本上是通过为3键一个标准的美国电话键盘上可用选项的增加,到达最后循环后回到第一个选项。它通过引用包含奠定了作为行/列/选项每个键选项的3维数组这样做。它是写在,它可以用于与小的修改的其他键可以重复使用这样的方式

this basically increments through the available options on a standard US telephone keypad for the "3" key, looping back to the first option after the last is reached. it does so by referencing a 3-dimensional array containing the options for each key layed out as row/column/options. it is written in such a way that it can be reused for other keys with minor modifications.

在......?...结构基本上是对的if / else的快捷方式。所以你可以重写的声明是这样的:

the "...?...:..." construct is basically a shortcut for if/else. so you can rewrite the statement like this:

if (K_count < (byte)(CharacterMask[0][(customKey - '0') - 1][0])) {
    K_count = ++K_count;
} else {
    K_count = 1;
}

在++​​操作员只需添加1到变量,因此您可以重写:

the "++" operator simply adds 1 to the variable, so you can rewrite as:

if (K_count < (byte)(CharacterMask[0][(customKey - '0') - 1][0])) {
    K_count = K_count + 1;
} else {
    K_count = 1;
}

CharacterMask是一个描述一个典型的电话小键盘3维数组。前2 dimensionsindicate键盘的行和列。第三个维度包含该键可用的选项。因此,例如,在电话上的键3具有4个选项(3,D,E,和F)。由于某些原因,笔者还包括选项计数作为在阵列(索引0)的第一项。所以CharacterMask [0] [2]会给你包含后跟字符'3','D','E'4号的数组,'F'。正因为如此,CharacterMask [0] [2] [0]将返回数字4同样CharacterMask [0] [2] [1]将返回焦炭'3'。因为这条线code是唯一真正关心的选项,而不是他们的价值观编号,最终数组索引是硬codeD给定为0,你可以重写code像这样澄清:

CharacterMask is a 3-dimensional array that describes a typical telephone keypad. the first 2 dimensionsindicate the row and column of the keypad. the third dimension contains the options available on that key. so, for example, the 3 key on a telephone has 4 options (3,d,e, and f). for some reason, the author also includes the option count as the first item in the array (index 0). so CharacterMask[0][2] would give you an array containing the number 4 followed by the characters '3','d','e', and 'f'. as such, CharacterMask[0][2][0] will return the number 4. similarly CharacterMask[0][2][1] would return the char '3'. since this line of code is only really concerned with the number of options, not their values, the final array index is hard-coded to 0. given that, you can rewrite the code like this to clarify:

rowIndex = 0;
columnIndex = (customKey - '0') - 1;
optionCountIndex = 0;
if (K_count < (byte)(CharacterMask[rowIndex][columnIndex][optionCountIndex])) {
    K_count = K_count + 1;
} else {
    K_count = 1;
}

由于customKey是char,而不是一个数字,我们可以用 - 操作符减去'0'字符。这隐含蒙上两个值到一个字节并返回这些字节之间的差异。因为数字在大多数字符集有序0-9,这有效地得到我们的字符存储在customKey变量的数值(例如炭3成为字节3)。所以code可以改写为如下:

since customKey is a char and not a number, we can use the "-" operator to subtract the '0' char. this implicitly casts both values to a byte and returns the difference between those bytes. since the numbers are ordered 0-9 in most character sets, this effectively gets us the numerical value of the character stored in the customKey variable (e.g. char 3 becomes byte 3). so the code can be rewritten as follows:

rowIndex = 0;
keyNumber = (customKey - '0');
columnIndex = keyNumber - 1;
optionCountIndex = 0;
if (K_count < (byte)(CharacterMask[rowIndex][columnIndex][optionCountIndex])) {
    K_count = K_count + 1;
} else {
    K_count = 1;
}

因为他们键1,2,3在0,1,2列从零开始的索引列计数发现,我们需要从keyNumber减去1得到列索引如上图

since they keys 1,2,3 are found in columns 0,1,2 in a zero-based indexed column count, we need to subtract 1 from the keyNumber to get the column index as shown above

由于CharacterMask是一个字符数组,我们需要的第一个值转换为字节来获取值最初输入回来。此重写澄清:

because CharacterMask is a char array, we need to cast the first value to a byte to get the value initially entered back. this rewrite clarifies that:

rowIndex = 0;
keyNumber = (customKey - '0');
columnIndex = keyNumber - 1;
optionCountIndex = 0;
optionCountAsCharType = (CharacterMask[rowIndex][columnIndex][optionCountIndex]);
if (K_count < (byte)optionCountAsCharType) {
    K_count = K_count + 1;
} else {
    K_count = 1;
}

与K_count比较依赖于一个事实,即选择​​阵列长度等于选项计数加1,因为它是基于零的索引,这意味着最后的索引等于选项计数。所以只要目前K_count(又名选项指数)小于期权数量,您仍然可以添加1不超过最大索引值。但如果你是在最后一个索引,则必须回滚到1(第一个选项的索引)。它可以做多一个重构更加清晰:

the comparison with K_count relies on the fact that the option array length is equal to the option count plus 1. since it is zero-based indexed, that means the last index is equal to the option count. so as long as the current K_count (aka option index) is less than the option count, you can still add 1 without exceeding the max index value. but if you are on the last index, you must roll back to 1 (the index of the first option). it could be made more clear with one more refactor:

rowIndex = 0;
keyNumber = (customKey - '0');
columnIndex = keyNumber - 1;
optionCountIndex = 0;
optionCountAsCharType = (CharacterMask[rowIndex][columnIndex][optionCountIndex]);
nextIndexIsInsideArrayBounds = K_count < (byte)optionCountAsCharType
if (nextIndexIsInsideArrayBounds) {
    K_count = K_count + 1;
} else {
    K_count = 1;
}

241