C實(shí)現(xiàn)分子沉積模擬的示例代碼
更新時(shí)間:2013年11月19日 14:40:26 作者:
這篇文章主要介紹了計(jì)算機(jī)在材料科學(xué)中的一個(gè)練習(xí)題,功能是模擬氣化后分子沉積
復(fù)制代碼 代碼如下:
/******************分子沉積模擬器****************/
/* 主要功能:模擬單片分子沉積 */
/*-------------------By TJX---------------------*/
#include<stdio.h>
#include<graphics.h>
#include<alloc.h>
#include<stdlib.h>
#include<time.h>
float dir; /*移動(dòng)方向概率參數(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); /*用時(shí)間初始化隨機(jī)器*/
printf("\n\n\n\n\tPlease Input The Pf(0<=pf<=1):\n");
do{
scanf("%f",&dir); /*獲取移動(dòng)方向概率參數(shù)*/
getch();
}while(dir<0||dir>1);
clrscr();
initgraph(&gdriver, &gmode, "c:\\tc"); /*初始化圖形驅(qū)動(dòng)*/
errorcode = graphresult(); /*檢測(cè)是否初始化成功*/
if (errorcode != grOk) /* an error occurred 錯(cuò)誤發(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); /*分子移動(dòng)*/
}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ī)生成分子運(yùn)動(dòng)方向*/
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語言鏈表基本操作(帶有創(chuàng)建鏈表 刪除 打印 插入)
這篇文章主要介紹了c語言鏈表基本操作,大家參考使用吧2013-12-12
Qt?TCP網(wǎng)絡(luò)通信學(xué)習(xí)
用于數(shù)據(jù)傳輸?shù)牡蛯泳W(wǎng)絡(luò)協(xié)議,多個(gè)物聯(lián)網(wǎng)協(xié)議都是基于TCP協(xié)議的,這篇文章為大家介紹了Qt?TCP網(wǎng)絡(luò)通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C語言根據(jù)協(xié)議分割獲取字符串單元的實(shí)現(xiàn)代碼
今天小編就為大家分享一篇關(guān)于C語言根據(jù)協(xié)議分割獲取字符串單元的實(shí)現(xiàn)代碼,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
C語言中的結(jié)構(gòu)體的入門學(xué)習(xí)教程
這篇文章主要介紹了C語言中的結(jié)構(gòu)體的入門學(xué)習(xí)教程,以struct語句定義的結(jié)構(gòu)體是C語言編程中的重要基礎(chǔ),需要的朋友可以參考下2015-12-12
C/C++判斷傳入的UTC時(shí)間是否當(dāng)天的實(shí)現(xiàn)方法
在項(xiàng)目中經(jīng)常會(huì)顯示一個(gè)時(shí)間,如果這個(gè)時(shí)間在今日內(nèi)就顯示為時(shí)分秒,否則顯示為年月日,有需要的朋友可以參考一下2014-01-01

