c-first-steps

a C playground
Index Commits Files Refs README
commit a5cea0607e8c18aafc7440febd5cf54e2f428671
parent 6879ea897017265d605e8b32422ff4a54734ae3b
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Fri, 13 Nov 2020 20:07:55 -0300

Added guia02 code

Diffstat:
Asrc/guia02/ej1.c | 16++++++++++++++++
Asrc/guia02/ej2.c | 21+++++++++++++++++++++
Asrc/guia02/ej3.c | 15+++++++++++++++
Asrc/guia02/ej4.c | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/guia02/ej5.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/guia02/ej6.c | 27+++++++++++++++++++++++++++
Asrc/guia02/ej7.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/guia02/ej7_2.c | 42++++++++++++++++++++++++++++++++++++++++++
Asrc/guia02/ej8.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 390 insertions(+), 0 deletions(-)
diff --git a/src/guia02/ej1.c b/src/guia02/ej1.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+typedef enum {
+        FALSE,
+        TRUE
+} bool_t; 
+
+int main(void)
+{
+    bool_t isworking = TRUE;
+    printf("%s\n", isworking ? "TRUE" : "FALSE");
+
+    return 0;
+}
+
+
diff --git a/src/guia02/ej2.c b/src/guia02/ej2.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+typedef enum {
+        ERROR,
+        OK
+} status_t; 
+
+int main(void)
+{
+
+    status_t isworking = OK;
+
+    if (isworking == 1)
+        printf("OK\n");
+    else
+        printf("ERROR\n");
+
+    return 0;
+}
+
+
diff --git a/src/guia02/ej3.c b/src/guia02/ej3.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+typedef enum {
+    LUN, MAR, MIE, JUE, VIE, SAB, DOM
+} dia_t;
+
+int main(void)
+{
+    dia_t dia = VIE;
+
+    printf("%d\n", dia);
+    
+
+    return 0;
+}
diff --git a/src/guia02/ej4.c b/src/guia02/ej4.c
@@ -0,0 +1,79 @@
+/*    Ejercicio 4 - Guia 2, Algoritmos y Programacion I - FIUBA    */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX_INP_LEN 10
+
+typedef enum {
+    FAHRENHEIT,
+    CELSIUS
+} escala_t;
+
+typedef float dato_t;    
+                    
+dato_t ftoc(dato_t fahrenheit)
+{
+    return (((fahrenheit - 32) *5 ) / 9);
+}
+
+dato_t ctof(dato_t celsius)
+{
+    return (((celsius * 9) / 5) + 32);
+}
+
+void title(int i)
+{
+    printf(
+        "---------------------------------------\n"
+        "---------------------------------------\n"
+        "--- %s TO %s CONVERTER ---\n"
+        "---------------------------------------\n"
+        "---      to exit press Ctrl+D      ----\n"
+        "---------------------------------------\n"
+        "---------------------------------------\n",
+        i ? "CELSIUS" : "FAHRENHEIT",
+        i ? "FAHRENHEIT" : "CELSIUS"
+    );
+}
+
+void cursor()
+{
+    printf(">> ");
+}
+
+
+
+int main(void)
+{
+    char buffer[MAX_INP_LEN];
+    escala_t c;
+    dato_t i, res;
+    printf("0 - FAHRENHEIT TO CELSIUS\n");
+    printf("1 - CELSIUS TO FAHRENHEIT\n");
+    cursor();
+    c = getchar() - '0';
+    if (c == FAHRENHEIT) {
+        title(FAHRENHEIT);
+        while (fgets(buffer, MAX_INP_LEN, stdin) != NULL) {
+            i = atoi(buffer);
+            res = ftoc(i);
+            printf("%.0f°F = %.2f°C\n", i, res);
+            cursor();
+        }
+    } else if (c == CELSIUS) {
+        title(CELSIUS);
+        while (fgets(buffer, MAX_INP_LEN, stdin) != NULL) {
+            i = atoi(buffer);
+            res = ctof(i);
+            printf("%.0f°C = %.2f°F\n", i, res);
+            cursor();
+        }
+    }
+    printf("\n");
+    return 0;
+}
+
+
+
diff --git a/src/guia02/ej5.c b/src/guia02/ej5.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+
+typedef enum {
+    JAN, FEB, MAR, APR, MAY, JUN, JUL, AGO, SEP, OCT, NOV, DEC
+} mes_t;
+
+#define JANUARY        "ENERO\n"
+#define FEBRUARY    "FEBRERO\n"
+#define MARCH         "MARZO\n"
+#define APRIL        "ABRIL\n"
+#define MAYO        "MAYO\n"
+#define JUNE        "JUNIO\n"
+#define JULY        "JULIO\n"
+#define AUGUST        "AGOSTO\n"
+#define SEPTEMBER    "SEPTIEMBRE\n"
+#define OCTOBER        "OCTUBRE\n"
+#define NOVEMBER    "NOVIEMBRE\n"
+#define DECEMBER    "DICIEMBRE\n"
+
+int main(void) {
+    mes_t mes = NOV;
+    switch(mes)
+    {
+        case JAN:
+            printf(JANUARY);
+            break;
+        case FEB:
+            printf(FEBRUARY);
+            break;
+        case MAR:
+            printf(MARCH);
+            break;
+        case APR:
+            printf(APRIL);
+            break;
+        case MAY:
+            printf(MAYO);
+            break;
+        case JUN:
+            printf(JUNE);
+            break;
+        case JUL:
+            printf(JULY);
+            break;
+        case AGO:
+            printf(AUGUST);
+            break;
+        case SEP:
+            printf(SEPTEMBER);
+            break;
+        case OCT:
+            printf(OCTOBER);
+            break;
+        case NOV:
+            printf(NOVEMBER);
+            break;
+        case DEC:
+            printf(DECEMBER);
+            break;
+    }    
+    return 0;
+}
diff --git a/src/guia02/ej6.c b/src/guia02/ej6.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <ctype.h>
+
+int main(void)
+{
+    int alpha, nonalpha, num;
+    int c;
+    
+    alpha = nonalpha = num = 0;
+
+    while((c = getchar()) != EOF ) {
+        if(isalpha(c))
+            alpha++;
+        else if(isdigit(c))
+            num++;
+        else if(!isalnum(c))
+            nonalpha++;
+    }
+    printf(
+        "Alphanumerics: %d\n"
+        "NonAlphanumerics: %d\n"
+        "Digits: %d\n",
+        alpha, nonalpha, num
+    );
+
+    return 0;
+}
diff --git a/src/guia02/ej7.c b/src/guia02/ej7.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#define MAX_LEN 5    // max digits of the entered degree;
+#define ERR_MSG_LEN "El angulo ingresado contiene demasiados digitos"
+
+typedef enum {
+    AGUDO, OBTUSO, RECTO
+} angulo_t;
+
+
+void clean(char *buffer)
+{
+    for(size_t i = 0; i < MAX_LEN; i++)
+        buffer[i] = '\0';
+}
+
+
+int main(void) {
+    
+    char buffer[MAX_LEN];
+    clean(buffer);
+    int c, d, e, i;
+    i = 0;        
+    while(((c = getchar()) != EOF) && c != '\n') {
+        if(i < MAX_LEN) {
+            buffer[i] = c;
+            i++;
+        } else if (i >= MAX_LEN) {
+            fprintf(stderr, ERR_MSG_LEN"\n");
+            return 1;
+        }
+    }
+    d = atoi(buffer);
+
+    if (d < 90)
+        e = AGUDO;
+    else if (d == 90)
+        e = RECTO;
+    else if (d > 90)
+        e = OBTUSO;
+
+
+    switch (e)
+    {
+        case AGUDO:
+            printf("El angulo es AGUDO\n");
+            break;
+        case RECTO:
+            printf("El angulo es RECTO\n");
+            break;
+        case OBTUSO:
+            printf("El angulo es OBTUSO\n");
+            break;
+    }
+    return 0;
+}
+
+
+
+
diff --git a/src/guia02/ej7_2.c b/src/guia02/ej7_2.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#define MAX_LEN 5    // max digits of the entered degree;
+#define ERR_MSG_LEN "El angulo ingresado contiene demasiados digitos"
+
+typedef enum {
+    AGUDO, OBTUSO, RECTO
+} angulo_t;
+
+void clean(char *buffer)
+{
+    for(size_t i = 0; i < MAX_LEN; i++)
+        buffer[i] = '\0';
+}
+
+
+int main(void) {
+    
+    char buffer[MAX_LEN];
+    int c, d, i;
+    i = 0;
+
+    while((c = getchar()) != EOF) {
+        if(c != '\n') {
+            if(i < MAX_LEN) {
+                buffer[i] = c;
+                i++;
+            } else if(i > MAX_LEN) {
+                fprintf(stderr, ERR_MSG_LEN"\n");
+                return 1;
+            }
+        }
+    i = 0;
+    d = atoi(buffer);
+    printf("%d\n", d);
+    clean(buffer);
+    }
+
+    return 0;
+}
diff --git a/src/guia02/ej8.c b/src/guia02/ej8.c
@@ -0,0 +1,66 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#define MAX_LEN 5    // max digits of the entered degree;
+#define ERR_MSG_LEN "El angulo ingresado contiene demasiados digitos"
+
+#define FST_YR 48
+#define SND_YR 95
+#define TRD_YR 143
+
+typedef enum {
+    AGUDO, OBTUSO, RECTO
+} angulo_t;
+
+
+void clean(char *buffer)
+{
+    for(size_t i = 0; i < MAX_LEN; i++)
+        buffer[i] = '\0';
+}
+
+
+int main(void) {
+    
+    char buffer[MAX_LEN];
+    clean(buffer);
+    int c, d, e, i;
+    i = 0;        
+    while(((c = getchar()) != EOF) && c != '\n') {
+        if(i < MAX_LEN) {
+            buffer[i] = c;
+            i++;
+        } else if (i >= MAX_LEN) {
+            fprintf(stderr, ERR_MSG_LEN"\n");
+            return 1;
+        }
+    }
+    d = atoi(buffer);
+
+    if (d < FST_YR)
+        e = AGUDO;
+    else if (d > )
+        e = RECTO;
+    else if (d > 90)
+        e = OBTUSO;
+
+
+    switch (e)
+    {
+        case AGUDO:
+            printf("El angulo es AGUDO\n");
+            break;
+        case RECTO:
+            printf("El angulo es RECTO\n");
+            break;
+        case OBTUSO:
+            printf("El angulo es OBTUSO\n");
+            break;
+    }
+    return 0;
+}
+
+
+
+