Skip to content

Instantly share code, notes, and snippets.

@skarllot
Created September 14, 2010 13:50
Show Gist options
  • Save skarllot/579057 to your computer and use it in GitHub Desktop.
Save skarllot/579057 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TIPOS_VINHOS 3
#define BUFFER_SIZE 10
struct vinho_t {
char id;
char *name;
int sum;
} vinhos[] = {
{'1', "vinho tinto", 0},
{'2', "vinho branco", 0},
{'3', "vinho rosé", 0}
};
// Total em estoque
int e_sum = 0;
// Obtém o índice em "vinhos" pelo id
int get_index_by_id(char id);
// Imprime estoque
void print_estoque();
int main()
{
int i;
char opc[BUFFER_SIZE] = "?";
while (1) {
for (i = 0; i < TIPOS_VINHOS; i++) {
printf("Digite %c para %s\n", vinhos[i].id, vinhos[i].name);
}
printf("Digite E para estoque\n");
printf("Digite S para sair\n");
printf("Escolha: ");
fgets(opc, BUFFER_SIZE, stdin);
// limpa os caracteres excedentes
if (opc[strlen(opc)-1] != '\n') // se o último caracter não for ENTER
while (getchar() != '\n'); // loop até encontrar ENTER
if (opc[0] == 'e' || opc[0] == 'E') {
print_estoque();
}
else if (opc[0] == 's' || opc[0] == 'S') {
break;
}
else {
i = get_index_by_id(opc[0]);
if (i != -1) {
vinhos[i].sum++;
e_sum++;
printf("\nAdicionado um %s.\n\n", vinhos[i].name);
}
}
printf("\n\n\n");
}
print_estoque();
return 0;
}
int get_index_by_id(char id)
{
int i;
for (i = 0; i < TIPOS_VINHOS; i++) {
if (vinhos[i].id == id)
return i;
}
return -1;
}
void print_estoque()
{
int i;
printf("\n\n\nQuantidade de vinhos no estoque: %d\n", e_sum);
for (i = 0; i < TIPOS_VINHOS; i++) {
printf("Quantidade de %s: %.2f%%\n", vinhos[i].name,
(float)vinhos[i].sum / e_sum * 100.0f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment