Advertisement
desdemona

auta wspolbiezne

Mar 21st, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdio.h>
  3.  
  4.  
  5. void gotoxy(int x, int y)
  6. {
  7.   COORD c;
  8.   c.X = x - 1;
  9.   c.Y = y - 1;
  10.   SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
  11. }
  12.  
  13. int pisanie;
  14.  
  15. #pragma argsused
  16. struct dane_dla_watku
  17. {
  18.     int i;
  19.     int samochodek;
  20. }myjnia = { 0, 0 };
  21.  
  22. struct dane
  23. {
  24.     struct dane_dla_watku* myjnia;
  25.     int numer;
  26. };
  27.  
  28. DWORD WINAPI funkcja_auta(void* argumenty);
  29. DWORD WINAPI funkcja_myjni(void* argumenty);
  30.  
  31. struct dane tmp[10];
  32.  
  33. int main(int argc, char **argv)
  34. {
  35.     int i,ilosc;
  36.     DWORD id;
  37.  
  38.     HANDLE watek;
  39.  
  40.     printf("Ile samochodow chcesz wypuscic na ulice?\n");
  41.     scanf("%d",&ilosc);
  42.  
  43.     CreateThread(
  44.             NULL,
  45.             0,
  46.             funkcja_myjni,
  47.             (void*)&myjnia,
  48.             0,
  49.             &id);
  50.    
  51.     for(i = 0; i<ilosc; i++)
  52.     {
  53.         tmp[i].myjnia = &myjnia;
  54.         tmp[i].numer = i+1;
  55.  
  56.         watek = CreateThread(
  57.                     NULL,
  58.                     0,
  59.                     funkcja_auta,
  60.                     (void*)&tmp[i],
  61.                     0,
  62.                     &id);
  63.     }
  64.  
  65.     //CreateProcess((LPCSTR)"C:\\Windows\\System32\\notepad.exe",NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
  66.  
  67.     Sleep(100000);
  68.  
  69.     return 0;
  70. }
  71.  
  72. DWORD WINAPI funkcja_auta(void* argumenty)
  73. {
  74.     struct dane* moje = (struct dane*)argumenty;
  75.  
  76.     while(pisanie);
  77.  
  78.         pisanie = 1;
  79.         gotoxy(0,moje->numer+2);
  80.         printf("Auto %d: Wyjezdzam na miasto!\n", moje->numer);
  81.         pisanie = 0;
  82.  
  83.     while(1)
  84.     {
  85.         Sleep((rand()%3000)+2000+moje->numer*100);
  86.         if(rand()%2 == 0)
  87.         {
  88.             if(moje->myjnia->i == 0)
  89.             {
  90.                 moje->myjnia->i = 1;
  91.                 moje->myjnia->samochodek = moje->numer;
  92.  
  93.                 while(pisanie);
  94.                
  95.                     pisanie = 1;
  96.                     gotoxy(0,moje->numer+2);
  97.                     printf("Auto %d: Wjezdzam do myjni\n", moje->numer);
  98.                     pisanie = 0;
  99.                
  100.                 Sleep(((rand()%4)+2)*1000);
  101.                 moje->myjnia->i = 0;
  102.  
  103.                 while(pisanie);
  104.                
  105.                     pisanie = 1;
  106.                     gotoxy(0,moje->numer+2);
  107.                     printf("Auto %d: Wjechalem z myjni\n", moje->numer);
  108.                     pisanie = 0;
  109.                
  110.             }
  111.             else
  112.             {
  113.                 while(pisanie);
  114.                
  115.                     pisanie = 1;
  116.                     gotoxy(0,moje->numer+2);
  117.                     printf("Auto %d: Chcialem jechac do myjni ale jest zajeta\n", moje->numer);
  118.                     pisanie = 0;
  119.                
  120.             }
  121.         }
  122.         else
  123.         {
  124.             while(pisanie);
  125.            
  126.                 pisanie = 1;
  127.                 gotoxy(0,moje->numer+2);
  128.                 printf("Auto %d: Jezdze po miescie\n", moje->numer);
  129.                 pisanie = 0;
  130.                
  131.            
  132.         }
  133.     }
  134.     return 0;
  135. }
  136.  
  137. DWORD WINAPI funkcja_myjni(void* argumenty)
  138. {
  139.     int byloauto = 0;
  140.  
  141.     struct dane_dla_watku* dane = (struct dane_dla_watku*)argumenty;
  142.  
  143.     while(pisanie);
  144.  
  145.     pisanie = 1;
  146.     gotoxy(0,0);
  147.     printf("Otworzylismy myjnie w miescie!\n");
  148.     pisanie = 0;
  149.  
  150.     while(1)
  151.     {
  152.         if(dane->i == 1)
  153.         {
  154.             while(pisanie);
  155.  
  156.             pisanie = 1;
  157.             gotoxy(0,0);
  158.             printf("Myjnia: W myjni jest samochod samochod: %d\n", dane->samochodek);
  159.             pisanie = 0;
  160.  
  161.             byloauto = 1;
  162.         }
  163.         if(dane->i == 0)
  164.         {
  165.             while(pisanie);
  166.  
  167.             pisanie = 1;
  168.             gotoxy(0,0);
  169.             printf("Myjnia: Wolne miejsce w myjni\n");
  170.             pisanie = 0;
  171.  
  172.             byloauto = 0;
  173.         }
  174.         Sleep(1000);
  175.     }
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement