C實現(xiàn)分子沉積模擬的示例代碼
來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次
復制代碼 代碼如下:
/******************分子沉積模擬器****************/
/* 主要功能:模擬單片分子沉積 */
/*-------------------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ù)*/
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); /*用時間初始化隨機器*/
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)); /*隨機生成分子出現(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); /*設置線形*/
setviewport(69,69,639,479,1); /*設置顯示區(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); /*隨機生成分子運動方向*/
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數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(一)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(二)


閱讀排行
本欄相關
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10使用C語言求解撲克牌的順子及n個骰子