- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
<分区>
我正在创建 Game of Life 程序,我创建了它的工作副本,但我在制作它时遇到了问题,因此当我尝试更改我的代码来执行它时,用户可以输入网格的 x 和 y(行和列)它得到凌乱并导致很多错误。我还尝试使用 malloc()
和 free()
开始使用堆,但我没有运气。下面的代码只是一个有效的硬编码解决方案。 (我也注释掉了测试数据和用户输入部分)。预先感谢您提供的任何帮助。
#include <stdio.h>
#define HEIGHT 12
#define WIDTH 12
#define LIFE_YES 'X'
#define LIFE_NO 'O'
typedef int TableType[HEIGHT][WIDTH];
void printTable(TableType table) {
int height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
if (table[height][width] == LIFE_YES) {
printf("X");
}
else {
printf("-");
}
}
printf("\n");
}
printf("\n");
}
void clearTable(TableType table) {
int height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
table[height][width] = LIFE_NO;
}
}
}
void askUser(TableType tableA) {
int i;
int n;
int height, width;
printf("Enter the amount of initial organisms: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
scanf("%d %d", &height, &width);
tableA[height][width] = LIFE_YES;
}
printTable(tableA);
printf("Generation 0");
}
int getNeighborValue(TableType table, int row, int col) {
if (row < 0 || row >= HEIGHT || col < 0 || col >= WIDTH || table[row][col] != LIFE_YES ) {
return 0;
}
else {
return 1;
}
}
int getNeighborCount(TableType table, int row, int col) {
int neighbor = 0;
neighbor += getNeighborValue(table, row - 1, col - 1);
neighbor += getNeighborValue(table, row - 1, col);
neighbor += getNeighborValue(table, row - 1, col + 1);
neighbor += getNeighborValue(table, row, col - 1);
neighbor += getNeighborValue(table, row, col + 1);
neighbor += getNeighborValue(table, row + 1, col - 1);
neighbor += getNeighborValue(table, row + 1, col);
neighbor += getNeighborValue(table, row + 1, col + 1);
return neighbor;
}
void calculate(TableType tableA) {
TableType tableB;
int neighbor, height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
neighbor = getNeighborCount(tableA, height, width);
if (neighbor==3) {
tableB[height][width] = LIFE_YES;
}
else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
tableB[height][width] = LIFE_YES;
}
else {
tableB[height][width] = LIFE_NO;
}
}
}
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
tableA[height][width] = tableB[height][width];
}
}
}
/* test data
void loadTestData(TableType table) {
table[3][4] = LIFE_YES;
table[3][5] = LIFE_YES;
table[3][6] = LIFE_YES;
table[10][4] = LIFE_YES;
table[10][5] = LIFE_YES;
table[10][6] = LIFE_YES;
table[11][6] = LIFE_YES;
table[12][5] = LIFE_YES;
}
*/
int main(void) {
TableType table;
char end;
int generation = 0;
clearTable(table);
/*askUser(table);*/
/*loadTestData(table);*/
printTable(table);
while (end != 'q') {
calculate(table);
printTable(table);
printf("Generation %d\n", ++generation);
printf("Press q to quit or 1 to continue: ");
scanf(" %c", &end);
}
return 0;
}
测试数据第一个实例的输出:[3][4]、[3][5]、[3][6] 上的 X。
------------
------------
------------
----XXX-----
------------
------------
------------
------------
------------
------------
------------
------------
更新:
#include <stdio.h>
#define LIFE_YES 'X'
#define LIFE_NO 'O'
extern int HEIGHT, WIDTH;
typedef int **TableType;
void printTable(TableType table) {
int height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
if (table[height][width] == LIFE_YES) {
printf("X");
}
else {
printf("-");
}
}
printf("\n");
}
printf("\n");
}
void clearTable(TableType table) {
int height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
table[height][width] = LIFE_NO;
}
}
}
void askUser(TableType tableA) {
int i;
int n;
int height, width;
printf("Enter the amount of initial organisms: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter dimensions (x y) where organism %d will live: ", i + 1);
scanf("%d %d", &height, &width);
tableA[height][width] = LIFE_YES;
}
printTable(tableA);
printf("Generation 0");
}
int getNeighborValue(TableType table, int row, int col) {
if (row < 0 || row >= HEIGHT || col < 0 || col >= WIDTH || table[row][col] != LIFE_YES ) {
return 0;
}
else {
return 1;
}
}
int getNeighborCount(TableType table, int row, int col) {
int neighbor = 0;
neighbor += getNeighborValue(table, row - 1, col - 1);
neighbor += getNeighborValue(table, row - 1, col);
neighbor += getNeighborValue(table, row - 1, col + 1);
neighbor += getNeighborValue(table, row, col - 1);
neighbor += getNeighborValue(table, row, col + 1);
neighbor += getNeighborValue(table, row + 1, col - 1);
neighbor += getNeighborValue(table, row + 1, col);
neighbor += getNeighborValue(table, row + 1, col + 1);
return neighbor;
}
void calculate(TableType tableA) {
TableType tableB;
int neighbor, height, width;
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
neighbor = getNeighborCount(tableA, height, width);
if (neighbor==3) {
tableB[height][width] = LIFE_YES;
}
else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
tableB[height][width] = LIFE_YES;
}
else {
tableB[height][width] = LIFE_NO;
}
}
}
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
tableA[height][width] = tableB[height][width];
}
}
}
/* test data
void loadTestData(TableType table) {
table[3][4] = LIFE_YES;
table[3][5] = LIFE_YES;
table[3][6] = LIFE_YES;
table[10][4] = LIFE_YES;
table[10][5] = LIFE_YES;
table[10][6] = LIFE_YES;
table[11][6] = LIFE_YES;
table[12][5] = LIFE_YES;
}
*/
int main(void) {
char end;
int generation = 0;
printf("Enter the amount of rows and columns you want in the grid: ");
scanf("%i%i\n" &HEIGHT, &WIDTH);
TableType table = malloc(HEIGHT * sizeof(int*));
for (int i = 0; i < HEIGHT; i++) {
table[i] = malloc(WIDTH * sizeof(int));
}
clearTable(table);
/*askUser(table);*/
/*loadTestData(table);*/
printTable(table);
while (end != 'q') {
calculate(table);
printTable(table);
printf("Generation %d\n", ++generation);
printf("Press q to quit or 1 to continue: ");
scanf(" %c", &end);
}
return 0;
}
计算()
void calculate(TableType tableA) {
TableType tableB;
int neighbor, height, width, i;
**tableB= malloc(HEIGHT * sizeof(int*));
for (i = 0; i < HEIGHT; i++) {
tableB[i] = malloc(WIDTH * sizeof(int));
}
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
neighbor = getNeighborCount(tableA, height, width);
if (neighbor==3) {
tableB[height][width] = LIFE_YES;
}
else if (neighbor == 2 && tableA[height][width] == LIFE_YES) {
tableB[height][width] = LIFE_YES;
}
else {
tableB[height][width] = LIFE_NO;
}
}
}
for (height = 0; height < HEIGHT; height++) {
for (width = 0; width < WIDTH; width++) {
tableA[height][width] = tableB[height][width];
}
}
free(tableB);
}
至少在某些 ML 系列语言中,您可以定义可以执行模式匹配的记录,例如http://learnyouahaskell.com/making-our-own-types-and-typeclasses -
这可能是其他人已经看到的一个问题,但我正在尝试寻找一种专为(或支持)并发编程而设计的语言,该语言可以在 .net 平台上运行。 我一直在 erlang 中进行辅助开发,以了解该语言,并且喜欢建立一个稳
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我正在寻找一种进程间通信工具,可以在相同或不同系统上运行的语言和/或环境之间使用。例如,它应该允许在 Java、C# 和/或 C++ 组件之间发送信号,并且还应该支持某种排队机制。唯一明显与环境和语言
我有一些以不同语言返回的文本。现在,客户端返回的文本格式为(en-us,又名美国英语): Stuff here to keep. -- Delete Here -- all of this below
问题:我希望在 R 中找到类似 findInterval 的函数,它为输入提供一个标量和一个表示区间起点的向量,并返回标量落入的区间的索引。例如在 R 中: findInterval(x = 2.6,
我是安卓新手。我正在尝试进行简单的登录 Activity ,但当我单击“登录”按钮时出现运行时错误。我认为我没有正确获取数据。我已经检查过,SQLite 中有一个与该 PK 相对应的数据。 日志猫。
大家好,感谢您帮助我。 我用 C# 制作了这个计算器,但遇到了一个问题。 当我添加像 5+5+5 这样的东西时,它给了我正确的结果,但是当我想减去两个以上的数字并且还想除或乘以两个以上的数字时,我没有
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 4 年前。 Improve th
这就是我所拥有的 #include #include void print(int a[], int size); void sort (int a[], int size); v
你好,我正在寻找我哪里做错了? #include #include int main(int argc, char *argv[]) { int account_on_the_ban
嘿,当我开始向数组输入数据时,我的代码崩溃了。该程序应该将数字读入数组,然后将新数字插入数组中,最后按升序排列所有内容。我不确定它出了什么问题。有人有建议吗? 这是我的代码 #include #in
我已经盯着这个问题好几个星期了,但我一无所获!它不起作用,我知道那么多,但我不知道为什么或出了什么问题。我确实知道开发人员针对我突出显示的行吐出了“错误:预期表达式”,但这实际上只是冰山一角。如果有人
我正在编写一个点对点聊天程序。在此程序中,客户端和服务器功能写入一个唯一的文件中。首先我想问一下我程序中的机制是否正确? I fork() two processes, one for client
基本上我需要找到一种方法来发现段落是否以句点 (.) 结束。 此时我已经可以计算给定文本的段落数,但我没有想出任何东西来检查它是否在句点内结束。 任何帮助都会帮助我,谢谢 char ch; FI
我的函数 save_words 接收 Armazena 和大小。 Armazena 是一个包含段落的动态数组,size 是数组的大小。在这个函数中,我想将单词放入其他称为单词的动态数组中。当我运行它时
我有一个结构 struct Human { char *name; struct location *location; int
我正在尝试缩进以下代码的字符串输出,但由于某种原因,我的变量不断从文件中提取,并且具有不同长度的噪声或空间(我不确定)。 这是我的代码: #include #include int main (v
我想让用户选择一个选项。所以我声明了一个名为 Choice 的变量,我希望它输入一个只能是 'M' 的 char 、'C'、'O' 或 'P'。 这是我的代码: char Choice; printf
我正在寻找一种解决方案,将定义和变量的值连接到数组中。我已经尝试过像这样使用 memcpy 但它不起作用: #define ADDRESS {0x00, 0x00, 0x00, 0x00, 0x0
我是一名优秀的程序员,十分优秀!