the-c-programming-language/dc.c (1485B)
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; // this statement is not practical at all 15 // but was part of the intro to typedef 16 17 dato_t ftoc(dato_t fahrenheit) { 18 return (((fahrenheit - 32)*5)/9); 19 } 20 21 dato_t ctof(dato_t celsius) { 22 return (((celsius * 9)/5) + 32); 23 } 24 25 void title(int i) 26 { 27 printf( 28 "---------------------------------------\n" 29 "---------------------------------------\n" 30 "--- %s TO %s CONVERTER ---\n" 31 "---------------------------------------\n" 32 "--- to exit press Ctrl+D ----\n" 33 "---------------------------------------\n" 34 "---------------------------------------\n", 35 i ? "CELSIUS" : "FAHRENHEIT", 36 i ? "FAHRENHEIT" : "CELSIUS" 37 ); 38 } 39 40 void cursor() { 41 printf(">> "); 42 } 43 44 45 int main(void) 46 { 47 char buffer[MAX_INP_LEN]; 48 escala_t c; 49 dato_t i, res; 50 printf("0 - FAHRENHEIT TO CELSIUS\n"); 51 printf("1 - CELSIUS TO FAHRENHEIT\n"); 52 cursor(); 53 c = getchar() - '0'; 54 if(c == FAHRENHEIT) { 55 title(FAHRENHEIT); 56 while(fgets(buffer, MAX_INP_LEN, stdin) != NULL) { 57 i = atoi(buffer); 58 res = ftoc(i); 59 printf("%.0f°F = %.2f°C\n", i, res); 60 cursor(); 61 } 62 } else if(c == CELSIUS) { 63 title(CELSIUS); 64 while(fgets(buffer, MAX_INP_LEN, stdin) != NULL) { 65 i = atoi(buffer); 66 res = ctof(i); 67 printf("%.0f°C = %.2f°F\n", i, res); 68 cursor(); 69 } 70 } 71 printf("\n"); 72 return 0; 73 } 74 75 76