私は次のように求められました。
文字 'x' または空白スペースの 25 x 25 マトリックスを含むテキスト ファイルを読み取る ANSI C プログラムを作成します。
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++]));
}