C implement sample code for molecular deposition simulation _c language

Source: Internet
Author: User
Tags int size rand

Copy Code code as follows:

/****************** Molecular deposition simulator ****************/
/* Main function: Analog monolithic molecular deposition * *
/*-------------------by TJX---------------------* *

#include <stdio.h>
#include <graphics.h>
#include <alloc.h>
#include <stdlib.h>
#include <time.h>
float dir; /* Moving direction probability parameters in 0-1 * *
int main ()
{int i,count=0; /* Set the total number of molecules * *
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*)); /* Allocate memory space/*
for (i=0;i<100;i++)
{a[i]= (char*) malloc (167*sizeof (char));
memset (a[i],0,167);} /* Initializes the array with 0, 0 represents the molecule, and 1 represents the molecule.
Lt=time (NULL);
seed= (unsigned) lt;
Srand (seed); /* Use time to initialize the random device * *
printf ("\n\n\n\n\tplease Input Pf (0<=pf<=1): \ n");
do{
scanf ("%f", &dir); /* Get the moving direction probability parameter * *
Getch ();
}while (dir<0| | DIR&GT;1);

CLRSCR ();

Initgraph (&gdriver, &gmode, "C:\\TC"); /* Initialize graphics driver/*
ErrorCode = Graphresult (); /* Detect if initialization succeeded/*
if (errorcode!= grOk)/* An error occurred errors occur then exit * *
{
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); /* Returns the number of picture bytes used for the eraser * *
Buf=malloc (size); /* Allocate memory for it * *
Size=imagesize (9,49,11,51); /* Returns the number of image bytes used for overlay * *
Buf1=malloc (size);
Drawscreen (BUF,BUF1);
Drawtxtscr (dir); /* Draw Interface * *
Putimage (9,49,buf,copy_put); /* Clear 9, 49 of the molecule * *
Getch ();
do{
x=3* (int) (166.0* (rand ()/32767.0)); /* Random generation of molecules appear in the position * *
Move (X,A,BUF,BUF1); * * Molecular Mobile/
}while (++COUNT&LT;10);
Getch ();
Closegraph ();
Free (BUF);
Free (BUF1); /* FREE Memory * *
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)/* Draw Workspace * *
{int userdata[8]={0,0,501,0,501,300,0,300};
int size;
SetBkColor (BLUE);
Cleardevice ();
SetColor (GREEN);
Setlinestyle (0,0,norm_width); /* Set Linear */
SetViewport (69,69,639,479,1); /* Set Display area * *
Setfillstyle (1,green);
Fillpoly (4,userdata);
GetImage (2,2,4,4,BU); /* will get the simulated eraser into memory/*
SetColor (yellow);
Setfillstyle (1,yellow);
Circle (10,50,1); /* Draw the simulation molecule * *
FloodFill (10,50,yellow); * * Fill Yellow/
GetImage (9,49,11,51,BU1); /* Put the analog molecule into memory/*
}
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; * * The position of the decision molecule
else if (sx>0&&sx<498) start=2;
else start=3;
J=SX/3; /* Record * *
I=SY/3;
if (start==1&&sy<297&& (a[i+1][j]+a[i][j+1]) ==0)/* Determine whether there are any molecules around it.
{dirction= (float) (rand ()/32627.0); /* Randomly generated molecular motion direction * *
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.