K& R回答1-12(使用函数减少代码行数)

时间:2013-10-11 16:22:08

标签: c kernighan-and-ritchie

我编写了以下程序来回答Kernighan和Ritchies ch1问题12。

问题是我从未真正理解如何正确使用函数,并且想知道为什么我写入此程序的那个 getcharc()不起作用?

什么是解释正确功能使用的好资源。哪里?怎么样?

我从Richard Heathfield的网站(使用||或者我使用的嵌套while语句)知道这个问题的最佳解决方案,但是我想知道如何使我的程序正常工作:

#include <stdio.h>
int getcharc ();
// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space

int main()
{
    int c;

    while ((c = getchar()) != EOF) {
        while ( c == '\t') {
            getcharc(c);
        }
        while ( c == '\b') {
            getcharc(c);
        }
        while ( c == '\\') {
            getcharc(c);
        }
        while ( c == ' ') {
            getcharc(c);
        }
        putchar(c);
    }
}
int getcharc ()
{
    int c;

    c = getchar();
    printf("\n");
    return 0;
}

原始程序(我知道它有bug),没有这个功能:

#include <stdio.h>

// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space

int main()
{
    int c;

    while ((c = getchar()) != EOF) {
        while ( c == '\t') {
            c = getchar();
            printf("\n");
        }
        while ( c == '\b') {
            c = getchar();
            printf("\n");
        }
        while ( c == '\\') {
            c = getchar();
            printf("\n");
        }
        while ( c == ' ') {
            c = getchar();
            printf("\n");
        }
        putchar(c);
    }
}

所以我试图用这个功能停止

c = getchar();
printf("\n");

每次重复。

3 个答案:

答案 0 :(得分:1)

这个getcharc()函数究竟应该做什么? 做什么,从输入读取一个字符,打印换行符,并返回零。刚输入的字符被丢弃,因为你没有对它做任何事情。调用它时,也会忽略返回值。在每个调用它的地方,你都是在一个无限循环中调用它,因为没有为改变循环控制变量做出规定。

也许您打算像c = getcharc()这样的东西,但这并不会真正有用,因为无论如何你都没有从函数返回c。 (好吧,无论如何,这对“无限循环”部分都有帮助。)

无论如何,这个功能有什么意义?如果您只是在其位置正确使用getchar(),看起来您已经有了解决方案,除了其他一些错误。

答案 1 :(得分:0)

可能的解决方案之一是,将您的功能原型更改为int getcharc (int c, int flag) 现在你的代码经过一些修改;

#include <stdio.h>
int getcharc (int c, int flag);
// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space

int main()
{
    int c;
    int flag = 0;  //to keep track of repeated newline chars.

    while ((c = getchar()) != '\n') {
        flag = getcharc(c, flag);   // call getcharc() for each char in the input string. Testing for newline and printing of chars be done in the getcharc() function
    }
    return 0;     
}

int getcharc (int c, int flag)
{
        if( (c == ' ' || c == '\t' || c == '\b' || c== '\\')  && flag == 0)
        {
            printf("\n");
            flag = 1;
        }
        else
        {
            if(c != ' ' && c != '\t' && c != '\b' && c!= '\\')
                {
                     putchar(c);
                     flag = 0;
                }
        }
        return flag;
}

编辑:

  

但我想保留嵌套的while语句而不是使用||或

您的嵌套while循环仅对每个字符执行一次,因为grtchar()一次读取一个字符。 此处不需要嵌套循环!您可以将while替换为if来检查它,您的代码将为给定字符串提供相同的输出。请参阅输出here

  

从Richard Heathfield的网站(使用||或者,而不是嵌套的while语句,我已经使用过)知道这个问题的最佳解决方案,但是我想知道如何使我的程序正常工作:

通过添加if条件和break语句,您可以在一定程度上(通过您的错误)使您的程序正常工作;

#include <stdio.h>
int getcharc (int c);

int main()
{
    int c;

    while ((c = getchar()) != '\n') {
        while ( c == '\t') {
            c = getcharc(c);
            if(c != '\t')
                break;
            }

        ....
        ....


        while ( c == ' ') {
            c = getcharc(c);
            if(c != ' ')
                break;
            }
        putchar(c);
    }
    return 0;
}
int getcharc (int c)
{
    c = getchar();
    printf("\n");
    return c;
}

答案 2 :(得分:0)

// compiled by my brain muhahaha

#include <stdio.h>
int getcharc(); // we prototype getcharc without an argument

int main()
{
    int c; // we declare c

    // read character from stdio, if end of file quit, store read character in c
    while ((c = getchar()) != EOF) {  
        // if c is tab \t call function getcharc() until forever since c never changes
        while ( c == '\t') { 
            getcharc(c); // we call function getcharc with an argument
            // however getcharc doesn't take an argument according to the prototype
        }
        // if c is \b call function getcharc() until forever since c never changes
        while ( c == '\b') {
            getcharc(c);
        }
        // if c is \\ call function getcharc() until forever since c never changes
        while ( c == '\\') {
            getcharc(c);
        }
        // if c is ' ' call function getcharc() until forever since c never changes
        while ( c == ' ') {
            getcharc(c);
        }
        // since we never will get here but if we happened to get here by some
        // strange influence of some rare cosmic phenomena print out c
        putchar(c);
    }
}

// getcharc doesn't take an argument
int getcharc ()
{
    int c;  // we declare another c

    c = getchar(); // we read from the keyboard a character
    printf("\n"); // we print a newline
    return 0; // we return 0 which anyway will never be read by anyone
}

也许你会对旧的K&amp; R

感到困惑

现在当你编写一个函数参数时,你将它指定为

int getcharch(int c)
{
  ...
}