首页 > 编程 > C > 正文

C实现分子沉积模拟的示例代码

2020-01-26 15:42:20
字体:
来源:转载
供稿:网友

复制代码 代码如下:

/******************分子沉积模拟器****************/
/* 主要功能:模拟单片分子沉积                    */
/*-------------------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;
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选