我被要求:
创建一个 ANSI C 程序,该程序将读取包含字符“x”或空格的 25 x 25 矩阵的文本文件。
Display the initial generation using the function printf.
Calculate the next generation using the rules mentioned above and save it in another textfile.
The filenames of the input and output textfiles should be specified as command-line parameters.
到目前为止,我所拥有的只是一个要求文本文件的代码,然后我的代码将其打印出来。我缺乏应用康威生命游戏规则的代码。我不知道该放在哪里。以及使用什么代码。:( 请帮我。
这是我现在拥有的:
#include <stdio.h>
#include <string.h>
#define MAX_LINE_LEN 80
#define N 25
void display(char [][N+1], size_t);
int main(int argc, char *argv[]) {
FILE *fp;
char in[MAX_LINE_LEN];
char grid[N][N+1] = { { '\0' } };
size_t i = 0;
printf("Enter filename: ");
fgets(in, MAX_LINE_LEN, stdin);
*(strchr(in, '\n')) = '\0';
if ((fp = fopen(in, "r")) != NULL) {
while ((i < N) && (fgets(in, MAX_LINE_LEN, fp) != NULL)) {
*(strchr(in, '\n')) = '\0';
/* should verify only 'x' and space in string before storing */
strncpy(grid[i++], in, N);
}
/* pad each row with spaces, if necessary, for NxN array */
for (i = 0; i < N; i++) {
while (strlen(grid[i]) < N) {
strcat(grid[i], " ");
}
}
/* For all generations ...
compute next generation */
display(grid, N);
/* End for all generations */
} else {
printf("%s not found.\n", in);
getch();
}
getch();
}
void display(char a[][N+1], size_t n) {
size_t i;
for (i = 0; i < n; puts(a[i++]));
}