Advertisement
HwapX

Desafio Programa Caixa

Nov 1st, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | None | 0 0
  1. // http://www.forum-invaders.com.br/vb/showthread.php/40655-Desafio-C-Programa-Caixa
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct
  7. {
  8.     int size;
  9.     int count;
  10.     int *items;
  11. } box;
  12.  
  13. void swap(int *a, int *b)
  14. {
  15.     int t = (*a);
  16.  
  17.     (*a) = (*b);
  18.     (*b) = t;
  19. }
  20.  
  21. int box_add(box *thebox, int item)
  22. {
  23.     if(thebox->count < thebox->size)
  24.     {
  25.         thebox->items[thebox->count] = item;
  26.         return(thebox->count++);
  27.     }
  28.     return(-1);
  29. }
  30.  
  31. int box_remove(box *thebox, int pos)
  32. {
  33.     if((pos >= 0) && (pos < thebox->count))
  34.     {
  35.         thebox->items[pos] = 0;
  36.         thebox->count--;
  37.         int i = pos;
  38.  
  39.         while(i < thebox->count)
  40.         {
  41.             i++;
  42.             if(thebox->items[i])
  43.             {
  44.                 swap(&thebox->items[i], &thebox->items[i-1]);
  45.             }
  46.             else
  47.             {
  48.                 break;
  49.             }
  50.         }
  51.  
  52.         return(1);
  53.     }
  54.     else
  55.     {
  56.         return(0);
  57.     }
  58. }
  59.  
  60. int box_find(box *thebox, int item)
  61. {
  62.     int i;
  63.  
  64.     for(i = 0; i < thebox->count; i++)
  65.         if(thebox->items[i] == item)
  66.             return i;
  67.  
  68.     return -1;
  69. }
  70.  
  71. void box_show(box *thebox)
  72. {
  73.     int i;
  74.  
  75.     for(i = 0; i < thebox->size; i++)
  76.         printf("[%d]", thebox->items[i]);
  77.     printf("\n");
  78. }
  79.  
  80. void menu(box *thebox)
  81. {
  82.     int value = 0;
  83.     char option = 0;
  84.  
  85.     while(option != 's')
  86.     {
  87.         option = 0;
  88.         printf("\nSelecione a opcao desejada\n[a]Adicionar\n[r]Remover\n[p]Procurar\n[m]Mostrar\n[s]Sair\nOpcao: ");
  89.         scanf("%*[^\n]");
  90.         scanf("%*[^arpms]%c", &option);
  91.         scanf("%*[^\n]");
  92.         switch(option)
  93.         {
  94.         case 'a':
  95.             printf("Digite o valor que deseja adicionar: ");
  96.             //scanf("");
  97.             scanf("%*[^1-9]%d", &value);
  98.             value = box_add(thebox, value);
  99.             if(value >= 0)
  100.             {
  101.                 printf("Adicionado na posicao: %d\n", value);
  102.             }
  103.             else
  104.             {
  105.                 printf("A caixa esta cheia.\n");
  106.             }
  107.             break;
  108.         case 'r':
  109.             printf("Digite a posicao que deseja remover: ");
  110.             scanf("%*[^0-9]%d", &value);
  111.             value = box_remove(thebox, value);
  112.             if(value)
  113.             {
  114.                 printf("Removida!\n");
  115.             }
  116.             else
  117.             {
  118.                 printf("Posicao invalida!\n");
  119.             }
  120.             break;
  121.         case 'p':
  122.             printf("Digite o valor que deseja procurar: ");
  123.             scanf("%*[^1-9]%d", &value);
  124.             value = box_find(thebox, value);
  125.             if(value >= 0)
  126.             {
  127.                 printf("Encontrado na posicao: %d\n", value);
  128.             }
  129.             else
  130.             {
  131.                 printf("O valor nao existe na caixa.\n");
  132.             }
  133.             break;
  134.         case 'm':
  135.             box_show(thebox);
  136.             break;
  137.         case 's':
  138.             break;
  139.         default:
  140.             printf("Opcao invalida!\n");
  141.         }
  142.     }
  143. }
  144.  
  145. int main()
  146. {
  147.     box mybox = {0, 0, NULL};
  148.  
  149.     while(mybox.size < 1)
  150.     {
  151.         printf("Informe o tamanho da caixa: ");
  152.         scanf("%*[^1-9]");
  153.         scanf("%d", &mybox.size);
  154.     }
  155.  
  156.     mybox.items = calloc(mybox.size, sizeof(int));
  157.  
  158.     menu(&mybox);
  159.  
  160.     free(mybox.items);
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement