Cooler_

testttt

Apr 8th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. // by Cooler agenda exemplo
  5.  
  6. //limite cadastros
  7. #define LIMITE 1024
  8.  
  9. #define BUFFER 32
  10.  
  11. //macro Anti Bug do "new line" comum em compiladores antigos, 1 para ativar 0 para desativar
  12. #define ANTBUG 1
  13.  
  14. struct agenda  {
  15.  char nome[BUFFER];
  16.  int ra;
  17. };
  18.  
  19. struct agenda list[LIMITE],carry;
  20. struct agenda *lista=list;
  21.  
  22. int cmp(const void *a, const void *b)
  23. {
  24. //faz o casting para struct e verifica em ordem decrescente
  25.  struct agenda *ia = (struct agenda *)a;
  26.  struct agenda *ib = (struct agenda *)b;
  27.  return (strcmp(ia->nome,ib->nome));
  28. }
  29.  
  30. void banner()
  31. {
  32.  puts("\ncode agenda by cooler\n digite numero da opção\n 0- exit\n 1 inserir\n 2 remover \n 3 listar \n 4 listar em ordem de nome\n");
  33. }
  34.  
  35. void inserir (int count)
  36. {
  37.  if(count != LIMITE)
  38.  {
  39.   puts("\ndigite nome");
  40.   scanf("%30s",lista[count].nome);
  41. #if ANTBUG
  42.   getchar();
  43. #endif
  44.   puts("\ndigite RA");
  45.   scanf("%6d",&lista[count].ra);
  46. #if ANTBUG
  47.   getchar();
  48. #endif
  49.  }
  50.  else {
  51.   puts("agenda cheia");
  52.  }
  53. }
  54.  
  55. void delete(int count)
  56. {
  57.  int position=0,count3=0,count2=0;
  58.  
  59.  puts("qual RA para remover cadastro ?\n");
  60.  scanf("%6d",&position);
  61. #if ANTBUG
  62.   getchar();
  63. #endif
  64.  
  65.  count3=count;
  66.  
  67.  while(count)
  68.  {
  69.    if(lista[count].ra == position)
  70.    {  
  71.     count2=count;
  72.  
  73.     while(count2<count3)
  74.     {
  75.      carry=lista[count2];
  76.      lista[count2]=lista[count2+1];
  77.      lista[count2+1]=carry;
  78.      count2++;
  79.     }
  80.  
  81.     break;
  82.    }
  83.   count--;
  84.  }
  85.  
  86. }
  87.  
  88. void listar(int count)
  89. {
  90.  while(count)
  91.  {
  92.   fprintf(stdout,"Nome: %s  RA: %d   position: %d\n",lista[count].nome,lista[count].ra,count);
  93.   count--;
  94.  }
  95. }
  96.  
  97. int main()
  98. {
  99.  int escolha=1,count=0;
  100.  
  101.  while(escolha)
  102.  {
  103.   banner();
  104.   scanf("%1d",&escolha);
  105. #if ANTBUG
  106.   getchar();
  107. #endif
  108.   switch(escolha)
  109.   {
  110.    case 1:
  111.     count++;
  112.     inserir(count);
  113.    break;
  114.  
  115.    case 2:
  116.     delete(count);
  117.     count--;
  118.     break;
  119.  
  120.    case 3:
  121.     listar(count);
  122.    break;
  123.  
  124.    case 4:
  125. // quicksort para organizar por nome nossa lista
  126.     qsort(lista,count+1,sizeof(struct agenda),cmp);
  127.     listar(count);
  128.    break;
  129.   }
  130.  }
  131.  
  132.  puts("saindo");
  133.  
  134.  return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment