程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C完成份子堆積模仿的示例代碼

C完成份子堆積模仿的示例代碼

編輯:關於C++

C完成份子堆積模仿的示例代碼。本站提示廣大學習愛好者:(C完成份子堆積模仿的示例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C完成份子堆積模仿的示例代碼正文



/******************份子堆積模仿器****************/
/* 重要功效:模仿單片份子堆積                    */
/*-------------------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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved