Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Catalogo de Livraria
- Exemplo de como usar "Arrays with Struct" em C
- Autor: Cooler_
- contato: c00f3r@gmail.com , tony.unix@yahoo.com.br
- webSite: BotecoUnix.com.br
- erro
- catalogo5.c: In function ‘main’:
- catalogo5.c:104: error: invalid operands to binary ^ (have ‘struct livro’ and ‘struct livro’)
- catalogo5.c:104: error: expected ‘)’ before ‘;’ token
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define SWAP(x, y) (x ^= y; y^= x; x^= y)
- #define MAX 100
- void opcao_menu() {
- int i;
- char *banner[] = {
- "-----------------------------------------------------------",
- "Programa Livraria",
- "coded by Cooler_",
- "(1)Listar",
- "(2)Adicionar ",
- "(3)Procurar livro",
- "(4)Remover livro",
- "(5)Mostrar dados em Ordem Alfabética usando QuickSort",
- "(6)Fim",
- "-----------------------------------------------------------",
- };
- for(i=0; i<=9; i++) printf("%s\n",banner[i]);
- }
- int sort_char( const void *a, const void *b) {
- return( strcmp(a,b) );
- }
- struct livro {
- char titulo[MAX];
- char autor[MAX];
- float valor;
- };
- int main() {
- int op=0;
- struct livro livraria[MAX], pon;
- int count=0;
- int x,index;
- char achar[MAX];
- while(op!=6)
- {
- opcao_menu();
- scanf("%d",&op); getchar();
- switch(op)
- {
- case 1:
- printf("Listando livros:\n");
- for (index = 0; index < count; index++)
- printf("%s de %s , valor: $%.2f\n", livraria[index].titulo,
- livraria[index].autor, livraria[index].valor);
- break;
- case 2:
- printf("Digite Titulo do livro.\n");
- fgets(livraria[count].titulo,sizeof(livraria[count].titulo),stdin);
- livraria[count].titulo[strlen(livraria[count].titulo)-1] = '\0';
- printf("Digite o autor.\n");
- fgets(livraria[count].autor,sizeof(livraria[count].autor),stdin);
- livraria[count].autor[strlen(livraria[count].autor)-1] = '\0';
- printf("Digite o valor.\n");
- scanf("%f", &livraria[count++].valor);
- while (getchar() != '\n') continue;
- break;
- case 3:
- printf("Digite Titulo do livro a procurar.\n");
- fgets(achar,sizeof(achar),stdin);
- achar[strlen(achar)-1] = '\0';
- for (index = 0; index < count; index++)
- {
- if(strstr(livraria[index].titulo, achar) != NULL)
- printf("%s de %s , valor: $%.2f\n", livraria[index].titulo,
- livraria[index].autor, livraria[index].valor);
- }
- break;
- case 4:
- x=1;
- printf("Digite Titulo do livro a deletar.\n");
- fgets(achar,sizeof(achar),stdin);
- achar[strlen(achar)-1] = '\0';
- for (index = 0; index < count; index++)
- {
- if(strstr(livraria[index].titulo, achar) != NULL)
- {
- printf("dado a ser removido \n %s de %s , valor: $%.2f\n", livraria[index].titulo,
- livraria[index].autor, livraria[index].valor);
- /*
- pon=livraria[index];
- livraria[index]=livraria[index+1];
- livraria[index+1]=pon;
- */
- SWAP(livraria[index], livraria[index+1]);
- x=0;
- }
- }
- if(!x) count--;
- break;
- case 5:
- printf("Listando livros em Ordem Alfabética:\n");
- qsort((void *)livraria, count, sizeof(livraria[0]), sort_char);
- for (index = 0; index < count; index++)
- printf("%s de %s , valor: $%.2f\n", livraria[index].titulo,
- livraria[index].autor, livraria[index].valor);
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement