Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace telefoni2
  9. {
  10.     class Program
  11.     {
  12.         static int[] val = new int[5];
  13.         static SemaphoreSlim S = new SemaphoreSlim(1);
  14.         const int postiTot = 200;
  15.         static int postiLiberi = postiTot;
  16.         static int[] stelle = new int[5] { 1, 2, 3, 4, 5 };
  17.  
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             Parallel.Invoke(() => Cassa(1), () => Cassa(2));
  22.  
  23.             Parallel.Invoke(() => Hostess(1), () => Hostess(2));
  24.  
  25.             Console.WriteLine(postiLiberi.ToString());
  26.  
  27.             FindMax();
  28.  
  29.             for(int i = 0; i<5; i++)
  30.             {
  31.                 Console.WriteLine("{0} stelle: {1}", stelle[i], val[i]);
  32.             }
  33.         }
  34.  
  35.         static void Cassa(int numCassa)
  36.         {
  37.             int nb;
  38.             Random r = new Random(numCassa);
  39.  
  40.             for (int i = 0; i < 20; i++)
  41.             {
  42.                 Thread.Sleep(r.Next(10, 15 + 1));
  43.                 nb = r.Next(1, 10 + 1);
  44.                 Console.WriteLine("cassa num: " + numCassa.ToString());
  45.                 Occupa(nb);
  46.             }
  47.             Console.WriteLine("cassa " + numCassa.ToString() + " chiusa\n");
  48.         }
  49.  
  50.         static void Occupa(int quanti)
  51.         {
  52.             S.Wait();
  53.             if (postiLiberi >= quanti)
  54.             {
  55.                 postiLiberi -= quanti;
  56.                 Console.WriteLine("assegnati " + quanti.ToString() + " posti\n");
  57.             }
  58.             else
  59.             {
  60.                 Console.WriteLine("posti non disponibili\n");
  61.             }
  62.             S.Release();
  63.         }
  64.  
  65.         static void Hostess(int numHostess)
  66.         {
  67.             Random r = new Random(numHostess);
  68.             while (postiLiberi <= postiTot)
  69.             {
  70.                 Thread.Sleep(r.Next(10, 15 + 1));
  71.  
  72.                 Console.WriteLine("hostess num: {0}", numHostess);
  73.                 S.Wait();
  74.                 Intervista();
  75.                 S.Release();
  76.             }
  77.  
  78.         }
  79.  
  80.         static void Intervista()
  81.         {
  82.             Random valutazione = new Random();
  83.             int aux = valutazione.Next(1, 5 + 1);
  84.  
  85.             val[aux - 1]++;
  86.  
  87.             postiLiberi++;
  88.         }
  89.  
  90.         static void FindMax()
  91.         {
  92.             int aux;
  93.  
  94.             for (int i = 0; i < 5 - 1; i++)
  95.             {
  96.                 for(int j=i+1; j<5; j++)
  97.                 {
  98.                     if (val[i] < val[j])
  99.                     {
  100.                         aux = val[j];
  101.                         val[j] = val[i];
  102.                         val[i] = aux;
  103.  
  104.                         aux = stelle[j];
  105.                         stelle[j] = stelle[i];
  106.                         stelle[i] = aux;
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement