Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // by Cooler agenda exemplo
- //limite cadastros
- #define LIMITE 1024
- #define BUFFER 32
- //macro Anti Bug do "new line" comum em compiladores antigos, 1 para ativar 0 para desativar
- #define ANTBUG 1
- struct agenda {
- char nome[BUFFER];
- int ra;
- };
- struct agenda list[LIMITE],carry;
- struct agenda *lista=list;
- int cmp(const void *a, const void *b)
- {
- //faz o casting para struct e verifica em ordem decrescente
- struct agenda *ia = (struct agenda *)a;
- struct agenda *ib = (struct agenda *)b;
- return (strcmp(ia->nome,ib->nome));
- }
- void banner()
- {
- 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");
- }
- void inserir (int count)
- {
- if(count != LIMITE)
- {
- puts("\ndigite nome");
- scanf("%32s",lista[count].nome);
- #if ANTBUG
- getchar();
- #endif
- puts("\ndigite RA");
- scanf("%6d",&lista[count].ra);
- #if ANTBUG
- getchar();
- #endif
- }
- else {
- puts("agenda cheia");
- }
- }
- void delete(int count)
- {
- int position=0,count3=0,count2=0;
- puts("qual RA para remover cadastro ?\n");
- scanf("%6d",&position);
- #if ANTBUG
- getchar();
- #endif
- count3=count;
- while(count)
- {
- if(lista[count].ra == position)
- {
- count2=count;
- while(count2<count3)
- {
- carry=lista[count2];
- lista[count2]=lista[count2+1];
- lista[count2+1]=carry;
- count2++;
- }
- break;
- }
- count--;
- }
- }
- void listar(int count)
- {
- while(count)
- {
- fprintf(stdout,"Nome: %s RA: %d position: %d\n",lista[count].nome,lista[count].ra,count);
- count--;
- }
- }
- int main()
- {
- int escolha=1,count=0;
- while(escolha)
- {
- banner();
- scanf("%6d",&escolha);
- #if ANTBUG
- getchar();
- #endif
- switch(escolha)
- {
- case 1:
- count++;
- inserir(count);
- break;
- case 2:
- delete(count);
- count--;
- break;
- case 3:
- listar(count);
- break;
- case 4:
- // quicksort para organizar por nome nossa lista
- qsort(lista,count+1,sizeof(struct agenda),cmp);
- listar(count);
- break;
- }
- }
- puts("saindo");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment