
正文
c语言for函数图形 c语言 for函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
C语言编程 利用for循环输出图形 1 2 3 4 5 6 7 8 9 10 ..........
生命游戏
/* ------------------------------------------------------ */
/* PROGRAM game of life : */
/* This is a finite implementation of John H. Conway's */
/* Game of Life. Refere to my book for detail please. */
/* */
/* Copyright Ching-Kuang Shene July/25/1989 */
/* ------------------------------------------------------ */
#include stdio.h
#include stdlib.h
#define MAXSIZE 50 /* board size */
#define OCCUPIED 1 /* occupied flag */
#define UNOCCUPIED 0
#define YES 1
#define NO 0
char cell[MAXSIZE][MAXSIZE]; /* the board */
char workcopy[MAXSIZE][MAXSIZE]; /* a working copy */
int row; /* No. of rows you want */
int column; /* no. of columns you want */
int generations; /* maximum no. of generation*/
/* ------------------------------------------------------ */
/* FUNCTION read_in : */
/* This function reads in the number of generations, */
/* the number of rows, the number of columns and finally */
/* the initial configuration (the generation 0). Then put*/
/* your configuration to the center of the board. */
/* ------------------------------------------------------ */
void read_in(void)
{
int max_row, max_col; /* max # of row and col. */
int col_gap, row_gap; /* incremnet of row and col */
int i, j;
char line[100];
gets(line); /* read in gens, row and col*/
sscanf(line, "%d%d%d", generations, row, column);
for (i = 0; i row; i++)/* clear the board */
for (j = 0; j column; j++)
cell[i][j] = UNOCCUPIED;
max_col = 0; /* read in the config. */
for (max_row = 0; gets(line) != NULL; max_row++) {
for (i = 0; line[i] != '\0'; i++)
if (line[i] != ' ')
cell[max_row][i] = OCCUPIED;
max_col = (max_col i) ? i : max_col;
}
row_gap = (row - max_row)/2; /* the moving gap */
col_gap = (column - max_col)/2;
for (i = max_row + row_gap - 1; i = row_gap; i--) {
for (j = max_col + col_gap - 1; j = col_gap; j--)
cell[i][j] = cell[i-row_gap][j-col_gap];
for ( ; j = 0; j--)
cell[i][j] = UNOCCUPIED;
}
for ( ; i = 0; i--)
for (j = 0; j column; j++)
cell[i][j] = UNOCCUPIED;
}
/* ------------------------------------------------------ */
/* FUNCTION display : */
/* Display the board. */
/* ------------------------------------------------------ */
#define DRAW_BOARDER(n) { int i; \
printf("\n+"); \
for (i = 0; i n; i++) \
printf("-"); \
printf("+"); \
}
void display(int gen_no)
{
int i, j;
if (gen_no == 0)
printf("\n\nInitial Generation :\n");
else
printf("\n\nGeneration %d :\n", gen_no);
DRAW_BOARDER(column);
for (i = 0; i row; i++) {
printf("\n|");
for (j = 0; j column; j++)
printf("%c", (cell[i][j] == OCCUPIED) ? '*' : ' ');
printf("|");
}
DRAW_BOARDER(column);
}
/* ------------------------------------------------------ */
/* FUNCTION game_of_life : */
/* This is the main function of Game of Life. */
/* ------------------------------------------------------ */
void game_of_life(void)
{
int stable; /* stable flag */
int iter; /* iteration count */
int top, bottom, left, right; /* neighborhood bound */
int neighbors; /* # of neighbors */
int cell_count; /* # of cells count */
int done;
int i, j, p, q;
display(0); /* display initial config. */
done = NO;
for (iter = 1; iter = generations !done; iter++) {
memmove(workcopy, cell, MAXSIZE*MAXSIZE); /*copy*/
stable = YES; /* assume it is in stable */
cell_count = 0; /* # of survived cells = 0 */
for (i = 0; i row; i++) { /* scan each cell...*/
top = (i == 0) ? 0 : i - 1;
bottom = (i == row - 1) ? row-1 : i + 1;
for (j = 0; j column; j++) {
left = (j == 0) ? 0 : j - 1;
right = (j == column - 1) ? column-1 : j + 1;
/* compute number of neighbors */
neighbors = 0;
for (p = top; p = bottom; p++)
for (q = left; q = right; q++)
neighbors += workcopy[p][q];
neighbors -= workcopy[i][j];
/* determine life or dead */
if (workcopy[i][j] == OCCUPIED)
if (neighbors == 2 || neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
else if (neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
stable = stable (workcopy[i][j] == cell[i][j]);
}
}
if (cell_count == 0) {
printf("\n\nAll cells die out.");
done = YES;
}
else if (stable) {
printf("\n\nSystem enters a stable state.");
done = YES;
}
else
display(iter);
}
}
/* ------------------------------------------------------ */
void main(void)
{
read_in();
game_of_life();
}
求采纳为满意回答。
相关问答
Q1: C语言编程:使用for循环输出如下图形
记着输入*最多的个数n即可,还可以实现客户重新输入
#includeiostream
using
namespace
std;
int
main()
{
int
i,k,j,n;
char
a;
do
{
cout"请输入你所要图形*最多为n:";
cinn;
for(i=1;i=n;i++)
{
for(k=1;k=n-i;k++)
cout"
";
{
for(j=1;j=i;j++)
cout"
*";
coutendl;
}
}
for(i=1;i=n;i++)
{
for(k=1;k=i;k++)
cout"
";
{
for(j=n-i;j=1;j--)
cout"
*";
coutendl;
}
}
cout"你是否需要继续?(y/n):\n";
cina;
}while(a=='y'||a=='Y');
return
0;
}
Q2: c语言题:利用for循环编程,输出如下图形
记着输入*最多的个数n即可,还可以实现客户重新输入
#include
using namespace std;
int main()
{
int i,k,j,n;
char a;
do
{
cout"请输入你所要图形*最多为n:";
cinn;
for(i=1;i=n;i++)
{
for(k=1;k=n-i;k++)
cout" ";
{
for(j=1;j=i;j++)
cout" *";
coutendl;
}
}
for(i=1;i=n;i++)
{
for(k=1;k=i;k++)
cout" ";
{
for(j=n-i;j=1;j--)
cout" *";
coutendl;
}
}
cout"你是否需要继续?(y/n):\n";
cina;
}while(a=='y'||a=='Y');
return 0;
}
Q3: 如何用c语言双for循环输出下面图形??
对一个二维图形的输出,一般可以使用双重循环进行控制输出,其中外层循环控制输出的行数,内存循环输出每行的信息。
对于第1题,参考代码如下:
#include stdio.h
#include stdlib.h
int main(int argc, char*argv[])
{
int i,j;
int n=4;
for(i=1;i=n;i++)//控制输出n行
{
for(j=0;j2*i-1;j++) //第i行控制输出2*i-1个*
{
printf("*");
}
printf("\n");
}
system("pause");
return 0;
}
编译后的运行结果如下:
对于第2题,参考源程序如下:
#include stdio.h
#include stdlib.h
int main(int argc, char*argv[])
{
int i,j;
int n=4;
for(i=1;i=n;i++)//控制输出n行
{
//第i行输出n-i个空格
for(j=0;jn-i;j++)
{
printf(" ");
}
//第i行输出2*i-1个*
for(j=0;j2*i-1;j++)
{
printf("*");
}
printf("\n");
}
system("pause");
return 0;
}
Q4: 用C语言的双重for循环语句怎么输出如下图形
#includestdio.h
void main()
{
int i,j,k;
for(i=0;i=3;i++) //输出上面四行*号
{
for(j=0;j=2-i;j++)
printf(" "); //输出*号前面的空格
for(k=0;k=2*i;k++)
printf("*"); //输出*号
printf("\n");
}
for(i=0;i=2;i++) //输出下面三行*号
{
for(j=0;j=i;j++)
printf(" ");
for(k=0;k=4-2*i;k++)
printf("*");
printf("\n");
}
}
我已经运行过了,没有问题!祝你成功!
关于c语言for函数图形和c语言 for函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








