過橋問題動畫顯示(多線程,簡陋版)

來源:互聯網
上載者:User
Code:
  1. #include<windows.h>   
  2. #include<iostream.h>   
  3. #include<stdio.h>   
  4. void gotoxy(int x,int y)     
  5. {   
  6.     COORD c;   
  7.     c.X=x;   
  8.     c.Y=y;   
  9.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);   
  10. }   
  11.   
  12. /*四個訊號量,東,西方向訊號量,東,西段互斥訊號量,用訊號量控制過橋的對象,同時過橋的對象僅且
     
  13. 只有兩個,並且不能同向,多個線程用同一個線程入口函數*/  
  14.   
  15. HANDLE hE_Dir;  //從西往東訊號量
      
  16. HANDLE hW_Dir; //從西往東訊號量 
      
  17. HANDLE hE_Mutex; //西段線程間的互斥
      
  18. HANDLE hW_Mutex;  //東段線程互斥
      
  19.   
  20. DWORD WINAPI EtoW1_Proc(LPVOID lpParameter);//東到西邊線程
      
  21.   
  22. DWORD WINAPI WtoE1_Proc(LPVOID lpParameter);//西到東邊線程
      
  23.   
  24.   
  25. void bridge();  //建立過橋函數
      
  26. int i;   
  27. int j;   
  28. int time1=100,time2=300;   
  29. BOOL E_Release;   
  30. BOOL W_Release;   
  31.   
  32. int main()   
  33. {   
  34.     bridge();  //橋型   
  35.     gotoxy(12,12);  //游標第一次到(12,12)
      
  36.      //各個互斥訊號量   
  37.     hE_Dir = CreateMutex(NULL,FALSE,NULL);      
  38.     hW_Dir = CreateMutex(NULL,FALSE,NULL);   
  39.     hE_Mutex = CreateMutex(NULL,FALSE,NULL);   
  40.     hW_Mutex = CreateMutex(NULL,FALSE,NULL);   
  41.     //線程建立   
  42.     HANDLE hThread1,hThread2;   
  43.     hThread1=CreateThread(NULL,0,WtoE1_Proc,NULL,0,NULL);   
  44.     hThread2=CreateThread(NULL,0,EtoW1_Proc,NULL,0,NULL);   
  45.     CloseHandle(hThread1);   
  46.     CloseHandle(hThread2);   
  47.   
  48.   
  49.   
  50.        
  51.     Sleep(200000);  //主線程沉睡200秒
      
  52.   
  53.     return 0;   
  54. }   
  55.   
  56. //西到東邊線程   
  57. DWORD WINAPI WtoE1_Proc(LPVOID lpParameter)   
  58. {   
  59.     while(TRUE)   
  60.     {   
  61.   
  62.         WaitForSingleObject(hE_Dir,INFINITE);   //從西到東互斥
      
  63.         WaitForSingleObject(hE_Mutex,INFINITE); //進入西段,互斥
      
  64.         gotoxy(12,12);   
  65.         for(i=14;i<=34;i++)   
  66.         {   
  67.             gotoxy(i,12);   
  68.             time1=(time1+180)%500;   //隨機速度
      
  69.             Sleep(time1);   
  70.             gotoxy(i,12);//切換線程後,回到原地
      
  71.             printf("☆");   
  72.             //gotoxy(i,12);   
  73.         }   
  74.   
  75.         W_Release = ReleaseMutex(hE_Mutex); //離開西段
      
  76.         if(W_Release)/*判斷誰先到,先到等1秒鐘*/ Sleep(1000);   //到中間地區休息,等待另一方到來
      
  77.         gotoxy(i,12);   
  78.         printf(" ");   
  79.         gotoxy(34,11);   
  80.         printf("☆");   
  81.         Sleep(100);   
  82.         gotoxy(34,11);   
  83.         printf("");   
  84.         gotoxy(35,11);   
  85.         printf("☆");   
  86.         Sleep(100);   
  87.         gotoxy(35,11);   
  88.         printf(" ");   
  89.   
  90.         WaitForSingleObject(hW_Mutex,INFINITE);    //進入東段,互斥
      
  91.   
  92.         for(i=35;i<=56;i++)   
  93.         {   
  94.             gotoxy(i,12);   
  95.             Sleep(90);   
  96.             gotoxy(i,12);   
  97.             printf("☆");   
  98.             gotoxy(i,12);   
  99.         }   
  100.         gotoxy(i,12);   
  101.         printf(" ");   
  102.         ReleaseMutex(hW_Mutex);     //解開東段互斥
      
  103.         //ReleaseMutex(hE_Mutex);
      
  104.     }   
  105. //  return 0;   
  106. }   
  107.   
  108. //東到西邊線程   
  109. DWORD WINAPI EtoW1_Proc(LPVOID lpParameter)   
  110. {   
  111. while(TRUE)   
  112.     {   
  113.   
  114.         WaitForSingleObject(hW_Dir,INFINITE);   //從東到西互斥
      
  115.         WaitForSingleObject(hW_Mutex,INFINITE); //進東段 ,互斥
      
  116.          
  117.         gotoxy(56,12);   
  118.         for(j=54;j>=36;j--)   
  119.         {   
  120.             gotoxy(j,12);   
  121.             time2=(time2+80)%500;   
  122.             Sleep(time2);     
  123.             gotoxy(j,12);  //切換線程後,回到原地
      
  124.             printf("★");   
  125.             gotoxy(j,12);   
  126.         }   
  127.            
  128.         E_Release = ReleaseMutex(hW_Mutex);   //離開東段,
      
  129.         if(E_Release) Sleep(1000);   
  130.         gotoxy(j+1,12);   
  131.         printf(" ");   
  132.         gotoxy(36,13);   
  133.         printf("★");   
  134.         Sleep(100);   
  135.         gotoxy(36,13);   
  136.         printf("");   
  137.         gotoxy(35,13);   
  138.         printf("★");   
  139.         Sleep(100);   
  140.         gotoxy(35,13);   
  141.         printf(" ");   
  142.         gotoxy(34,13);   
  143.         printf("★");   
  144.         Sleep(100);   
  145.         gotoxy(34,13);   
  146.         printf(" ");   
  147.   
  148. WaitForSingleObject(hE_Mutex,INFINITE);//進入西段,互斥
      
  149.   
  150.         for(j=32;j>=12;j--)   
  151.         {   
  152.             gotoxy(j,12);   
  153.             Sleep(100);   
  154.             gotoxy(j,12);   
  155.             printf("★");   
  156.         }   
  157.         gotoxy(j+1,12);   
  158.         printf(" ");   
  159.           ReleaseMutex(hE_Mutex);//離開西段段
      
  160.            
  161.     }   
  162. return 0;   
  163. }   
  164.   
  165.   
  166. void bridge()   
  167. {   
  168.     //☆   █   ★   //   
  169.     ///////////////////////////////建立小橋//////////////////////////////////////////
      
  170.     gotoxy(20+10,8);                                                //8行30列 
      
  171.     printf("█████");     
  172.     gotoxy(20+10,9);                                                //9行30列
      
  173.     printf("█      █");      
  174.     gotoxy(20+10,10);                                               //10行30列
      
  175.     printf("█      █");   
  176.     gotoxy(4+10,11);                                                //11行14列
      
  177.     printf("█████████      █████████");   
  178.     gotoxy(4+10,13);                                               //13行14列
      
  179.     printf("█████████      █████████");   
  180.     gotoxy(20+10,14);                                               //14行30列
      
  181.     printf("█      █");   
  182.     gotoxy(20+10,15);                                               //15行30列
      
  183.     printf("█      █");   
  184.     gotoxy(20+10,16);                                                //16行30列
      
  185.     printf("█████");   
  186.        
  187.     //gotoxy(56,12);   //右邊起始點
      
  188.     //printf("★");   
  189.     //gotoxy(13,12);    //左邊起始點
      
  190.     //printf("☆");   
  191.     //          右邊中間        gotoxy(38,12); 
      
  192.     //          左邊中間        gotoxy(30,12);
      
  193. //////////////////////////////////////////////////////////////////////////////
      
  194. }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.