9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia02/ex04.c (1414B)
   1 /*    Ejercicio 4 - Guia 2, Algoritmos y Programacion I - FIUBA    */
   2 
   3 
   4 #include <stdio.h>
   5 #include <stdlib.h>
   6 
   7 #define MAX_INP_LEN 10
   8 
   9 typedef enum {
  10     FAHRENHEIT,
  11     CELSIUS
  12 } escala_t;
  13 
  14 typedef float dato_t;    
  15                     
  16 dato_t ftoc(dato_t fahrenheit)
  17 {
  18     return (((fahrenheit - 32) *5 ) / 9);
  19 }
  20 
  21 dato_t ctof(dato_t celsius)
  22 {
  23     return (((celsius * 9) / 5) + 32);
  24 }
  25 
  26 void title(int i)
  27 {
  28     printf(
  29         "---------------------------------------\n"
  30         "---------------------------------------\n"
  31         "--- %s TO %s CONVERTER ---\n"
  32         "---------------------------------------\n"
  33         "---      to exit press Ctrl+D      ----\n"
  34         "---------------------------------------\n"
  35         "---------------------------------------\n",
  36         i ? "CELSIUS" : "FAHRENHEIT",
  37         i ? "FAHRENHEIT" : "CELSIUS"
  38     );
  39 }
  40 
  41 void cursor()
  42 {
  43     printf(">> ");
  44 }
  45 
  46 
  47 
  48 int main(void)
  49 {
  50     char buffer[MAX_INP_LEN];
  51     escala_t c;
  52     dato_t i, res;
  53     printf("0 - FAHRENHEIT TO CELSIUS\n");
  54     printf("1 - CELSIUS TO FAHRENHEIT\n");
  55     cursor();
  56     c = getchar() - '0';
  57     if (c == FAHRENHEIT) {
  58         title(FAHRENHEIT);
  59         while (fgets(buffer, MAX_INP_LEN, stdin) != NULL) {
  60             i = atoi(buffer);
  61             res = ftoc(i);
  62             printf("%.0f°F = %.2f°C\n", i, res);
  63             cursor();
  64         }
  65     } else if (c == CELSIUS) {
  66         title(CELSIUS);
  67         while (fgets(buffer, MAX_INP_LEN, stdin) != NULL) {
  68             i = atoi(buffer);
  69             res = ctof(i);
  70             printf("%.0f°C = %.2f°F\n", i, res);
  71             cursor();
  72         }
  73     }
  74     printf("\n");
  75     return 0;
  76 }
  77 
  78 
  79