c-first-steps

a C playground
Index Commits Files Refs README
commit ce4ad8eac071003393907010f3f94fb4ac40e364
parent 51d6dc5d68afec55afa31ba9e4e1fb6bc158b384
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Thu, 29 Oct 2020 20:01:56 -0300

update ftoc.c "fahrenheit to celsius" to dc.c "degree converter"

Diffstat:
Asrc/dc.c | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 80 insertions(+), 0 deletions(-)
diff --git a/src/dc.c b/src/dc.c
@@ -0,0 +1,80 @@
+/*    Ejercicio 4 - Guia 2, Algoritmos y Programacion I - FIUBA    */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+
+typedef enum {
+    FAHRENHEIT,
+    CELSIUS
+} escala_t;
+
+typedef float dato_t;
+
+char s[10];
+
+float ftoc(dato_t fahrenheit) {
+    return (((fahrenheit - 32)*5)/9);
+}
+
+float ctof(dato_t celsius) {
+    return (((celsius * 9)/5) + 32);
+}
+
+void title_ftoc()
+{
+    printf("---------------------------------------\n");
+    printf("---------------------------------------\n");
+    printf("--- FAHRENHEIT TO CELSIUS CONVERTER ---\n");
+    printf("---------------------------------------\n");
+    printf("---      to exit press Ctrl+D      ----\n");
+    printf("---------------------------------------\n");
+    printf("---------------------------------------\n");
+}
+
+void title_ctof()
+{
+    printf("---------------------------------------\n");
+    printf("---------------------------------------\n");
+    printf("--- CELCIUS TO FAHRENHEIT CONVERTER ---\n");
+    printf("---------------------------------------\n");
+    printf("---      to exit press Ctrl+D      ----\n");
+    printf("---------------------------------------\n");
+    printf("---------------------------------------\n");
+}
+
+
+int main(void)
+{
+    escala_t c;
+    printf("0 - FAHRENHEIT TO CELSIUS\n");
+    printf("1 - CELSIUS TO FAHRENHEIT\n");
+    printf(">> ");
+    c = getchar();
+    c -= '0';
+    if(c == FAHRENHEIT) {
+        title_ftoc();
+        dato_t i;
+        float res;
+        while(fgets(s, 10, stdin) != NULL) {
+            i = atoi(s);
+            res = ftoc(i);
+            printf("%.0f°F = %.2f°C\n", i, res);
+            printf(">> ");
+        }
+    } else if(c == CELSIUS) {
+        title_ctof();
+        dato_t i;
+        float res;
+        while(fgets(s, 10, stdin) != NULL) {
+            i = atoi(s);
+            res = ctof(i);
+            printf("%.0f°C = %.2f°F\n", i, res);
+            printf(">> ");
+        }
+    }
+    return 0;
+}
+
+
+