SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <stdio.h> | |
| 2 | #include <stdlib.h> | |
| 3 | #include <conio.h> | |
| 4 | ||
| 5 | ||
| 6 | typedef struct{
| |
| 7 | ||
| 8 | int codigo; | |
| 9 | char titulo[50]; | |
| 10 | char autor[30]; | |
| 11 | char assunto[10]; | |
| 12 | char editora[20]; | |
| 13 | int ano; | |
| 14 | int edicao; | |
| 15 | }DADOS; | |
| 16 | ||
| 17 | void Registro (DADOS *dados, FILE *arquivo){
| |
| 18 | ||
| 19 | printf("Digite o codigo do livro:\n");
| |
| 20 | scanf("%d",&dados->codigo);
| |
| 21 | fflush(stdin); | |
| 22 | ||
| 23 | printf("Digite o autor:\n");
| |
| 24 | gets(dados->autor); | |
| 25 | fflush(stdin); | |
| 26 | ||
| 27 | printf("Digite o titulo:\n");
| |
| 28 | gets(dados->titulo); | |
| 29 | fflush(stdin); | |
| 30 | ||
| 31 | printf("Digite o assunto:\n");
| |
| 32 | gets(dados->assunto); | |
| 33 | fflush(stdin); | |
| 34 | ||
| 35 | printf("Digite a editora:\n");
| |
| 36 | gets(dados->editora); | |
| 37 | fflush(stdin); | |
| 38 | ||
| 39 | printf("Digite o ano:\n");
| |
| 40 | scanf("%d", &dados->ano);
| |
| 41 | fflush(stdin); | |
| 42 | ||
| 43 | printf("Digite a edicao:\n");
| |
| 44 | scanf("%d", &dados->edicao);
| |
| 45 | fflush(stdin); | |
| 46 | ||
| 47 | fwrite(dados,sizeof(DADOS),1,arquivo); | |
| 48 | } | |
| 49 | ||
| 50 | int Pesquisa (DADOS *dados,FILE *arquivo, char nome){
| |
| 51 | ||
| 52 | int i=0; | |
| 53 | ||
| 54 | while(!feof(arquivo)){
| |
| 55 | ||
| 56 | fread(dados,sizeof(DADOS),1,arquivo); | |
| 57 | if (dados->titulo == nome){
| |
| 58 | ||
| 59 | return(i); | |
| 60 | ||
| 61 | }else{
| |
| 62 | ||
| 63 | return(-1); | |
| 64 | } | |
| 65 | i++; | |
| 66 | } | |
| 67 | } | |
| 68 | ||
| 69 | void Pesquisa2 (DADOS *dados,FILE *arquivo, int ano){
| |
| 70 | ||
| 71 | fread(dados,sizeof(DADOS),1,arquivo); | |
| 72 | while (!feof(arquivo)){
| |
| 73 | ||
| 74 | if(dados->ano == ano){
| |
| 75 | ||
| 76 | printf("O codigo do livro eh:\n %d", dados->codigo);
| |
| 77 | printf("O Titulo do livro eh:\n %s", dados->titulo);
| |
| 78 | printf("O autor eh:\n %s ", dados->autor);
| |
| 79 | printf("O assunto eh:\n %s ", dados->assunto);
| |
| 80 | printf("A editora eh:\n %s", dados->editora);
| |
| 81 | printf("Edicao:\n %d", dados->edicao);
| |
| 82 | ||
| 83 | } | |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | ||
| 88 | int main() | |
| 89 | {
| |
| 90 | FILE *arquivo; | |
| 91 | arquivo = fopen("acervo.cad","w");
| |
| 92 | int ano,opcao; | |
| 93 | DADOS *dados; | |
| 94 | char titulo[50]; | |
| 95 | ||
| 96 | dados = malloc(sizeof(DADOS)); | |
| 97 | ||
| 98 | printf("1-Adicionar livro\n 2-Pesquisar livro\n 3-Imprimir registros anuais\n 4-sair\n");
| |
| 99 | printf("Digite o numero da sua opcao:\n");
| |
| 100 | scanf("%d", &opcao);
| |
| 101 | fflush(stdin); | |
| 102 | ||
| 103 | if(opcao == 1){
| |
| 104 | Registro(dados,arquivo); | |
| 105 | } | |
| 106 | ||
| 107 | if(opcao == 2){
| |
| 108 | ||
| 109 | printf("Digite o titulo");
| |
| 110 | gets(titulo); | |
| 111 | fflush(stdin); | |
| 112 | ||
| 113 | Pesquisa(dados,arquivo,titulo); | |
| 114 | } | |
| 115 | ||
| 116 | if(opcao == 3){
| |
| 117 | ||
| 118 | printf("Digite o ano:");
| |
| 119 | scanf("%d", &ano);
| |
| 120 | fflush(stdin); | |
| 121 | Pesquisa2(dados,arquivo,ano); | |
| 122 | } | |
| 123 | ||
| 124 | if(opcao == 4){
| |
| 125 | exit(0); | |
| 126 | } | |
| 127 | ||
| 128 | free(dados); | |
| 129 | dados = NULL; | |
| 130 | ||
| 131 | fclose(arquivo); | |
| 132 | ||
| 133 | getche(); | |
| 134 | ||
| 135 | } |