C實現(xiàn)分子沉積模擬的示例代碼
更新時間:2013年11月19日 14:40:26 作者:
這篇文章主要介紹了計算機(jī)在材料科學(xué)中的一個練習(xí)題,功能是模擬氣化后分子沉積
復(fù)制代碼 代碼如下:
/******************分子沉積模擬器****************/
/* 主要功能:模擬單片分子沉積 */
/*-------------------By TJX---------------------*/
#include<stdio.h>
#include<graphics.h>
#include<alloc.h>
#include<stdlib.h>
#include<time.h>
float dir; /*移動方向概率參數(shù)在0-1間*/
int main()
{int i,count=0; /*設(shè)置分子總數(shù)*/
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*)); /*分配內(nèi)存空間*/
for(i=0;i<100;i++)
{a[i]=(char*)malloc(167*sizeof(char));
memset(a[i],0,167);} /*用0初始化數(shù)組,0代表該處無分子,1代表有分子*/
lt=time(NULL);
seed=(unsigned)lt;
srand(seed); /*用時間初始化隨機(jī)器*/
printf("\n\n\n\n\tPlease Input The Pf(0<=pf<=1):\n");
do{
scanf("%f",&dir); /*獲取移動方向概率參數(shù)*/
getch();
}while(dir<0||dir>1);
clrscr();
initgraph(&gdriver, &gmode, "c:\\tc"); /*初始化圖形驅(qū)動*/
errorcode = graphresult(); /*檢測是否初始化成功*/
if (errorcode != grOk) /* an error occurred 錯誤發(fā)生則退出*/
{
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); /*返回用于橡皮擦的圖片字節(jié)數(shù)*/
buf=malloc(size); /*為其分配內(nèi)存*/
size=imagesize(9,49,11,51); /*返回用于覆蓋的圖片字節(jié)數(shù)*/
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)); /*隨機(jī)生成分子出現(xiàn)位置*/
move(x,a,buf,buf1); /*分子移動*/
}while(++count<10);
getch();
closegraph();
free(buf);
free(buf1); /*釋放內(nèi)存*/
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) /*繪制工作區(qū)*/
{ int userdata[8]={0,0,501,0,501,300,0,300};
int size;
setbkcolor(BLUE);
cleardevice();
setcolor(GREEN);
setlinestyle(0,0,NORM_WIDTH); /*設(shè)置線形*/
setviewport(69,69,639,479,1); /*設(shè)置顯示區(qū)*/
setfillstyle(1,GREEN);
fillpoly(4,userdata);
getimage(2,2,4,4,bu); /*將獲取模擬橡皮擦存入內(nèi)存*/
setcolor(YELLOW);
setfillstyle(1,YELLOW);
circle(10,50,1); /*繪制模擬分子*/
floodfill(10,50,YELLOW); /*填充黃色*/
getimage(9,49,11,51,bu1); /*將模擬分子存入內(nèi)存*/
}
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; /*判定分子出現(xiàn)位置*/
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); /*隨機(jī)生成分子運動方向*/
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;
}
相關(guān)文章
C++中vector和數(shù)組之間的轉(zhuǎn)換及其效率問題詳解
c++?vector轉(zhuǎn)數(shù)組是一種將vector容器的元素轉(zhuǎn)換為數(shù)組的方法,主要能幫助提高程序的性能和效率,下面這篇文章主要給大家介紹了關(guān)于C++中vector和數(shù)組之間的轉(zhuǎn)換及其效率問題的相關(guān)資料,需要的朋友可以參考下2023-03-03C++實現(xiàn)神經(jīng)BP神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)神經(jīng)BP神經(jīng)網(wǎng)絡(luò),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05C++實現(xiàn)LeetCode(59.螺旋矩陣之二)
這篇文章主要介紹了C++實現(xiàn)LeetCode(59.螺旋矩陣之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C語言中枚舉與聯(lián)合體的使用方法(enum union)
枚舉的意思就是列舉,將每一個可能的取值都進(jìn)行一一列舉,下面這篇文章主要給大家介紹了關(guān)于C語言中枚舉與聯(lián)合體的使用方法,需要的朋友可以參考下2021-09-09