K&R练习1-21 - 精神上的不理解

asd*_*dfg 6 c kr-c

"不可能"的K&R练习.

"编写一个程序entab,用最小数量的制表符和空格替换空白字符串以达到相同的间距.使用相同的制表位,比如每n列.应该是变量还是符号参数?"

我遇到的问题是,我不确定如何正确地做到这一点.我知道这不是很解释,但这就是问题所在.我见过的大多数例子都计算了一些空白,用一个标签取代了那些系列,但这并不是它的要求,我认为我理解它的要求,但目前感觉无法做到这一点.

谁能帮忙:)

编辑:到目前为止我写的代码可以在这里找到.

Bil*_*ter 15

如果你的问题是"这要我做什么?" 我想我可以通过解释原始问题来帮助(以不同的方式提出同样的问题).

编写一个程序,该程序将带有空格的输入文本作为输出,并尽可能使用制表符生成视觉上等效的文本输出.

例如,每隔8个字符使用tabstops,并将空格显示为"." 和标签为' - ';

input;
".foo:...bar;......#comment"
output;
".foo:-bar;-..#comment"

input;
".......-foo:.....bar;......#comment"
output;
"-foo:-.bar;-...#comment"
Run Code Online (Sandbox Code Playgroud)

编写程序,以便tabstop参数n可以改变,即允许除8之外的n值.准备好证明你决定使na恒定,或者变量.

编辑我看了你的代码,我认为它比它需要的更复杂.我的建议是一次做一个角色.没有必要缓冲整行.在读取每个字符时保持列数('\n'将其重置为零,'\ t'将其重置为1或更多,其他字符将其增加).当你看到一个空格(或标签)时,不要立即发出任何东西,开始你的诱捕过程,发出零个或多个标签,然后再空格(在'\n'或非空格字符,以先到者为准).

最后的提示是状态机可以使这种算法更容易编写,验证,测试和读取.

编辑2在无耻的尝试让OP接受我的答案的过程中,我现在已经开始实际编写了一个解决方案,基于我上面提供的提示和我在讨论中的评论.

// K&R Exercise 1-21, entab program, for Stackoverflow.com
#include <stdio.h>
#define N 4     // Tabstop value. Todo, make this a variable, allow
                //  user to modify it using command line

int main()
{
    int col=0, base_col=0, entab=0;

    // Loop replacing spaces with tabs to the maximum extent
    int c=getchar();
    while( c != EOF )
    {

        // Normal state
        if( !entab )
        {

            // If whitespace goto entab state
            if( c==' ' || c=='\t' )
            {
                entab = 1;
                base_col = col;
            }

            // Else emit character
            else
                putchar(c);
        }

        // Entab state
        else
        {

            // Trim trailing whitespace
            if( c == '\n' )
            {
                entab = 0;
                putchar( '\n' );
            }

            // If not whitespace, exit entab state
            else if( c!=' ' && c!='\t' )
            {
                entab = 0;

                // Emit tabs to get close to current column position
                //  eg base_col=1, N=4, col=10
                //  base_col + 3 = 4 (1st time thru loop)
                //  base_col + 4 = 8 (2nd time thru loop)
                while( (base_col + (N-base_col%N)) <= col )
                {
                    base_col += (N-base_col%N);
                    putchar( '\t' );
                }

                // Emit spaces to close onto current column position
                // eg base_col=1, N=4, col=10
                //  base_col -> 8, and two tabs emitted above
                //  base_col + 1 = 9 (1st time thru this loop)
                //  base_col + 1 = 10 (2nd time thru this loop)
                while( (base_col + 1) <= col )
                {
                    base_col++;
                    putchar( ' ' );
                }

                // Emit buffered character after tabs and spaces
                putchar( c );
            }
        }

        // Update current column position for either state
        if( c == '\t' )
            col += (N - col%N); // eg col=1, N=4, col+=3
        else if( c == '\n' )
            col=0;
        else
            col++;

        // End loop
        c = getchar();
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


E.M*_*.M. 0

我的理解是,你不必真正知道问题是什么或如何解决它才能回答这个问题。这个问题似乎是在问您是否理解何时使用变量而不是“符号参数”。我实际上不确定“符号参数”是什么意思;这似乎是过时的术语。

话虽如此,解决问题的第一部分(用制表符替换空格)相当简单。思考除法和余数。