Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace telefoni2
- {
- class Program
- {
- static int[] val = new int[5];
- static SemaphoreSlim S = new SemaphoreSlim(1);
- const int postiTot = 200;
- static int postiLiberi = postiTot;
- static int[] stelle = new int[5] { 1, 2, 3, 4, 5 };
- static void Main(string[] args)
- {
- Parallel.Invoke(() => Cassa(1), () => Cassa(2));
- Parallel.Invoke(() => Hostess(1), () => Hostess(2));
- Console.WriteLine(postiLiberi.ToString());
- FindMax();
- for(int i = 0; i<5; i++)
- {
- Console.WriteLine("{0} stelle: {1}", stelle[i], val[i]);
- }
- }
- static void Cassa(int numCassa)
- {
- int nb;
- Random r = new Random(numCassa);
- for (int i = 0; i < 20; i++)
- {
- Thread.Sleep(r.Next(10, 15 + 1));
- nb = r.Next(1, 10 + 1);
- Console.WriteLine("cassa num: " + numCassa.ToString());
- Occupa(nb);
- }
- Console.WriteLine("cassa " + numCassa.ToString() + " chiusa\n");
- }
- static void Occupa(int quanti)
- {
- S.Wait();
- if (postiLiberi >= quanti)
- {
- postiLiberi -= quanti;
- Console.WriteLine("assegnati " + quanti.ToString() + " posti\n");
- }
- else
- {
- Console.WriteLine("posti non disponibili\n");
- }
- S.Release();
- }
- static void Hostess(int numHostess)
- {
- Random r = new Random(numHostess);
- while (postiLiberi <= postiTot)
- {
- Thread.Sleep(r.Next(10, 15 + 1));
- Console.WriteLine("hostess num: {0}", numHostess);
- S.Wait();
- Intervista();
- S.Release();
- }
- }
- static void Intervista()
- {
- Random valutazione = new Random();
- int aux = valutazione.Next(1, 5 + 1);
- val[aux - 1]++;
- postiLiberi++;
- }
- static void FindMax()
- {
- int aux;
- for (int i = 0; i < 5 - 1; i++)
- {
- for(int j=i+1; j<5; j++)
- {
- if (val[i] < val[j])
- {
- aux = val[j];
- val[j] = val[i];
- val[i] = aux;
- aux = stelle[j];
- stelle[j] = stelle[i];
- stelle[i] = aux;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement