Not able to figure out the logical error in C program

大兔子大兔子 提交于 2019-12-12 10:55:06

问题


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;

}

回答1:


the correct change of condition is:

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

or

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

See De Morgan's law




回答2:


You've gotten a number of answers to your original question, but I feel obliged to add one minor detail: both the original and the modified version suffer from a couple of what I'd consider problems. First, they don't really detect white-space correctly (e.g., they ignore vertical tabs, and any other white-space as defined by the locale), and they produce blank lines if words are separated by more than one white-space character.

For the first problem, I'd use isspace instead of directly comparing to the white-space characters you know about (incidentally, eliminating the source of the problem you encountered). For the second, you could add some logic to skip all consecutive white-space characters when you encounter the first, or you could add a flag to write out a new-line if and only if the current character is a space and the previous character you wrote was not a new-line.

Alternatively, you could read words using scanf with the %s conversion:

char buffer[256];

while (scanf("%255s", buffer))
    printf("%s\n", buffer);

This approach, however, imposes an upper limit on the size of a single word. Under normal circumstances, that's rarely problem, but depending on the nature of the input it could/can be.




回答3:


You need to change the ||s to &&s, i.e. change

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

to

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

i.e. "IF c not equal to space AND c not equal to return AND c not equal to tab THEN ..."




回答4:


Remember your logic programming classes:

!(A || B) == (!A && !B)
!(A && B) == (!A || !B)

In other words: your condition needs to read like this:

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



回答5:


You could do what MByD said, or you could change your or's (||) to and's (&&).




回答6:


The error is in your conditional expression. When you negate the first expression

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

you get:

c!= ' ' && c!= '\n' &&c != '\t'

and not you defined in your question.

Remember:

(A && B) is the same as (!A || !B)




回答7:


The opposite of A or B or C is not A and not B and not C, so use:

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



回答8:


You need to study a bit more on DeMorgan's Laws. It is not enough to change the combination operators from "||" to "&&", you must also negate all of the elements being combined to obtain the same solution set.



来源:https://stackoverflow.com/questions/6023292/not-able-to-figure-out-the-logical-error-in-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!