【问题标题】:how do I writing function for this code我如何为这段代码编写函数
【发布时间】:2015-06-02 15:25:43
【问题描述】:

我正在尝试为代码编写函数或方法。我真的不知道如何在下面的输出中打印出出生数字和死亡数字的循环。

这是我的代码:

#include <stdio.h>

char grid[10][10];

// Moved this function here
int occ(int x, int y) {
    int i, j, count;
    char gridCopy[10][10];


// You probably are going to do this often in the future
// Make a function for it instead of having the same code often
void printGrid() {
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++)
            printf("%c", grid[i][j]);
        printf("\n");
    }
}

【问题讨论】:

  • 旁白:如果每个单元格是01,而不是'*',代码会更高效,因为在添加邻居时,您可以直接对单元格值求和。否则,您将对'*' 进行笨拙的测试,每次通过每个单元格执行 8 次,而实际上在每一代的显示中每个单元格只需要一次 '*'
  • 扩展@WeatherVane 所说的内容:在内部,使用01 跟踪单元格,但将其打印为-*
  • formfunction 分开。通过这种方式,您可以通过出色的新 GUI 界面轻松重用计算部分。
  • 嘿伙计们,你能写一个简单的函数吗,比如我想要的方法的基本结构?我真的迷路了
  • 这里有一个基本的代码结构cboard.cprogramming.com/c-programming/…,但它是否有效,是否完整,有待您探索。

标签: c loops output


【解决方案1】:

occ 函数中不需要 gridCopy。 请记住,数组是转置的。当您访问元素 (x,y) 时,您需要使用grid[y][x]

下面是函数generateNext() 的工作代码,它为整个grid 数组生成下一个状态。这是您需要girdCopy 的地方。结果与您的示例完全相同。

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

char grid[10][10];
int  born,died,generation;

// Moved this function here
int occ(int x, int y) {
    int  count;
    int  xm1,xp1,ym1,yp1;

    xm1=x-1;xp1=x+1;ym1=y-1;yp1=y+1;
    if (xm1<0) // handle boundary cases - wrap around
        xm1=9;
    if (ym1<0)
        ym1=9;
    if (xp1>9)
        xp1=0;
    if (yp1>9)
        yp1=0;

    // Checking on the value of the neighboring cells
    count = 0;
    if (grid[ym1][x] == '*')
        count++;
    if (grid[ym1][xp1] == '*')
        count++;
    if (grid[y][xp1] == '*')
        count++;
    if (grid[yp1][xp1] == '*')
        count++;
    if (grid[yp1][x] == '*')
        count++;
    if (grid[yp1][xm1] == '*')
        count++;
    if (grid[y][xm1] == '*')
        count++;
    if (grid[ym1][xm1] == '*')
        count++;

    return count;
}

void generateNext()
{
    int x,y;
    char gridCopy[10][10];

    born=0;died=0;
    generation++;

    for (y=0; y<10; y++) {
        for (x=0; x<10; x++) {
            gridCopy[y][x]=grid[y][x];
        }
    }

    for (y=0; y<10; y++) {
        for (x=0; x<10; x++) {
            if (grid[y][x]=='*' && occ(x,y)<2)
                {gridCopy[y][x]='-';died++;}
            if (grid[y][x]=='*' && occ(x,y)>3)
                {gridCopy[y][x]='-';died++;}
            if (grid[y][x]=='-' && occ(x,y)==3)
                {gridCopy[y][x]='*';born++;}
        }
    }

    for (y=0; y<10; y++) {
        for (x=0; x<10; x++) {
            grid[y][x]=gridCopy[y][x];
        }
    }
}

// You probably are going to do this often in the future
// Make a function for it instead of having the same code often
void printGrid() {
    int x, y;
    for (y = 0; y < 10; y++) {
        for (x = 0; x < 10; x++)
            printf("%c", grid[y][x]);
        printf("\n");
    }
}


/*
 * 
 */
int main(int argc, char** argv) {

    int x, y, answ;

    // Setting entire grid to '-'
    for (y = 0; y < 10; y++)
        for (x = 0; x < 10; x++)
            grid[y][x] = '-'; // No need for a variable, just use '-' directly

    // Printing out grid
    printf("This is the original array\n");
    printGrid();

    // Setting initial state
    grid[5][4] = '*'; // No need for a variable, just use '*' directly
    grid[5][5] = '*'; 
    grid[5][6] = '*'; 
    grid[4][4] = '*'; grid[3][4] = '*';
    grid[4][6] = '*'; grid[3][6] = '*';

    // Printing out grid
    printf("This is the new array\n");
    printGrid();

    //printf("The number of neighbors is: %d\n", occ(3, 3));

    generation=0;
    answ=1;

    while(answ)
    {
        generateNext();
        printf("\n\nGeneration number %d",generation);
        printf("\nBorn=%d,Died=%d\n",born,died);
        printGrid();
        printf("\nPrint next generation-1,Exit-0:");
        scanf("%d",&answ);
    }

    return (EXIT_SUCCESS);
}

【讨论】:

    【解决方案2】:

    计数类似于打印或复制整个网格所必须执行的操作:使用相同的循环,但在每个单元格中,根据其中的内容添加到适当的计数器。

    或者,如果保留死细胞和活细胞的数量,您可以在更新网格时更新它们:如果细胞发生变化,则在细胞变得活跃或死亡时适当地更新计数器。如果您需要报告每轮出生人数和死亡人数,也可以使用此选项。

    【讨论】:

    • 嗨,Scott,你想在这里帮帮我,我一直对如何编写循环感到迷茫,想要给我基本的代码结构吗?
    • 您发布的代码中已经有 3 个示例:将 grid 复制到 gridCopy in occ()printGrid();和main() 的开头。
    • 哦,谢谢,我的意思是打印出第 2 代数字出生 = 2 数字死亡 = 0 的部分代码
    • 这应该是一个循环吧,我的代码中没有这个,你是这个意思吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2013-06-19
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多