C实现分子沉积模拟的示例代码
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
复制代码 代码如下:
/******************分子沉积模拟器****************/
/* 主要功能:模拟单片分子沉积 */
/*-------------------By TJX---------------------*/
#include<stdio.h>
#include<graphics.h>
#include<alloc.h>
#include<stdlib.h>
#include<time.h>
float dir; /*移动方向概率参数在0-1间*/
int main()
{int i,count=0; /*设置分子总数*/
int gdriver=DETECT, gmode,errorcode,size;
int x,y;
time_t lt;
unsigned seed;
char **a;
void *buf,*buf1;
a=(char**)malloc(100*sizeof(char*)); /*分配内存空间*/
for(i=0;i<100;i++)
{a[i]=(char*)malloc(167*sizeof(char));
memset(a[i],0,167);} /*用0初始化数组,0代表该处无分子,1代表有分子*/
lt=time(NULL);
seed=(unsigned)lt;
srand(seed); /*用时间初始化随机器*/
printf("\n\n\n\n\tPlease Input The Pf(0<=pf<=1):\n");
do{
scanf("%f",&dir); /*获取移动方向概率参数*/
getch();
}while(dir<0||dir>1);
clrscr();
initgraph(&gdriver, &gmode, "c:\\tc"); /*初始化图形驱动*/
errorcode = graphresult(); /*检测是否初始化成功*/
if (errorcode != grOk) /* an error occurred 错误发生则退出*/
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
for(i=0;i<100;i++) free(a[i]);
free(a);
exit(1); /* terminate with an error code */
}
setbkcolor(BLUE);
cleardevice();
size=imagesize(2,2,4,4); /*返回用于橡皮擦的图片字节数*/
buf=malloc(size); /*为其分配内存*/
size=imagesize(9,49,11,51); /*返回用于覆盖的图片字节数*/
buf1=malloc(size);
drawscreen(buf,buf1);
drawtxtscr(dir); /*绘制界面*/
putimage(9,49,buf,COPY_PUT); /*清除9,49处的分子*/
getch();
do{
x=3*(int)(166.0*(rand()/32767.0)); /*随机生成分子出现位置*/
move(x,a,buf,buf1); /*分子移动*/
}while(++count<10);
getch();
closegraph();
free(buf);
free(buf1); /*释放内存*/
for(i=0;i<100;i++) free(a[i]);
free(a);
return 0;
}
drawtxtscr()
{ int ud[8]={10,320,490,320,490,400,10,400};
char s[60];
setcolor(YELLOW);
setlinestyle(0,0,NORM_WIDTH);
setfillstyle(1,CYAN);
fillpoly(4,ud);
sprintf(s,"The downwards probobility of the atom equal:\n%.1f",dir);
settextstyle(2,0,4);
outtextxy(30,335,s);
settextstyle(4,0,2);
outtextxy(190,375,"---TJX---");
}
drawscreen(void *bu,void *bu1) /*绘制工作区*/
{ int userdata[8]={0,0,501,0,501,300,0,300};
int size;
setbkcolor(BLUE);
cleardevice();
setcolor(GREEN);
setlinestyle(0,0,NORM_WIDTH); /*设置线形*/
setviewport(69,69,639,479,1); /*设置显示区*/
setfillstyle(1,GREEN);
fillpoly(4,userdata);
getimage(2,2,4,4,bu); /*将获取模拟橡皮擦存入内存*/
setcolor(YELLOW);
setfillstyle(1,YELLOW);
circle(10,50,1); /*绘制模拟分子*/
floodfill(10,50,YELLOW); /*填充黄色*/
getimage(9,49,11,51,bu1); /*将模拟分子存入内存*/
}
move(int x,char **a,void *buf,void *buf1)
{ float dirction;
int sx,sy=0,i,j,end=0,start=0;
sx=x;
do{
if(sx==0) start=1; /*判定分子出现位置*/
else if(sx>0&&sx<498) start=2;
else start=3;
j=sx/3; /*记录*/
i=sy/3;
if(start==1&&sy<297&&(a[i+1][j]+a[i][j+1])==0) /*判定其四周是否有分子存在*/
{ dirction=(float)(rand()/32627.0); /*随机生成分子运动方向*/
if(dirction<=dir) {sy=sy+3 ;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx,sy-3,buf,COPY_PUT);}
else if(dirction>dir&&dirction<=(1+dir)/2)
{sy=sy+3;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx,sy-3,buf,COPY_PUT);}
else if(dirction>(1+dir)/2 && dirction<=1.0)
{ sx=sx+3;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx-3,sy,buf,COPY_PUT); }
}
else if(start==2&&sy<297&&(a[i][j-1]+a[i+1][j]+a[i][j+1])==0)
{ dirction=(float)(rand()/32627.0);
if(dirction<=dir) {sy=sy+3;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx,sy-3,buf,COPY_PUT);}
else if(dirction>dir&&dirction<=(1+dir)/2)
{sx=sx-3;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx+3,sy,buf,COPY_PUT);}
else if(dirction>(1+dir)/2 && dirction<=1.0)
{ sx=sx+3;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx-3,sy,buf,COPY_PUT); }
}
else if(start==3&&sy<297&&(a[i][j-1]+a[i+1][j])==0)
{ dirction=(float)(rand()/32627.0);
if(dirction<=dir) {sy=sy+3 ;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx,sy-3,buf,COPY_PUT);}
else if(dirction>dir&&dirction<=(1+dir)/2)
{sx=sx-3;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx+3,sy,buf,COPY_PUT);}
else
{ sy=sy+3;
putimage(sx,sy,buf1,COPY_PUT);
putimage(sx,sy-3,buf,COPY_PUT); }
}
else end=1;
}while(!end);
a[i][j]=1;
}
您可能感兴趣的文章
- 01-10数据结构课程设计-用栈实现表达式求值的方法详解
- 01-10使用OpenGL实现3D立体显示的程序代码
- 01-10求斐波那契(Fibonacci)数列通项的七种实现方法
- 01-10C语言 解决不用+、-、&#215;、&#247;数字运算符做加法
- 01-10使用C++实现全排列算法的方法详解
- 01-10用C++实现DBSCAN聚类算法
- 01-10深入全排列算法及其实现方法
- 01-10全排列算法的非递归实现与递归实现的方法(C++)
- 01-10用C语言实现单链表的各种操作(一)
- 01-10用C语言实现单链表的各种操作(二)


阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10delphi制作wav文件的方法
- 04-02jquery与jsp,用jquery
- 01-10C#中split用法实例总结
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10SublimeText编译C开发环境设置
- 01-10使用C语言求解扑克牌的顺子及n个骰子