getop()函数K& R book第78页
我正在学习K& R书.目前,我正在阅读第78页的getop()函数. 我确实了解代码,但是我需要澄清两件事.
I'm studying K&R book. Currently i'm reading function getop() at p.78. I do understand the code but i need clarifications about 2 things.
getop()的代码如下:
The code of getop() is as follows:
int getch(void);
void ungetch(int);
/* getop: get next character or numeric operand */
int getop(char s[])
{
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c; /* not a number */
i = 0;
if (isdigit(c)) /* collect integer part */
while (isdigit(s[++i] = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
我的问题是关于:s[0]
in:
My question is about: s[0]
in:
while ((s[0] = c = getch()) == ' ' || c == '\t')
while循环背后的想法是跳过空格和水平制表符,那么为什么我们将s保存在s [0]中呢?为什么作者不简单地写:
The idea behind the while loop is to skip spaces and horizontal tab, so why are we saving 'c' in s[0]? Why the authors didn't simply write:
while (c= getch() == ' ' || c == '\t')
我们以后将不再使用空格和制表符,为什么我们需要将c保存在s[0]
中?这里s[0]
有什么需要?
We are not going to use spaces and tabs later on, why do we need to save c in s[0]
for? What is the need for s[0]
here?
我的第二个问题是关于:
My second question is about:
s[1] = '\0';
为什么在这里为s[1]
分配'\ 0'(字符串的结尾)?
Why are we assigning '\0' (end of string) to s[1]
here?
我已经阅读过一些关于stackoverflow.com的先前答案,但我并不完全相信!
I have read some of the previous answers posted on stackoverflow.com about it but i'm not totally convinced!
关于上述问题的可接受答案是:因为该函数可能会在读取其余输入之前返回,然后s必须是完整(且终止)的字符串."
The accepted answer about the above question is: "Because the function might return before the remaining input is read, and then s needs to be a complete (and terminated) string."
好的.但是,如果输入的开头有一个空格,然后是一个操作数或运算符,该怎么办?在这种情况下,s[1] = '\0'
是否会过早关闭字符串?是不是
Ok. But what if input has one white space at the beginning and followed by an operand or operator? In this case, s[1] = '\0'
will close the string too early? isn't it?
在回答第一个问题时,在这种情况下,分配给s[0]
是一个方便的编码快捷方式.对于getch()
读取的每个 字符,c
的值都将复制到s[0]
,无论该字符将被使用还是丢弃.如果要丢弃,没什么大不了的.在while()
循环的下一次迭代中,它将被覆盖.如果要使用它,则它已被复制到目标数组s[]
中的必要位置.
In answer to your first question, the assignment to s[0]
in this case is a convenient coding shortcut. The value of c
is copied to s[0]
for every character read by getch()
, regardless of whether it will be used or discarded. If it is to be discarded, no big deal; it will be overwritten on the next iteration of the while()
loop. If it is to be used, then it has already been copied into its necessary location in the destination array s[]
.
回答第二个问题,
但是,如果输入的开头有一个空格,然后是空格,该怎么办? 操作数或运算符?
But what if input has one white space at the beginning and followed by an operand or operator?
请注意,上一个while()
循环可防止退出循环后,空白字符(空格和制表符)出现在s[0]
中.因此,执行
Note that the previous while()
loop prevents white space characters (spaces and tabs) from appearing in s[0]
after exit from the loop. Therefore, after execution of
s[1] = '\0';
s[]
字符串将由一个既不是空格也不是制表符的单个字符组成,后跟字符串终止符.
the s[]
string will consist of a single character that is neither a space nor a tab, followed by a string terminator.
在下一条语句中
if (!isdigit(c) && c != '.')
return c; /* not a number */
如果字符不是数字或小数点,则该函数将返回.这就是为什么必须终止字符串的原因.
the function will return if the character is anything but a digit or a decimal point. This is why it was necessary to terminate the string.