Share Pastebin
Guest
Public paste!

ShellCoder_

By: a guest | Feb 11th, 2010 | Syntax: C | Size: 7.77 KB | Hits: 22 | Expires: Never
Copy text to clipboard
  1. /*
  2. *       list.c - ShellCoder_
  3. *
  4. *      This program is free software; you can redistribute it and/or modify
  5. *      it under the terms of the GNU General Public License as published by
  6. *      the Free Software Foundation; either version 2 of the License, or
  7. *      (at your option) any later version.
  8. *    
  9. *      This program is distributed in the hope that it will be useful,
  10. *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. *      GNU General Public License for more details.
  13. *    
  14. *      You should have received a copy of the GNU General Public License
  15. *      along with this program; if not, write to the Free Software
  16. *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. *      MA 02110-1301, USA.
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #define FIRST 0
  22. typedef struct {
  23.    char** items;
  24.    int counter;
  25. } LIST;
  26. void Menu(void);
  27. void Init_List(LIST* list);
  28. int Push_to_list(LIST* list, char* item);
  29. char* Read_to_list(LIST* list, int pos);
  30. int Item_Exists(LIST* list, char* item);
  31. char* Pop_to_list(LIST* list, int pos);
  32. void Destroy_List(LIST* list);
  33. LIST prova;
  34. int main(void)
  35. {
  36.    Init_List(&prova);
  37.    int scelta;
  38.    char* elemento = (char*)malloc(99*sizeof(char));
  39.    int posizione;
  40.    while(1==1)
  41.    {
  42.       Menu();
  43.       scanf("%d",&scelta);
  44.       if(scelta==1)
  45.       {
  46.          printf("Elemento da aggiungere: ");
  47.          scanf("%s",elemento);
  48.          printf("\nNumero totale di elementi: %d\n\n",Push_to_list(&prova,elemento));
  49.       }
  50.       else if(scelta==2)
  51.       {
  52.          printf("Posizione elemento da leggere: ");
  53.          scanf("%d",&posizione);
  54.          printf("\nElemento letto: %s\n\n",Read_to_list(&prova,posizione));
  55.       }
  56.       else if(scelta==3)
  57.       {
  58.          printf("Inserire stringa di ricerca: ");
  59.          scanf("%s",elemento);
  60.          int pos = Item_Exists(&prova,elemento);
  61.          if(pos == -1)
  62.          {
  63.             printf("\nL'elemento non esiste.\n\n");
  64.          }
  65.          else
  66.          {
  67.             printf("\nL'elemento esiste alla posizione %d\n\n",pos);
  68.          }
  69.       }
  70.       else if(scelta==4)
  71.       {
  72.          printf("Cancellare (e)elemento o (p)posizione?\n");
  73.          char a;
  74.          scanf("\n%c",&a);
  75.          if((a=='e')||(a=='p'))
  76.          {
  77.             if(a=='e')
  78.             {
  79.                printf("Inserire elemento da cancellare: ");
  80.                scanf("%s",elemento);
  81.                int pos = Item_Exists(&prova,elemento);
  82.                if(pos!= -1)
  83.                {
  84.                   Pop_to_list(&prova,pos);
  85.                   printf("\nL'elemento %s è stato cancellato dalla posizione %d\n\n",elemento,pos);
  86.                }
  87.                else
  88.                {
  89.                   printf("\nL'elemento non esiste!\n\n");
  90.                }
  91.             }
  92.             else
  93.             {
  94.                printf("Inserire la posizione su cui cancellare: ");
  95.                scanf("%d",&posizione);
  96.                char* success = Pop_to_list(&prova,posizione);
  97.                if(success == NULL)
  98.                {
  99.                   printf("\nLa posizione indicata e' inesistente o non valida!\n\n");
  100.                }
  101.                else
  102.                {
  103.                   printf("\nL'elemento %s è stato cancellato dalla posizione %d\n\n",success,posizione);
  104.                }
  105.             }
  106.          }
  107.          else
  108.          {
  109.             printf("\nScelta sbagliata. scegli 'p' o 'e'.\n\n");
  110.          }
  111.       }
  112.       else if(scelta==5)
  113.       {
  114.          printf("Lettura elementi in corso...\n");
  115.          if(prova.counter == -1)
  116.          {
  117.             printf("\nNessun elemento esistente.\n\n");
  118.          }
  119.          else
  120.          {
  121.             int i = 0;
  122.             printf("\n");
  123.             while(i<=prova.counter)
  124.             {
  125.                printf("%s ",Read_to_list(&prova,FIRST+i));
  126.                i++;
  127.             }
  128.             printf("\n\n");
  129.          }
  130.       }
  131.       else if(scelta==6)
  132.       {
  133.          printf("\nGli elementi salvati sono: %d\n\n",prova.counter);
  134.       }
  135.       else if(scelta==7)
  136.       {
  137.          char scelta;
  138.          printf("Sei sicuro di voler deallocare e cancellare la lista? [y/n]\n");
  139.          scanf("\n%c",&scelta);
  140.          if(scelta=='y')
  141.          {
  142.             Destroy_List(&prova);
  143.             printf("Lista deallocata e cancellata con successo. Uscita in corso tra 5 secondi:");
  144.             sleep(1);
  145.             sleep(4);
  146.             exit(0);
  147.          }
  148.       }
  149.       else if(scelta==8)
  150.       {
  151.          exit(0);
  152.       }
  153.       else
  154.       {
  155.          printf("\nScelta non valida\n\n");
  156.       }
  157.    }
  158.    return 0;  
  159. }
  160. void Menu(void)
  161. {
  162.    printf("1) Aggiungere elemento\n");
  163.    printf("2) Leggere elemento\n");
  164.    printf("3) Verificare l'esistenza di un elemento\n");
  165.    printf("4) Cancellare un elemento\n");
  166.    printf("5) Leggere tutti gli elementi in ordine\n");
  167.    printf("6) Contare gli elementi\n");
  168.    printf("7) Dealloca la lista\n");
  169.    printf("8) Esci\n");
  170. }
  171.  
  172.  
  173. /* -------- DA QUI INIZIA IL CODICE DELLE FUNZIONI PER LA GESTIONE DELLE LISTE ---------- */
  174.  
  175. void Init_List(LIST* list){ /* Funzionante */
  176.      list->items = (char**)malloc(1*sizeof(char*));
  177.      list->counter = -1;
  178. }
  179. int Push_to_list(LIST* list, char* item){ /* Funzionante */
  180.    list->counter++;
  181.     list->items[list->counter] = (char*) malloc((strlen(item)+1)*sizeof(char));
  182.     int a = 0;
  183.     while(*item!='\0')
  184.     {
  185.                       list->items[list->counter][a]=*item;
  186.                       item++;
  187.                       a++;
  188.     }
  189.     list->items[list->counter][a] = '\0';
  190.     list->items = realloc(list->items, (list->counter+2) * sizeof(char*));
  191.     return list->counter;
  192. }
  193. char* Read_to_list(LIST* list, int pos) /* Funzionante */
  194. {
  195.    if((pos < 0) || (pos > list->counter))
  196.    {
  197.       return NULL;
  198.    }
  199.    else
  200.    {
  201.       return list->items[pos];
  202.    }
  203. }
  204. int Item_Exists(LIST* list, char* item) /* Funzionante */
  205. {
  206.    int i = 0;
  207.    while(i<=list->counter)
  208.    {
  209.       if(strcmp(list->items[i],item)==0)
  210.       {
  211.          break;
  212.       }
  213.       else
  214.       {
  215.          i++;
  216.       }
  217.    }
  218.    if(i > list->counter)
  219.    {
  220.       return -1;
  221.    }
  222.    else
  223.    {
  224.       return i;
  225.    }
  226. }
  227. char* Pop_to_list(LIST* list, int pos) /* Funzionante */
  228. {
  229.    if((pos < 0) || (pos > list->counter))
  230.    {
  231.       return NULL;
  232.    }
  233.    else
  234.    {
  235.       char* ptr = (char*)malloc(strlen(list->items[pos])+1*sizeof(char));
  236.       int p = 0;
  237.       while(list->items[pos][p]!='\0')
  238.       {
  239.          ptr[p] = list->items[pos][p];
  240.          p++;
  241.       }
  242.       ptr[p] = '\0';
  243.       free(list->items[pos]);
  244.       if(pos == FIRST)
  245.       {
  246.          p = 1;
  247.          while(p <= list->counter)
  248.          {
  249.             list->items[p-1] = list->items[p];
  250.             p++;
  251.          }
  252.       }
  253.       else if(pos == list->counter)
  254.       {
  255.          int i = list->counter - 1;
  256.          p = 0;
  257.          while(p<=i)
  258.          {
  259.             list->items[p] = list->items[p];
  260.             p++;
  261.          }
  262.       }
  263.       else
  264.       {
  265.          p = 0;
  266.          int e = pos;
  267.          while(p<e)
  268.          {
  269.             list->items[p] = list->items[p];
  270.             p++;
  271.          }
  272.          p++;
  273.          list->items[e] = list->items[p];
  274.          p = 1;
  275.          while(pos+p < list->counter)
  276.          {
  277.             list->items[pos+p] = list->items[pos+p+1];
  278.             p++;
  279.          }
  280.       }
  281.       list->counter = list->counter - 1;
  282.       list->items = (char**)realloc(list->items,(list->counter+3-1)*sizeof(char*));
  283.       return ptr;
  284.    }
  285. }
  286. void Destroy_List(LIST* list) /* Funzionante */
  287. {
  288.    int i = FIRST;
  289.    while(i <= list->counter)
  290.    {
  291.       free(list->items[i]);
  292.       i++;
  293.    }
  294.    free(list->items);
  295.    list->counter = -1;
  296. }