【问题标题】:Printing numbers using getchar使用 getchar 打印数字
【发布时间】:2020-03-20 19:34:23
【问题描述】:

所以我的问题是这样的,我想使用getchar 函数打印一个数字,如果我输入101 输出应该是101,但我得到11 作为输入和@987654325 @ 在数字内时被忽略。

代码:

#include <stdio.h>

#define Out  0
#define In   1

void ex04();

int main() {
    ex04();
    return 0;
}

void ex04() {
    int c;
    while ((c = getchar()) != EOF) {
        int state = Out;
        if (state == Out && c != '0' && c != '\n' && c != '\t' && c != ' ') {
            state = In;
            printf("%c", c);
        } else
        if (state == In && c == '0') {
            State = In;
            printf("%d", 0);
        } else
        if (state == Out && c == '0' || c == '\n' || c == '\t' || c == ' ') {
            state = Out;
        } else
        if (state == In && c == '\n' || c == '\t' || c == ' ') {
            state = Out;
        }
    }
}

例子:

输入:101

我的输出:11

正确输出:101

输入:1 2 0 4

我的输出:1 2 4

正确输出:1 2 0 4

输入:0

我的输出:

正确输出:0

真的很感激任何帮助,顺便说一句,我不能使用scanf

【问题讨论】:

  • state == OUT 始终为真,因为您刚刚分配了state = OUT。因此,当您分配 state = OUT 并且不打印任何内容时,您会遇到第三个条件。
  • 与您当前的问题无关,但state == In &amp;&amp; c == '\n' || c == '\t' || c == ' '(state == In &amp;&amp; c == '\n') || c == '\t' || c == ' ' 相同,可能不是您想要的。为什么不使用两个嵌套的if 子句,外部用于state,内部用于c
  • 目标到底是什么?您提供了 2 个输入和预期输出,但基于此,您可以使用 while ((c=getchar()) != '\n') if ( c &gt;= '0' &amp;&amp; c &lt;= '1') printf("%c", c);。还有其他标准吗?
  • 因为在第三个中如果我想要的是如果它在一个数字内并且字符不是 \n 或 \t 或 ''
  • 如果我输入 1 2 0 4,我忘记了一个标准,我想要 1 2 0 4 就像输出一样

标签: c printing getchar


【解决方案1】:

有一个优先级问题

if (state == Out && c == '0' || c == '\n' || c == '\t' || c == ' ')

你的意思可能是:

if (state == Out && (c == '0' || c == '\n' || c == '\t' || c == ' '))

同样的评论也适用于最后的测试。

您可能应该将int state = Out; 移出循环。

最后我不知道为什么你在测试中是特殊情况 0,但上面的这些修复确实改变了行为。

这是修改后的版本:

#include <stdio.h>

#define Out  0
#define In   1

void ex04(void) {
    int c;
    int state = Out;
    while ((c = getchar()) != EOF) {
        if (state == Out && c != '0' && c != '\n' && c != '\t' && c != ' ') {
            state = In;
            printf("%c", c);
        } else
        if (state == In && c == '0') {
            state = In;
            printf("%d", 0);
        } else
        if (state == Out && (c == '0' || c == '\n' || c == '\t' || c == ' ')) {
            state = Out;
        } else
        if (state == In && (c == '\n' || c == '\t' || c == ' ')) {
            state = Out;
        }
    }
}

int main() {
    ex04();
    return 0;
}

【讨论】:

    【解决方案2】:

    如果您想要该输出,只需将这一行 if (state == In &amp;&amp; c == '0') 更改为 if (state == Out &amp;&amp; c == '0'),因为您的 state 在每次进入第一个循环之后,当您初始化循环内的某些内容时,您的 out ,每次您遍历一个循环并转到第一个循环时,该变量的值再次成为初始化值。所以检查if(state==out) 意义不大。 (在你的情况下,不是每个地方)

    对于你的状态将出现的每个字符,只需删除它,使用它就像if(out==out) (这就是你的程序从不打印 0 的原因,因为 state==out 包含 0 在内的每个字符。)

    #include <stdio.h>
    
    
    void ex04();
    
    int main() {
        ex04();
        return 0;
    }
    
    void ex04() {
        int c;
        while ((c = getchar()) != EOF) {
            if (c != '0' && c != '\n' && c != '\t' && c != ' ') {
    
                printf("%c", c);
            }
            else
                if ( c == '0') {
    
                    printf("%d", 0);
                }
                else
                    if (c == '0' || c == '\n' || c == '\t' || c == ' ') {
    
                        //  printf("\n");
                    }
                    else
                        if ( c == '\n' || c == '\t' || c == ' ') {
    
                            //  printf("\n");
                        }
    
        }
    }
    

    那些printf(我评论过)将有助于输出可读性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2013-07-17
      • 1970-01-01
      相关资源
      最近更新 更多