#include
#define TABINC 8 //tab increment size
//replace strings of blanks with tabs and blanks
int main(void)
{
int c, nb, nt, pos;
nb = 0; //number of blanks
nt = 0; //number of tabs
for (pos = 1; (c = getchar()) != EOF; pos++)
{
if (c == ' ')
{
if (pos % TABINC != 0)
nb++;
else
{
nb = 0;
nt++;
}
}
else
{
for (; nt > 0; nt--)
putchar('\t');
if (c == '\t')
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;
}
}
return 0;
}
这是一道课后习题的答案代码,我想问的是最后一句
pos = pos + (TABINC - (pos - 1) % TABINC) - 1;
这个为什么要减1,我想的答案是
pos = pos + (TABINC-pos%TABINC);
我代入了几个数字答案都是相同的,只是减1有什么作用我不懂。
麻烦各位大神提点一下,谢谢。