用星号填充形状
我编写了一个程序来用星号填充封闭数字。
由于某种原因,它不接受标记值 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要:
EOF 是一个整数值,而不是一个字符。
You want:
EOF is an integer value, not a char.
我怀疑一旦达到 EOF 就永远不会退出。我的意思是,
getchar
可能会继续向您抛出EOF
,而您会问“不是\n
?好吧,无需停止”。另外,@Neil Butterworth 在他的回答中所说的非常明智。
I suspect this this never exits once
EOF
is reached. I mean,getchar
probably continues to throwEOF
at you and you ask "Not\n
? Okay, no need to stop".Also, what @Neil Butterworth said in his answer is really sensible.