English 中文(简体)
K&R 1-21
原标题:K&R exercise 1-21
  • 时间:2023-08-02 10:41:38
  •  标签:
  • c

Exercise 1-21. Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?

守则一写道:

#include <stdio.h>

#define TAB 8

void entab(int space);

int main()
{
    int c, i;

    i = 0;

    while ((c = getchar()) != EOF)
    {
        if (c ==    )
            ++i;

        else if (c ==  	 )
            i = i + TAB;

        else
        {
            entab(i);
            i = 0;
            putchar(c);
        }
    }

    return 0;
}

void entab(int space)
{
    int i;

    for (i = 1; space >= TAB; ++i)
        if (i == TAB)
        {
            putchar( 	 );
            i = 0;
            space = space - TAB;
        }

    for (i = 0; i < space; ++i)
        putchar(   );
}

但是,当我提供“3个空白空间+5个空白空间+j”投入时,我获得的产出比投入更长:

”。</p

图一第一行是投入,第二行是产出

最新情况:

根据其中一项评论和一项答复重新拟订该守则:

#include <stdio.h>

#define TAB 8

void entab(int space);

int main()
{
    int c, i, l;

    i = l = 0;

    while ((c = getchar()) != EOF)
    {
        ++l;

        if (c ==    )
            ++i;

        else if (c ==  	 )
            i = i + TAB - (l%8);

        else
        {
            entab(i);
            i = 0;
            putchar(c);
        }

        if (c ==  
 )
            l = 0;
    }

    return 0;
}


void entab(int space)
{
    int i;

    for (i = 0; space >= TAB; ++i)
        if (i == TAB)
        {
            putchar( 	 );
            i = -1;
            space = space - TAB;
        }

    for (i = 0; i < space; ++i)
        putchar(   );
}

but now the problem is that it s always one characters less than the input: enter image description here

问题回答

你们看到的产出比投入更长,因为该方案正在用一个表格和一些空间取代每个空间。 您应修改<代码>entab功能,用一个表格取代沥青,然后是适当数目的空间。

    #include <stdio.h>

#define TAB 8

void entab(int space);

int main()
{
    int c, i, l;

    i = l = 0;

    while ((c = getchar()) != EOF)
    {
        ++l;

        if (c ==    )
            ++i;
        else
        {
            entab(i);
            i = 0;
            putchar(c);
        }

        if (c ==  
 )
            l = 0;
    }

    return 0;
}

void entab(int space)
{
    int i, num_tabs, num_spaces;

    if (space == 0)
        return;

    num_tabs = space / TAB;
    num_spaces = space % TAB;

    for (i = 0; i < num_tabs; ++i)
        putchar( 	 );

    for (i = 0; i < num_spaces; ++i)
        putchar(   );
}

撰写过多的代码和使用太多的变量可能会造成混乱。 通知:<代码>int l;变量不用于任何用途,因此,在遇到新线时,只与读者混淆。

<代码>i = -1;是在<代码>中实现组合计算(<>>> > 代码/代码>的样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式样式(<>>>>>>>>> 代码> > > > > > 符号< >entab(<>>>>>>>>>>>>>>>>>>>>>>>> > > >。

The following may be adapted to suit your needs. It is comprised of only a single function and uses a "compile time" string to enable several test cases to be tried over and over (without retyping.)

#include <stdio.h>

#define TAB 8 // Experiment with this to try tabstops of 2, 4 or 10

int main( void ) {
    // Some test strings (concatenated into a single string)
    char *cp =
        "    	foo
"   // 4xSP "swallowed" by TAB
        " bar		
"    // 1xSP, string, and trailing TABS
        " 	  	  	 XXX
"     // Interwoven SP and TAB
        "           	foobar            
"   // 8xSP + TAB
        ;

    for( int i, nSP = 0; *cp; cp++ )
        if( *cp ==     )
            ++nSP;
        else if( *cp ==  	  ) // the modulo magic occurs here
            nSP = ( nSP / TAB + 1 ) * TAB ;
        else {
            if( nSP ) {
                for( i = nSP / TAB; i; i-- ) // count down to zero
                    printf( "{tab}" ); // use putchar( 	 ) for actual whitespace

                for ( i = nSP % TAB; i; i-- )
                    printf( "{SP}" ); // use putchar(   ) for actual whitespace
            }
            nSP = 0;
            putchar( *cp );
        }

    return 0;
}

这一例子的输出是:

{tab}foo
{SP}bar{tab}{tab}
{tab}{tab}{tab}{SP}XXX
{tab}{tab}foobar{tab}{SP}{SP}{SP}{SP}

下面的表格都与模块化有关。 The above "converts” in come tabs to the next tabstop byiging the number of required SP natures. 在进行产出时,再次使用模块替换具有足够TAB特性的SP型号。

Notice that both SP and TAB are merely "counted/calculated" by this function until it s time to output.

When adapting to use getchar() != EOF, take care that trailing whitespace is not truncated. It may be important to the application.

PS: an alternative, possibly simpler, calculation of "advancing to the next tabstop" would be:
nSP += TAB - (nSP % TAB);





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签