沃尔夫勒姆自动机时空图输出 C语言实现

 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h> //行宽度
#define ROW_LEN 38 //比特位域结构
typedef struct bits bits;
struct bits{ unsigned int c0 : ;
unsigned int c1 : ;
unsigned int c2 : ;
unsigned int c3 : ;
unsigned int c4 : ;
unsigned int c5 : ;
unsigned int c6 : ;
unsigned int c7 : ;
}; //行类型
typedef bits row[( ROW_LEN + ) / ]; //读取行中元胞
unsigned int get_cell( row rw, int x ){ unsigned int re = ; if( x < - || x > ROW_LEN ){ puts( "get_cell: 坐标错误." ); return re;
} if( - == x ){ x = ROW_LEN - ;
} if( ROW_LEN == x ){ x = ;
} switch( x % ){ case :{ re = rw[x / ].c0;
}break; case :{ re = rw[x / ].c1;
}break; case :{ re = rw[x / ].c2;
}break; case :{ re = rw[x / ].c3;
}break; case :{ re = rw[x / ].c4;
}break; case :{ re = rw[x / ].c5;
}break; case :{ re = rw[x / ].c6;
}break; case :{ re = rw[x / ].c7;
}break;
} return re;
} //修改行中元胞
void set_cell( row rw, int x, unsigned int v ){ if( x < - || x > ROW_LEN ){ puts( "set_cell: 坐标错误." ); return;
} if( - == x ){ x = ROW_LEN - ;
} if( ROW_LEN == x ){ x = ;
} v = v % ; switch( x % ){ case :{ rw[x / ].c0 = v;
}break; case :{ rw[x / ].c1 = v;
}break; case :{ rw[x / ].c2 = v;
}break; case :{ rw[x / ].c3 = v;
}break; case :{ rw[x / ].c4 = v;
}break; case :{ rw[x / ].c5 = v;
}break; case :{ rw[x / ].c6 = v;
}break; case :{ rw[x / ].c7 = v;
}break;
}
} //演化行中元胞
unsigned int evo_cell( row rw, int x, unsigned char tab ){ unsigned char num = ; if( x < || x > ROW_LEN - ){ puts( "evo_cell: 坐标错误." ); return ;
} num |= ( unsigned char )get_cell( rw, x - );
num <<= ;
num |= ( unsigned char )get_cell( rw, x );
num <<= ;
num |= ( unsigned char )get_cell( rw, x + ); return ( tab >> num ) & 0x01;
} //演化行到另外一个行
void evo_row( row rw1, row rw2, unsigned char tab ){ int x; for( x = ; x < ROW_LEN; x++ ){ set_cell( rw2, x, evo_cell( rw1, x, tab ) );
}
} //随机初始化行
void rand_row( row rw ){ int x; for( x = ; x < ROW_LEN; x++ ){ set_cell( rw, x, rand() % );
}
} //显示行
void display_row( row rw ){ int x; for( x = ; x < ROW_LEN; x++ ){ printf( "%s", get_cell( rw, x ) ? "▉" : " " );
} printf( "\n" );
} //规则选择,这里选择为规则30,随机产生等腰三角形
//这种自动机是一种随机数算法的根基
#define TYPE_ID 30 //主函数
int main( int argc, char * argv[] ){ row rw1, rw2; srand( ( unsigned int )time( NULL ) ); rand_row( rw1 ); while( ){ display_row( rw1 ); _getch(); evo_row( rw1, rw2, TYPE_ID ); display_row( rw2 ); _getch(); evo_row( rw2, rw1, TYPE_ID );
} return ;
}

运行效果:

沃尔夫勒姆自动机时空图输出 C语言实现

上一篇:将decimal类型的数值后面的0和.号去掉


下一篇:premake 在64位Ubuntu系统下编译32位GCC程序