有人可以解释一下这条线吗 [英] Can someone please explain this line

查看:33
本文介绍了有人可以解释一下这条线吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

它是这个简单代码的一部分,我无法理解它是如何精确工作的?

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 个维度表示键盘的行和列.第三维包含该键上可用的选项.因此,例如,电话上的 3 键有 4 个选项(3、d、e 和 f).出于某种原因,作者还包括选项计数作为数组中的第一项(索引 0).所以 CharacterMask[0][2] 会给你一个包含数字 4 后跟字符 '3'、'd'、'e' 和 'f' 的数组.因此,CharacterMask[0][2][0] 将返回数字 4.类似地,CharacterMask[0][2][1] 将返回字符3".由于这行代码只真正关心选项的数量,而不是它们的值,因此最终的数组索引被硬编码为 0.鉴于此,您可以像这样重写代码以澄清:

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 是一个字符而不是数字,我们可以使用-"运算符来减去 '0' 字符.这隐式地将两个值转换为一个字节并返回这些字节之间的差异.由于在大多数字符集中数字按 0-9 排序,这有效地为我们提供了存储在 customKey 变量中的字符的数值(例如,char 3 变为字节 3).所以代码可以改写如下:

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;
}

这篇关于有人可以解释一下这条线吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆