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0 0 0 0 0 0 0 0 0 0
*/
#define ARRAY_SIZE 20
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
void input(int matr[ARRAY_SIZE][ARRAY_SIZE]);
/*
输入数据
pre: matr是一个ARRAY_SIZE*ARRAY_SIZE的空数组
post: matr中有输入数据
*/
bool lord(int i, int j, int matr[ARRAY_SIZE][ARRAY_SIZE]);
/*
判断元胞的生或死的算法
pre: matr中有数据
post: 返回matr[i][j]的生死状况
*/
void print_matr(int matr[ARRAY_SIZE][ARRAY_SIZE]);
/*
打印数组
pre: matr中有数据
post: 打印出matr中的数据
*/
int new_array(int matr[ARRAY_SIZE][ARRAY_SIZE], int new_arr[ARRAY_SIZE][ARRAY_SIZE]);
/*
产生新的数组
pre: matr中有数据,new_arr是一个ARRAY_SIZE*ARRAY_SIZE的空数组
post: 由matr生成的新的数组
*/
void empty_array(int matr[ARRAY_SIZE][ARRAY_SIZE]);
bool diff(int matr[ARRAY_SIZE][ARRAY_SIZE], int new_arr[ARRAY_SIZE][ARRAY_SIZE]);
/*
如果相同返回0,不同返回1
pre:
post:
*/
int sum(int matr[ARRAY_SIZE][ARRAY_SIZE]);
void main()
{
// ofstream cout;
// cout.open("out.txt");
int array[ARRAY_SIZE][ARRAY_SIZE] ;
int array2[ARRAY_SIZE][ARRAY_SIZE];
input(array);
cout << endl;
cout << "The original generation:\n";
print_matr(array);
Sleep(1000);
// memset(array2, 0, 151); //如果在这里没有把数组初始化好,输出的数据可能会不正常
empty_array(array2);
cout << "See the generations of these Matrix:\n";
int count=0;
for(int index=0; index<=500; index++) //循环次数
{
count=count+new_array(array,array2);
cout << "The " << count << "th Generation with "<< sum(array2) << " residents:\n";
print_matr(array2);
system("cls");
count=count+new_array(array2,array);
cout << "The " << count << "th Generation with "<< sum(array) << " residents:\n";
print_matr(array);
system("cls");
if (!diff(array, array2))
break;
}
// lord(3,3, array);
cout << "The " << count << "th Generation with "<< sum(array) << " residents:\n";
print_matr(array);
system("pause");
}
int sum(int matr[ARRAY_SIZE][ARRAY_SIZE])
{
int sum=0;
for (int i=0; i<ARRAY_SIZE; i++)
{
for (int j=0; j<ARRAY_SIZE; j++)
sum=sum+matr[i][j];
}
return sum;
}
bool diff(int matr[ARRAY_SIZE][ARRAY_SIZE], int new_arr[ARRAY_SIZE][ARRAY_SIZE])
{
for (int i=0; i<ARRAY_SIZE; i++)
{
for (int j=0; j<ARRAY_SIZE; j++)
if(matr[i][j]==new_arr[i][j])
;
else
return 1;
}
return 0;
}
void empty_array(int matr[ARRAY_SIZE][ARRAY_SIZE])
{
for (int i=0; i<ARRAY_SIZE; i++) //size = 3
{
for (int j=0; j<ARRAY_SIZE; j++)
matr[i][j]=0;
}
}
int new_array(int matr[ARRAY_SIZE][ARRAY_SIZE], int new_arr[ARRAY_SIZE][ARRAY_SIZE])
{
// cout << sizeof(new_arr) << endl; //新数组的大小,输出好像不正确
for (int i=1; i<ARRAY_SIZE-1; i++) //size = 3
{
for (int j=1; j<ARRAY_SIZE-1; j++)
new_arr[i][j]=lord(i,j,matr);
}
return 1;
}
void input(int matr[ARRAY_SIZE][ARRAY_SIZE])
{
// int matr[size*size];
// int matr[ARRAY_SIZE][ARRAY_SIZE];
// cout << "Input the size of matrix:\n";
cout << "Paste the block in the begin of this code here:\n";
for (int i=0; i<ARRAY_SIZE; i++) //size = 3
{
for (int j=0; j<ARRAY_SIZE; j++)
cin >> matr[i][j];
}
}
//void check(
/**/
bool lord(int i, int j, int matr[ARRAY_SIZE][ARRAY_SIZE])
{
int sum;
sum=matr[i-1][j-1]+matr[i-1][j]+matr[i-1][j+1]+matr[i][j-1]+matr[i][j+1]+matr[i+1][j-1]+matr[i+1][j]+matr[i+1][j+1];
switch(sum)
{
case 0:
case 1:
return 0;
case 2:
return matr[i][j];
// return 0;
case 3:
return 1;
case 4:
case 5:
case 6:
case 7:
case 8:
return 0;
}
}
/**/
void print_matr(int matr[ARRAY_SIZE][ARRAY_SIZE])
{
for (int i=0; i<ARRAY_SIZE; i++)
{
for (int j=0; j<ARRAY_SIZE; j++)
if(matr[i][j]==1)
cout << '*';
else
cout << ' ';
cout << endl;
}
cout << endl;
}
/*
void print_matr_file(istream infile, ofstream)
{
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -