#include <stdio.h>
#define MAXCOL 10 /* maximum column of input */
#define TABINC 8 /* tab increment size */
char line[MAXCOL]; /* input line */
int exptab(int pos);
int findblnk(int pos);
int newpos(int pos);
void printl(int pos);
/* fold long input lines into two or more shorter lines */
int main()
{
int c, pos;
pos = 0; /* position in the lines */
while((c = getchar()) != EOF)
{
line[pos] = c; /* store current character */
if(c == '\t') /* expand tab character */
pos = exptab(pos);
else if(c == '\n')
{
printl(pos); /* print current input line */
pos = 0;
}
else if(++pos >= MAXCOL)
{
pos = findblnk(pos);
printl(pos);
pos = newpos(pos);
}
}
}
/* printl: print line until pos column */
void printl(int pos)
{
int i;
for(i = 0; i < pos; ++i)
putchar(line[i]);
if(pos > 0) /* any chars printed? */
putchar('\n');
}
/* exptab: expand tab into blanks */
int exptab(int pos)
{
line[pos] = ' '; /* tab is at least one blanks */
for(++pos; pos < MAXCOL && pos % TABINC != 0; ++pos)
line[pos] = ' ';
if(pos < MAXCOL) /* room left in current line */
return pos;
else /* current line is full */
{
printl(pos);
return 0; /* reset current position */
}
}
/* findblnk: find blank's position */
int findblnk(int pos)
{
while(pos > 0 && line[pos] != ' ')
--pos;
if(pos == 0) /* no blanks in the line? */
return MAXCOL;
else /* at least one blank */
return pos + 1; /* position after the blank */
}
/*newpos: rearrange line with new position */
int newpos(int pos)
{
int i, j;
if(pos <= 0 || pos >= MAXCOL)
return 0; /* nothing to rearrange */
else
{
i = 0;
for(j = pos; j < MAXCOL; ++j)
{
line[i] = line[j];
++i;
}
return i; /* new position in line */
}
}
编写一个程序,把较长的输入行“折”成短一些的两行或多行,折行的位置
在输入行的第 n 列之前的最后一个非空格之后。要保证程序能够智能地处理输入行很长以及
在指定的列前没有空格或制表符时的情况。
这个是 The C Programming Language 里的练习 1-22 直接回车就输出了? 这个换行是怎么实现的?
不用CTRL+Z? 各位大佬帮帮忙指点下
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。