用星号填充形状

发布于 2024-11-10 18:53:05 字数 1722 浏览 4 评论 0原文

我编写了一个程序来用星号填充封闭数字。

由于某种原因,它不接受标记值 EOF Ctrl+D

这是为什么呢?

#include "usefunc.h"

#define height 100
#define width 100

void showRow(int numbers[], int size_numbers) {
    int i;
    printf("[ ");
    for (i = 0; i < size_numbers-3; i++) {
        printf("%c, ", numbers[i]);
  }
    printf("%c ]", numbers[size_numbers-3]);
    printf("\n");
}

void showshape(int shape[][width], int lines, int max_buf) {
    int i, j;
    for (i = 0; i < lines; i++) {
        for (j = 0; j < max_buf; j++) {
            printf("%c", shape[i][j]);
        }
        printf("\n");
    }
}

void fill(int row[][width], int rownum, int end) {
    int i, c = 1, inside = 0;
    for (i = 0; i < end; i++) {
        if (row[rownum][i] == '*') {
            c++;
        }
        if (!(c%2)) inside = 1;
        else inside = 0;
        if (inside) {
            row[rownum][i] = '*';
        }
    }
}

int main () {
    int shape[height][width], i = 0, j = 0, lines = 0;
    int sentinel = 0;
    int temp = 0;
    while (sentinel != EOF) {
        while ((temp = getchar()) != '\n') {
            sentinel = temp;
            shape[i][j] = temp;
            j++;
        }
        i++;
        lines++;
    }
    for (i = 0; i < lines; i++) {
        fill(shape, i, width);
    }
    fill(shape, 0, j);
    //for (i = 0; i < lines; i++)
    showshape(shape, lines, j+2);
}

编辑 1

刚刚更新了代码。它并没有完全打印出盒子。这是怎么回事?


编辑 2

另一个更新。这次我复制了 temp 的值,但是我得到了

总线错误 

我做错了什么?

I wrote a program to fill in closed figures with asterisks.

For some reason it isn't accepting the sentinel value EOF Ctrl+D.

Why is this?

#include "usefunc.h"

#define height 100
#define width 100

void showRow(int numbers[], int size_numbers) {
    int i;
    printf("[ ");
    for (i = 0; i < size_numbers-3; i++) {
        printf("%c, ", numbers[i]);
  }
    printf("%c ]", numbers[size_numbers-3]);
    printf("\n");
}

void showshape(int shape[][width], int lines, int max_buf) {
    int i, j;
    for (i = 0; i < lines; i++) {
        for (j = 0; j < max_buf; j++) {
            printf("%c", shape[i][j]);
        }
        printf("\n");
    }
}

void fill(int row[][width], int rownum, int end) {
    int i, c = 1, inside = 0;
    for (i = 0; i < end; i++) {
        if (row[rownum][i] == '*') {
            c++;
        }
        if (!(c%2)) inside = 1;
        else inside = 0;
        if (inside) {
            row[rownum][i] = '*';
        }
    }
}

int main () {
    int shape[height][width], i = 0, j = 0, lines = 0;
    int sentinel = 0;
    int temp = 0;
    while (sentinel != EOF) {
        while ((temp = getchar()) != '\n') {
            sentinel = temp;
            shape[i][j] = temp;
            j++;
        }
        i++;
        lines++;
    }
    for (i = 0; i < lines; i++) {
        fill(shape, i, width);
    }
    fill(shape, 0, j);
    //for (i = 0; i < lines; i++)
    showshape(shape, lines, j+2);
}

Edit 1

Just updated the code. It doesn't quite print the box. what's going on?


Edit 2

Another update. This time I'm copying the value of temp, however I get

Bus error 

What am i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱着落日 2024-11-17 18:53:06

您想要:

int temp;

EOF 是一个整数值,而不是一个字符。

You want:

int temp;

EOF is an integer value, not a char.

无敌元气妹 2024-11-17 18:53:06
while ((temp = getchar()) != '\n') {
    shape`[i][j]` = temp;
    j++;
}

我怀疑一旦达到 EOF 就永远不会退出。我的意思是,getchar 可能会继续向您抛出 EOF,而您会问“不是 \n?好吧,无需停止”。

另外,@Neil Butterworth 在他的回答中所说的非常明智

while ((temp = getchar()) != '\n') {
    shape`[i][j]` = temp;
    j++;
}

I suspect this this never exits once EOF is reached. I mean, getchar probably continues to throw EOF at you and you ask "Not \n ? Okay, no need to stop".

Also, what @Neil Butterworth said in his answer is really sensible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文