Skip to content

Instantly share code, notes, and snippets.

@mferoc
Last active May 28, 2021 02:09
Show Gist options
  • Save mferoc/ea78f19c07fdc5f87727c7852e614c11 to your computer and use it in GitHub Desktop.
Save mferoc/ea78f19c07fdc5f87727c7852e614c11 to your computer and use it in GitHub Desktop.
exercise
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHAR_LIMIT 30
typedef struct consumo
{
char nome[CHAR_LIMIT];
float tempo;
float potencia;
} CONSUMO;
void flush(void)
{
int ch;
while ((ch = fgetc(stdin)) != EOF && ch != '\n')
{
}
}
float calcula_consumo(CONSUMO *c, int tamanho, int dias)
{
int i = 0;
float consumo_total = 0.0;
while (i < tamanho)
{
consumo_total += ((c + i)->potencia) * ((c + i)->tempo);
i++;
}
return (consumo_total * dias);
}
int main(void)
{
CONSUMO *c;
int i;
char str_in[30];
float num_in;
int dias;
float consumo_total;
c = (CONSUMO *)malloc(sizeof(c) * 5);
i = 0;
while (i < 5)
{
printf("equipamento %d\n", i);
scanf("%s", str_in);
flush();
memcpy((c + i)->nome, str_in, strlen(str_in) + 1);
scanf("%f", &num_in);
(c + i)->tempo = num_in;
flush();
scanf("%f", &num_in);
(c + i)->potencia = num_in;
flush();
i++;
}
printf("Insira o total de dias de funcionamento dos equipamentos:\n");
scanf("%d", &dias);
consumo_total = calcula_consumo(c, 5, dias);
printf("O total de consumo foi : %.2f\n", consumo_total);
free(c);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment