1 /* Reads a number of credits from stdin and prints the corresponding 2 grade of the Engineer in stdout*/ 3 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <ctype.h> 7 8 #define MAX_LEN 5 // max digits of the entered degree; 9 #define ERR_MSG_LEN "El numero ingresado contiene demasiados digitos" 10 #define ERR_NEG_MSG "El numero ingresado no es valido" 11 12 #define FST_YR 52 13 #define SND_YR 104 14 #define TRD_YR 156 15 16 #define FST_MSG "Primer año" 17 #define SND_MSG "Segundo año" 18 #define TRD_MSG "Tercer año" 19 #define FTH_MSG "Cuarto año" 20 21 typedef enum { 22 FIRST, SECOND, THIRD, FOURTH 23 } year_t; 24 25 typedef enum { 26 OK, ERROR 27 } status_t; 28 29 status_t clean(char *buffer) 30 { 31 if(buffer == NULL) 32 return ERROR; 33 34 for(size_t i = 0; i < MAX_LEN; i++) 35 buffer[i] = '\0'; 36 37 return OK; 38 } 39 40 41 int main(void) { 42 43 /* Defining variables */ 44 char buffer[MAX_LEN]; 45 int c, credits, i; 46 year_t year; 47 i = 0; 48 49 /* Cleaning buffer in case it contains a random number */ 50 if(clean(buffer) == ERROR) 51 return ERROR; 52 53 /* Reading one char from stdin and storing it on a buffer until EOF */ 54 while(((c = getchar()) != EOF) && c != '\n') { 55 if(i < MAX_LEN) { 56 buffer[i] = c; 57 i++; 58 } else if (i >= MAX_LEN) { 59 fprintf(stderr, ERR_MSG_LEN"\n"); 60 return 1; 61 } 62 } 63 64 /* Converting the first portion of buffer to int with atoi() 65 and cleaning the buffer */ 66 credits = atoi(buffer); 67 if(clean(buffer) == ERROR) 68 return ERROR; 69 70 /* Checks if credits is a valid number */ 71 if(credits < 0) { 72 fprintf(stderr, ERR_NEG_MSG"\n"); 73 return 1; 74 } 75 76 /* Evaluating the numbers of credits to match the current year */ 77 if (credits <= FST_YR) 78 year = FIRST; 79 else if ((credits > FST_YR) && (credits <= SND_YR)) 80 year = SECOND; 81 else if ((credits > SND_YR) && (credits <= TRD_YR)) 82 year = THIRD; 83 else if (credits > TRD_YR) 84 year = FOURTH; 85 86 /* Printing to stdout the corresponding year */ 87 switch (year) 88 { 89 case FIRST: 90 printf(FST_MSG"\n"); 91 break; 92 case SECOND: 93 printf(SND_MSG"\n"); 94 break; 95 case THIRD: 96 printf(TRD_MSG"\n"); 97 break; 98 case FOURTH: 99 printf(FTH_MSG"\n"); 100 break; 101 } 102 return OK; 103 } 104 105 106 107