无法找出C程序中的逻辑错误 [英] Not able to figure out the logical error in C program

查看:98
本文介绍了无法找出C程序中的逻辑错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每行打印输入一个单词的程序.

A program that prints its input one word per line.

int main() {

    int c;

    while ((c=getchar()) != EOF) {

        if (c== ' ' || c== '\n' ||c == '\t')
                putchar('\n');
        else {
            putchar(c);
        }
    }
    return 0;
}

上面的程序正确打印结果,每行一个字.相应地更改条件后,我期望下面的程序每行也打印一个单词.但是我没有得到正确的结果.我是在犯一些愚蠢的错误还是出了什么问题?

The above program prints the result correctly, one word per line. After changing the condition accordingly, I was expecting the program below to also print one word per line. However I am not getting the correct result. Am I making some silly mistake or is something wrong?

int main() {

    int c;

    while ((c=getchar()) != EOF) {

        if (c != ' ' || c != '\n' || c != '\t')
            putchar(c);
        else {
            putchar('\n');
        }
    }

    return 0;

}

推荐答案

正确的条件更改是:

if (!(c == ' ' || c == '\n' || c == '\t'))

if (c != ' ' && c != '\n' && c != '\t')

请参见迪摩根定律

这篇关于无法找出C程序中的逻辑错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆