9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia02/ex08.c (1116B)
   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <ctype.h>
   4 
   5 #define MAX_LEN 5    // max digits of the entered degree;
   6 #define ERR_MSG_LEN "El angulo ingresado contiene demasiados digitos"
   7 
   8 #define FST_YR 48
   9 #define SND_YR 95
  10 #define TRD_YR 143
  11 
  12 
  13 typedef enum {
  14     FIRST, SECOND, THIRD, FOURTH
  15 } year_t;
  16 
  17 
  18 void clean(char *buffer)
  19 {
  20     for(size_t i = 0; i < MAX_LEN; i++)
  21         buffer[i] = '\0';
  22 }
  23 
  24 
  25 int main(void) {
  26     
  27     char buffer[MAX_LEN];
  28     clean(buffer);
  29     int c, d, i;
  30     year_t e;
  31     i = 0;        
  32     while(((c = getchar()) != EOF) && c != '\n') {
  33         if(i < MAX_LEN) {
  34             buffer[i] = c;
  35             i++;
  36         } else if (i >= MAX_LEN) {
  37             fprintf(stderr, ERR_MSG_LEN"\n");
  38             return 1;
  39         }
  40     }
  41     d = atoi(buffer);
  42 
  43     if (d <= FST_YR) 
  44         e = FIRST;
  45     else if ((d > FST_YR) && (d <= SND_YR))
  46         e = SECOND;
  47     else if ((d > SND_YR) && d <= (TRD_YR))
  48         e = THIRD;
  49     else if (d > TRD_YR) 
  50         e = FOURTH;
  51 
  52 
  53     switch (e)
  54     {
  55         case FIRST:
  56             printf("Primer año\n");
  57             break;
  58         case SECOND:
  59             printf("Segundo año\n");
  60             break;
  61         case THIRD:
  62             printf("Tercer año\n");
  63             break;
  64         case FOURTH:
  65             printf("Curto año o superior\n");
  66             break;
  67     }
  68     return 0;
  69 }
  70 
  71 
  72 
  73