Advertisement
Dany1858

Esercizio pile con array

Dec 29th, 2014
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LUN 20
  4.  
  5. void pushCoda(int *, int *, int *, int *);
  6. void popCoda(int *, int *, int *);
  7. void printAll(int, int, int *);
  8. int isFullorEmpty(int, int);
  9.  
  10.      /*main!*/
  11. main()
  12. {
  13.       int coda[LUN];
  14.       int puntTesta=0, puntCoda=0;
  15.       int menu=-1, num=0;
  16.      
  17.       while(menu!=0){
  18.         if(num==99) num=1;
  19.         printAll(puntTesta, puntCoda, coda);
  20.         menu=menuLista();
  21.  
  22.         switch(menu){
  23.            case 1: {
  24.               pushCoda(&puntTesta, &puntCoda, coda, &num); break;}
  25.            case 2: {
  26.               popCoda(&puntTesta, &puntCoda, coda); break;}
  27.            
  28.            case 4: {puntTesta=0; puntCoda=0; num=0;
  29.                    printf("\n\tCoda svuotata!!"); getchar(); break;}
  30.            case 5: {menu=0; break;}
  31.            default:{
  32.               printf("opzione non ammessa");
  33.               printf("\n\n\nIn attesa...\t");
  34.               getchar();}
  35.            }  
  36.       system("cls"); }
  37.       return 0;
  38. }
  39.  
  40.                    /*menu'*/
  41. int menuLista()
  42. {
  43.     int menu;
  44.     printf("\n------------- MENU'------------\n");
  45.     printf("\n1. Prego prendere numerino\n\n2. Avanti il prossimo\n\n4. Svuota coda\n\n5. Exit\n\n ");
  46.     scanf("%d", &menu);
  47.     getchar();
  48.     return menu;
  49. }
  50.  
  51.            /*inserisci elemento*/
  52. void pushCoda(int *t, int *c, int *coda, int *num)
  53. {
  54.      int ele;
  55.      if(isFullorEmpty(*t, (*c)+1)==-1) {printf("\nCoda piena!!\t"); getchar();}
  56.      else {
  57.           system("cls");
  58.           (*num)++;
  59.           printAll(*t, *c, coda);
  60.           printf("\nIl tuo numero e': %d\t", *num);
  61.           *c=*c%LUN; coda[*c]=*num; ++*c;
  62.           getchar();}
  63. }
  64.  
  65.           /*togli il primo elemento in coda*/
  66. void popCoda(int *t, int *c, int *coda)
  67. {
  68.      int size;
  69.      system("cls");
  70.      size=isFullorEmpty(*t, *c);
  71.      if(size<=0) {printf("\nCoda vuota!!\t"); getchar();}
  72.      else {
  73.           *t=*t%LUN;
  74.           printAll((*t)+1, *c, coda);
  75.           printf("\nServiamo il: %d", coda[*t]);
  76.           (*t)++;
  77.           getchar();}
  78. }
  79.  
  80.                      /*stampa la coda*/
  81. void printAll(int t, int c, int *coda)
  82. {
  83.      int i, size, numPers;
  84.      size=isFullorEmpty(t, c);
  85.      if(size==-1 || size==0) numPers=0;
  86.      else numPers=size;
  87.      printf("\nPersone in coda: %d\n\t\t-------------------> Il prossimo", numPers);
  88.      for(i=t; i!=c;){ i=i%(LUN); printf("\n\t|\t%d\t|", coda[i]); i++;}
  89.      size=LUN-(size+1);
  90.      for(i=0; i<size; i++) printf("\n\t|\t\t|");
  91.      printf("\n\t\t<------------------- Ultimo arrivato\n");
  92. }
  93.  
  94.            /*Funzione ausiliare che controlla se la memoria è piena o vuota,
  95.            *restituisce anche il numero di elementi presenti in coda*/
  96. int isFullorEmpty(int t, int c)
  97. {
  98.     int size;
  99.     if(c==0 && t==0) return 0;
  100.     size=(c - t + LUN) % LUN;
  101.     if(size==0) return -1;
  102.     return size;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement