练习 1-21,C 程序设计语言,最后一行解释

Exercise 1-21, C Programming Language, Final Line Explanation

练习 1-21 C 编程语言 Kernighan

请解释最后一行。

#include <stdio.h>

#define TABINC 8

/* replace strings of blanks with tabs and blanks */
int main()
{
    int c, nb, nt, pos;

    nb = 0;   /* number of blanks necessary */
    nt = 0;   /* number of tabs necessary   */
    for (pos = 1; (c = getchar()) != EOF; ++pos)
       if (c == ' ') {
           if (pos % TABINC != 0)
                 ++nb;                /* increment # of blanks */
           else {
                 nb = 0;              /* reset # of blanks */
                 ++nt;                /* one more tab      */
            }
          } else {
                 for ( ; nt > 0 ; --nt)
                        putchar('\t')        /* output tab(s)   */
                 if (c == '\t')              /* forget the blank(s) */
                       nb = 0;
                 else
                    for ( ; nb > 0; --nb)
                         putchar(' ');
                 putchar(c);
                 if (c == '\n')
                    pos = 0;
                 **else if (c == '\t')
                    pos = pos + (TABINC - (pos-1) % TABINC) - 1;
          }
}**

我无法理解最后一行。 . .你能解释一下如何破译算术吗?

这是什么意思? 否则如果 (c == '\t') pos = pos + (TABINC - (pos-1) % TABINC) - 1;

else if (c == '\t')

测试当前字符是否为 Tab 字符。

pos = pos + (TABINC - (pos-1) % TABINC) - 1;

从当前 pos 列计算下一个 TABINC 列的倍数。

(pos-1) % TABINC是最后一个制表位和当前位置之间的字符数;例如如果 pos == 10pos-19,而 9 % 81。下一个制表位是 16,即 10 + (8 - 1) - 1.