Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 请给我解释一下……我';我卡住了_C - Fatal编程技术网

C 请给我解释一下……我';我卡住了

C 请给我解释一下……我';我卡住了,c,C,我开始学习c语言,最近我开始学习丹尼斯·里奇和布赖恩·克尼汉写的c语言编程书。。。我一直在练习1.9“编写一个c程序,将其输入复制到输出,将一个或多个空格的每个字符串替换为一个空格”。。。我试过很多方法。。。在互联网上找到答案,但没有解释。。。 我见过一个代码,看起来像 #include <stdio.h> main() { int c, last; last = EOF; while ((c = getchar()) != EOF) { i

我开始学习c语言,最近我开始学习丹尼斯·里奇和布赖恩·克尼汉写的c语言编程书。。。我一直在练习1.9“编写一个c程序,将其输入复制到输出,将一个或多个空格的每个字符串替换为一个空格”。。。我试过很多方法。。。在互联网上找到答案,但没有解释。。。 我见过一个代码,看起来像

#include <stdio.h>
main()
{
    int c, last;
    last = EOF;
    while ((c = getchar()) != EOF) 
    {
    if (c != ' ')
        putchar(c);
    if (c == ' ') 
        {
        if (last != ' ')
        putchar(c);
        }
    last = c;
    }
}
#包括
main()
{
INTC,最后;
last=EOF;
而((c=getchar())!=EOF)
{
如果(c!='')
普查尔(c);
如果(c='')
{
如果(最后一个!='')
普查尔(c);
}
last=c;
}
}
所以请有人解释一下,或者给我另一个有正确解释的代码

谢谢

#包括
#include <stdio.h>
main()
{
    int c, last;
    last = EOF;

    /* while we get a char that isn't EOF */
    while ((c = getchar()) != EOF) {
        /* if it's not a space, print it */
        if (c != ' ')
            putchar(c);
        /* otherwise, if the previous char was not a space, print it */
        if (c == ' ') 
            if (last != ' ')
                putchar(c);
        last = c;
    }
}
main() { INTC,最后; last=EOF; /*而我们得到的字符不是EOF*/ 而((c=getchar())!=EOF){ /*如果不是空格,请打印它*/ 如果(c!='') 普查尔(c); /*否则,如果前一个字符不是空格,则打印它*/ 如果(c='') 如果(最后一个!='') 普查尔(c); last=c; } }
此代码可以更简单地编写为

#include <stdio.h>
main()
{
    int c, last;

    /* while we get a char that isn't EOF */
    for (last = EOF; (c = getchar()) != EOF; last = c) {
        /* if it's not a space, print it */
        if (c != ' ')
            putchar(c);
        /* otherwise, if the previous char was not a space, print it */
        else
            if (last != ' ')
                putchar(c);
    }
}
#包括
main()
{
INTC,最后;
/*而我们得到的字符不是EOF*/
for(last=EOF;(c=getchar())!=EOF;last=c){
/*如果不是空格,请打印它*/
如果(c!='')
普查尔(c);
/*否则,如果前一个字符不是空格,则打印它*/
其他的
如果(最后一个!='')
普查尔(c);
}
}

想一想。就在你面前。读一下。你知道“如果”是什么意思吗?不查一查。不知道“!=”是什么意思?查一查。用铅笔和纸在逻辑中穿行,尽可能地把它分解成最小的部分——就是这样做的。不要试图一下子消化它,把它分解。你再也找不到比这更简单的例子了。我一定会按照你说的做,先生。。。感谢您在纸上写下一个示例字符串--
例如“foo bar baz”
,然后详细了解代码对该字符串的作用。对于每个语句,对于循环的每个迭代,写出一行,其中包含该点上所有变量的值。沿页面向下移动的更改值列。30多年后,偶尔我也会这样做。