From ce59f39ab39c5a77584c3963768ef2f60446c792 Mon Sep 17 00:00:00 2001 From: Alexandre P Francisco Date: Fri, 14 Mar 2014 19:50:21 +0000 Subject: [PATCH] Add aula04. --- aula04/Makefile | 18 +++++++++ aula04/atletas.c | 61 ++++++++++++++++++++++++++++++ aula04/complexos.c | 58 ++++++++++++++++++++++++++++ aula04/depositos.c | 84 +++++++++++++++++++++++++++++++++++++++++ aula04/depositos_in.txt | 4 ++ 5 files changed, 225 insertions(+) create mode 100644 aula04/Makefile create mode 100644 aula04/atletas.c create mode 100644 aula04/complexos.c create mode 100644 aula04/depositos.c create mode 100644 aula04/depositos_in.txt diff --git a/aula04/Makefile b/aula04/Makefile new file mode 100644 index 0000000..f2a6b4d --- /dev/null +++ b/aula04/Makefile @@ -0,0 +1,18 @@ +CC=gcc +CFLAGS=-Wall -ansi -pedantic + +EXECS=atletas complexos depositos + +all: ${EXECS} + +atletas: atletas.o + gcc ${CFLAGS} -o $@ $< + +complexos: complexos.o + gcc ${CFLAGS} -o $@ $< + +depositos: depositos.o + gcc ${CFLAGS} -o $@ $< + +clean: + rm -f *.o ${EXECS} diff --git a/aula04/atletas.c b/aula04/atletas.c new file mode 100644 index 0000000..43fc1d5 --- /dev/null +++ b/aula04/atletas.c @@ -0,0 +1,61 @@ +/** + * Exercicio 1 da aula 4. Podemos gerar inputs com o comando: + * + * for X in $(seq 1 35); do echo $RANDOM; done > input.txt + * + * Dado um ficheiro input.txt podemos testar com: + * + * ./atletas < input.txt + */ + +#include +#include +#include + +#define DIM1 5 +#define DIM2 7 + +void leMatriz (float tempos[][DIM2], int atletas, int sessoes); +void escreveMediaColunas(float tempos[][DIM2], int atletas, int sessoes); +void escreveMinimoLinhas(float tempos[][DIM2], int atletas, int sessoes); + +int main() { + float tempos[DIM1][DIM2]; + + leMatriz(tempos, DIM1, DIM2); + escreveMediaColunas(tempos, DIM1, DIM2); + escreveMinimoLinhas(tempos, DIM1, DIM2); + + return EXIT_SUCCESS; +} + +void leMatriz (float tempos[][DIM2], int atletas, int sessoes) { + int i, j; + + for (i = 0; i < atletas; i++) + for (j = 0; j < sessoes; j++) + scanf("%f", &tempos[i][j]); +} + +void escreveMediaColunas(float tempos[][DIM2], int atletas, int sessoes) { + int i, j; + float total; + + for (j = 0; j < sessoes; j++) { + for (i = 0, total = 0.0; i < atletas; i++) + total += tempos[i][j]; + printf("Média da sessão %d: %f\n", j, total/sessoes); + } +} + +void escreveMinimoLinhas(float tempos[][DIM2], int atletas, int sessoes) { + int i, j; + float min; + + for (i = 0; i < atletas; i++) { + for (j = 0, min = FLT_MAX; j < sessoes; j++) + if (min > tempos[i][j]) + min = tempos[i][j]; + printf("Mínimo do atleta %d: %f\n", i, min); + } +} diff --git a/aula04/complexos.c b/aula04/complexos.c new file mode 100644 index 0000000..38bf46c --- /dev/null +++ b/aula04/complexos.c @@ -0,0 +1,58 @@ +/** + * Exercicios 2 e 3 da aula 4. + */ + +#include +#include + +typedef struct { + float real, img; +} complexo; + +complexo soma(complexo a, complexo b); +complexo le_complexo(); +void escreve_complexo(complexo a); + +int main() { + complexo x, y, z; + + x = le_complexo(); + y = le_complexo(); + + z = soma(x, y); + + escreve_complexo(x); + printf(" + "); + escreve_complexo(y); + printf(" = "); + escreve_complexo(z); + printf("\n"); + + return EXIT_SUCCESS; +} + +complexo soma(complexo a, complexo b) { + a.real += b.real; + a.img += b.img; + + return a; +} + +complexo le_complexo() { + complexo a; + char sign; + + scanf("%f%c%fi", &a.real, &sign, &a.img); + + if (sign == '-') + a.img *= -1; + + return a; +} + +void escreve_complexo(complexo a) { + if (a.img >= 0) + printf("%f+%fi", a.real, a.img); + else + printf("%f%fi", a.real, a.img); +} diff --git a/aula04/depositos.c b/aula04/depositos.c new file mode 100644 index 0000000..9d45f62 --- /dev/null +++ b/aula04/depositos.c @@ -0,0 +1,84 @@ +/** + * Exercicios 4, 5, 6, e 7 da aula 4. Testar com: + * + * ./depositos < depositos_in.txt + */ + +#include +#include + +#define MAXND 100 +#define IRS 0.21 + +typedef struct { + float valor, tanb; +} deposito; + +deposito le_deposito(); +void mostra_deposito(deposito d); +float juro_apos_1_ano(deposito d); +float tanb_media(deposito vd[], int n); +float saldo_apos_1_ano(deposito vd[], int n); + +int main() { + int nd, i; + deposito vd[MAXND]; + float juros; + + scanf("%d", &nd); + if (nd > MAXND) + nd = MAXND; + + for (i = 0; i < nd; i++) + vd[i] = le_deposito(); + + for (i = 0; i