]> web.ist.utl.pt Git - iaed.git/commitdiff
Add aula04.
authorAlexandre P Francisco <aplf@ist.utl.pt>
Fri, 14 Mar 2014 19:50:21 +0000 (19:50 +0000)
committerAlexandre P Francisco <aplf@ist.utl.pt>
Fri, 14 Mar 2014 19:50:21 +0000 (19:50 +0000)
aula04/Makefile [new file with mode: 0644]
aula04/atletas.c [new file with mode: 0644]
aula04/complexos.c [new file with mode: 0644]
aula04/depositos.c [new file with mode: 0644]
aula04/depositos_in.txt [new file with mode: 0644]

diff --git a/aula04/Makefile b/aula04/Makefile
new file mode 100644 (file)
index 0000000..f2a6b4d
--- /dev/null
@@ -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 (file)
index 0000000..43fc1d5
--- /dev/null
@@ -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 <stdio.h>
+#include <stdlib.h>
+#include <values.h>
+
+#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 (file)
index 0000000..38bf46c
--- /dev/null
@@ -0,0 +1,58 @@
+/**
+ * Exercicios 2 e 3 da aula 4.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+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 (file)
index 0000000..9d45f62
--- /dev/null
@@ -0,0 +1,84 @@
+/**
+ * Exercicios 4, 5, 6, e 7 da aula 4. Testar com:
+ *
+ *   ./depositos < depositos_in.txt
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#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 <nd; i++) {
+    printf("D: %d\t", i+1);
+    mostra_deposito(vd[i]);
+    juros = juro_apos_1_ano(vd[i]);
+    printf("\tJAB: %.2f\tJAL: %.2f\n", juros, juros * (1 - IRS));
+  }
+
+  printf("TANB média: %.2f%%\n", tanb_media(vd, nd)*100);
+  printf("Saldo após um ano: %.2f\n", saldo_apos_1_ano(vd, nd));
+
+  return EXIT_SUCCESS;
+}
+
+deposito le_deposito() {
+  deposito d;
+
+  scanf("%f %f%%", &d.valor, &d.tanb);
+  d.tanb /= 100;
+
+  return d;
+}
+
+void mostra_deposito(deposito d) {
+  printf("Saldo: %.2f\tTANB: %.2f%%", d.valor, d.tanb*100);
+}
+
+float
+juro_apos_1_ano(deposito d) {
+  return d.valor * d.tanb;
+}
+
+float tanb_media(deposito vd[], int n) {
+  int i = 0;
+  float media = 0;
+
+  for (i = 0; i < n; i++)
+    media += vd[i].tanb;
+
+  return media/n;
+}
+
+float saldo_apos_1_ano(deposito vd[], int n) {
+  int i = 0;
+  float saldo = 0;
+
+  for (i = 0; i < n; i++)
+    saldo += vd[i].valor + juro_apos_1_ano(vd[i]) * (1 - IRS);
+
+  return saldo;
+}
diff --git a/aula04/depositos_in.txt b/aula04/depositos_in.txt
new file mode 100644 (file)
index 0000000..c4aa985
--- /dev/null
@@ -0,0 +1,4 @@
+3
+15000 5%
+25000 3.5%
+10500 2%