Skip to content

Instantly share code, notes, and snippets.

@hugogrochau
Created May 28, 2013 02:09
Show Gist options
  • Save hugogrochau/5660109 to your computer and use it in GitHub Desktop.
Save hugogrochau/5660109 to your computer and use it in GitHub Desktop.
Solução da P2 de brinquedos
#include <stdio.h>
#include <stdlib.h>
#define ARQ_F "FORNECEDORES.TXT"
#define ARQ_B "BRINQUEDOS.TXT"
#define FORNECEDORES 40
#define BRINQUEDOS 300
#define CONSULTAS 10
void preenche_fornecedores(int[]);
void preenche_brinquedos(int[][2]);
int busca(int[][2], int, int);
int main(void) {
int vprazo[FORNECEDORES],
brinquedos[BRINQUEDOS][2],
i, brinquedo;
preenche_fornecedores(vprazo);
preenche_brinquedos(brinquedos);
for (i = 0; i < CONSULTAS; i++) {
int indice;
printf("ID do brinquedo:");
scanf("%d", &brinquedo);
indice = busca(brinquedos, BRINQUEDOS, brinquedo);
if (indice < 0)
printf("Brinquedo %d nao encontrado\n", brinquedo);
else
printf("Prazo de entrega: %d\n", vprazo[brinquedos[indice][1] - 1]);
}
return 0;
}
int busca(int vetor[][2], int tamanho, int chave) {
while (tamanho--) {
if (vetor[tamanho][0] == chave) return tamanho;
}
return -1;
}
void preenche_fornecedores(int vprazo[]) {
int id, prazo;
FILE *fp = fopen(ARQ_F, "r");
if (!fp) {
printf("Erro ao abrir arquivo");
exit(1);
}
while (fscanf(fp, "%d %d", &id, &prazo) != EOF)
vprazo[id-1] = prazo;
fclose(fp);
}
void preenche_brinquedos(int brinquedos[][2]) {
int i;
FILE *fp = fopen(ARQ_B, "r");
if (!fp) {
printf("Erro ao abrir arquivo");
exit(1);
}
for (i = 0; fscanf(fp, "%d %d", &brinquedos[i][0], &brinquedos[i][1]) != EOF; i++);
fclose(fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment