简体   繁体   English

为什么我的C代码不打印任何输出?

[英]Why is my C code not printing any output?

I am building a version of the Game of Life in ANSI C and I have almost all of the code written for it. 我正在ANSI C中构建生命游戏的版本,并且几乎所有代码都为此编写。

My C source file: 我的C源文件:

#include <stdio.h>
#include <stdlib.h>

#define HEIGHT 32
#define WIDTH 32
#define COMPASS 8
#define SPACE '.'

unsigned long mask = 0x80000000;
unsigned long neighbours[COMPASS] = { 0 };
unsigned long row[WIDTH] = { 0 };
unsigned long copy[WIDTH] = { 0 };

int northWest(unsigned long row, int rowNum) {
    copy[rowNum - 1] = row >>= 1;
    return copy[rowNum - 1];
}

int north(unsigned long row, int rowNum) {
    copy[rowNum - 1] = row;
    return copy[rowNum - 1];
}

int northEast(unsigned long row, int rowNum) {
    copy[rowNum - 1] = row <<= 1;
    return copy[rowNum - 1];
}

int west(unsigned long row, int rowNum) {
    copy[rowNum] = row >>= 1;
    return copy[rowNum];
}

int east(unsigned long row, int rowNum) {
    copy[rowNum] = row <<= 1;
    return copy[rowNum];
}

int southWest(unsigned long row, int rowNum) {
    copy[rowNum + 1] = row >>= 1;
    return copy[rowNum + 1];
}

int south(unsigned long row, int rowNum) {
    copy[rowNum + 1] = row;
    return copy[rowNum + 1];
}

int southEast(unsigned long row, int rowNum) {
    copy[rowNum + 1] = row <<= 1;
    return copy[rowNum + 1];
}

/*void clearRows(unsigned long row[]) {
    int i;
    system("clear");
    for (i = 0; i < HEIGHT; ++i) {
        row[i] = 0;
    }
}*/

void displayBinary(unsigned long x) {
    int bit;
    int mask;
    for (bit = 0; bit < HEIGHT; ++bit) {
        mask = 1 << bit;
        printf("%c", (x & mask) ? 'X' : SPACE);
    }
    printf("\n");
}

int main(void) {
    int i, alive;
    char ch;
    unsigned long init32;
    srand(time(NULL));

    for (i = 0; i < HEIGHT; ++i) {
        init32 = ((double)rand() / RAND_MAX) * 0xFFFFFFFF;
        row[i] = init32;
        displayBinary(row[i]);
    }

    do {
        system("clear");
        for (i = 0; i < HEIGHT; ++i) {
            neighbours[0] = north(row[i], i);
            neighbours[1] = south(row[i], i);
            neighbours[2] = west(row[i], i);
            neighbours[3] = east(row[i], i);
            neighbours[4] = northWest(row[i], i);
            neighbours[5] = northEast(row[i], i);
            neighbours[6] = southEast(row[i], i);
            neighbours[7] = southWest(row[i], i);
        }
        for (i = 0; i < HEIGHT; ++i) {
            alive += ((mask & neighbours[i]) ? 1 : 0);
            displayBinary(row[i]);
        }
    } while ((ch = getchar()) != 'n');

    return EXIT_SUCCESS;
}

What I am aiming for here is to have random 'X' characters printed on a 32x32 board, and have the program loop as long as the user does not type 'n'. 我的目标是在32x32板上印刷随机的“ X”字符,并在用户不键入“ n”的情况下进行程序循环。 For each loop iteration, I would like each coordinate to check its neighbours and "die" if it has less than two neighbours or more than three neighbours. 对于每个循环迭代,我希望每个坐标检查其邻居,如果它的邻居少于两个或邻居大于三个,则“死亡”。 Otherwise, it "lives" and an 'X' is printed at that coordinate. 否则,它将“运行”并在该坐标处打印“ X”。

I understand using bitwise ANDing may not be the best way to complete this, but my professor has asked we use bitwise AND, and so therefore I cannot change that logic. 我了解使用按位与运算可能不是完成此操作的最佳方法,但是我的教授要求我们使用按位与运算,因此我无法更改该逻辑。

I am having trouble clearing the rows between loops. 我在清除循环之间的行时遇到问题。 Could somebody please help me figure out how to print the updated rows for each iteration of the do loop? 有人可以帮我弄清楚如何为do循环的每次迭代打印更新的行吗?

Any help is much appreciated. 任何帮助深表感谢。 Thank you. 谢谢。

In order to show what is going on, I added this define, 为了显示正在发生的事情,我添加了此定义,

//#define SPACE (0x20)
#define SPACE ('.')

Then I changed both instances where you printed space, 然后,我更改了您打印空间的两个实例,

printf("%c", 0x20);

to

printf("%c", SPACE);

And then ran your program and got a single line, with some "." 然后运行您的程序,并在一行中添加一些“。”。 and some "X", but the second pass resulted in all ".", 和一些“ X”,但第二遍结果全为“。”,

Looks like your calculation of adjacent nodes might be wrong. 看来您对相邻节点的计算可能是错误的。

Why does your print appear wrong? 为什么打印错误?

Because your first COMPASS loop uses displayBinary() 8 times, 由于您的第一个COMPASS循环使用displayBinary()8次,

for (i = 0; i < COMPASS; ++i) {
    init32 = ((double)rand() / RAND_MAX) * 0xFFFFFFFF;
    row[i] = init32;
    displayBinary(row[i]);
}

While your subsequent looks use printf (rather than calling displayBinary), 8 times to only print 8 characters, 虽然您的后续外观使用printf(而不是调用displayBinary),但8次只能打印8个字符,

    for (i = 0; i < COMPASS; ++i) {
        alive += ((mask & row[i]) ? 1 : 0);
        if ((alive == 2) || (alive == 3))
            printf("%c", 'X');
        else
            printf("%c", SPACE);
    }

Your HEIGHT loop does a recalculation of neighbours[], but you should be reseting row[]. 您的HEIGHT循环会重新计算neighbours [],但是您应该重设row []。

I rewrote your displayBinary function, and this displays the entire row, 我重写了displayBinary函数,它显示了整行,

void displayBinary(unsigned long x) {
    //do {
    //    printf("%c", (x & mask) ? 'X' : SPACE);
    //} while ((mask >>= 1) != 0);
    int bit;
    int mask;
    for( bit=0; bit<32; ++bit )
    {
        mask = 1<<bit;
        printf("%c", (x & mask) ? 'X' : SPACE);
    }
    printf("\n");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打印所需的输出后,我的“c”代码打印出奇怪的东西 - My 'c' code prints weird stuff after printing the required output 为什么代码不将任何内容打印为输出? - Why code is not printing anything as output? 为什么我的C程序在每次执行时都更改输出而没有任何输入或代码更改? - Why does my C program changes the output in each execution without any input or code change? Linux pthread c 中的生产者和消费者问题 - 为什么我的代码不打印任何 output? - Linux pthread Producer and consumer problem in c - why does my code not print any output? 为什么我的代码没有给出任何 output 即使代码中没有错误? - Why is my code not giving any output even though there is no error in the code? 为什么我的 C 代码打印出额外的行? - Why is my C code printing out an extra line of rows? 为什么下面的c代码没有output? continue 关键字是否有错误 - why there is no output of following c code? is there any error of continue keyword 在C中,我的程序输出的数字不在用户输入数组内或数组中任何数字之和不输出任何内容 - In C, my program to output a number not inside a user input array or not sum of any numbers in the array is not printing anything C代码-为什么输出在我的代码中返回意外值? - C code - Why the output returned unexpected value in my code? 在我的代码中,第二个printf没有打印任何值 - in my code ,second printf is not printing any value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM