C中的字符,行和字计数器

时间:2014-11-26 03:09:50

标签: c

我很抱歉提出这么多问题,但是当我的程序给我错误而我无法理解如何修复时,我无能为力。我使用代码块。书名是The C Programming Language 2nd Edition。

守则:

#include <stdio.h>
#include <stdlib.h>
#define IN 1
#define OUT 0

int main()
{
    int c, nl, nw, nc, state;
    state = OUT;
    while((c = getchar()) != EOF)
    {
        ++nc;
        if (c == '\n')
            if (c == '' || c == '\n' || c = '\t')
            state = OUT;
        else if (state == OUT) {
            state = IN;
            ++nw;
        }
    }
    printf("%d %d %d \n", nl, nw, nc);
}

因此,当我构建它时,它在第14行上给出了错误说:

||=== Build: Debug in Line Counter v2 (compiler: GNU GCC Compiler) ===|
C:\Users\Uddhava\Desktop\Uddhava\Learning\Programming\C C+\Line Counter v2\
main.c|14|error: empty character constant|

C:\Users\Uddhava\Desktop\Uddhava\Learning\Programming\C C+\Line Counter v2\
main.c|14|error: lvalue required as left operand of assignment|

||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

6 个答案:

答案 0 :(得分:4)

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

应该是

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


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

在你的代码中,第一个if语句使第二个无意义。

答案 1 :(得分:4)

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

这一行的第一件事是它应该是:

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

答案 2 :(得分:1)

您正在针对空字符常量c测试''的值,这是不允许的。我想你想用空格' '来测试它。

答案 3 :(得分:1)

如果您想使用空格测试变量c的值,则应将其与' '而不是''进行比较。此外,比较运算符是==,而不是=(赋值),您用它来比较if语句的最后一个条件中的制表符'\t',因此您的if语句应该是:

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

答案 4 :(得分:1)

此代码块:

while((c = getchar()) != EOF)
{
    ++nc;
    if (c == '\n')
        if (c == '' || c == '\n' || c = '\t')
        state = OUT;
    else if (state == OUT) {
        state = IN;
        ++nw;
    }
}

does not indicate (via the indentation) exactly which 'if' clause 
is related to the 'else clause.  
this is a VERY good reason to include ALL the '{' and '}' braces

this is what is actually written in the code:

while((c = getchar()) != EOF)
{
    ++nc;
    if (c == '\n')
    {
        if (c == '' || c == '\n' || c = '\t') // two errors on this line
        {
            state = OUT;
        }
        else if (state == OUT) 
        {
            state = IN;
            ++nw;
        }
    }
}

the question becomes:  is this what you really wanted?
we already know that the code is not correct
because the second 'if' will ALWAYS fail due to 'c' being
already confirmed as containing a '\n'
and the comparison of 'c' to ''
and the assignment of '\t' to 'c'

therefore, I suspect it should be:

while((c = getchar()) != EOF)
{
    ++nc;
    if (('\n' == c) || (' ' == c) || ('\n' == c) || ('\t' == c) ) 
    {
        state = OUT;
    }
    else if (state == OUT) 
    {
        state = IN;
        ++nw;
    }
}

which would have also caught the assignment of '\t' to c at compile time
rather than you having to spend many an hour debugging the code.
I.E. put the literal on the left side of the comparison operator '=='

答案 5 :(得分:0)

这很有效,我使用了Amadeus,user3629249和Min Fu的想法。谢谢大家的帮助。

工作代码:

#include <stdio.h>
#include <stdlib.h>
#define IN 1
#define OUT 0

int main()
{
int c, nl, nw, nc, state;
state = OUT;
nc = 0;
nl = 0;
nw = 0;
while((c = getchar()) != EOF)
{
++nc;
if (('\n' == c) || (' ' == c) || ('\n' == c) || ('\t' == c) )
{
    state = OUT;
    ++nl;
}
else if (state == OUT)
{
    state = IN;
    ++nw;
}
}
printf("%d %d %d \n", nl, nw, nc);
return 0;
}