填充形状用C [英] fill shape with C

查看:216
本文介绍了填充形状用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序,以填补在用星号封闭的数字。由于某些原因,它是不接受的定点值EOF(CTRL-D)。这是为什么?

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);
}

还好,刚刚更新的code。它并不完全打印对话框。这是怎么回事?

okay, just updated the code. it doesn't quite print the box. what's going on??

另一个更新为code。这一次,我临时复制的价值。不过,我得到总线错误 - ?!什么我做错了。

another update for the code. this time i am copying the value of temp. however, i get Bus error - what am i doing wrong?!

推荐答案

您想要的:

int temp;

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

EOF is an integer value, not a char.

这篇关于填充形状用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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