Semana 9

Esta semana vamos a realizar los ejercicios propuestos en la guía de hilos.

Material

La guía de trabajo para el tema de hilos está aquí. Al final de la guía hay ejercicios tipos examen llamados alimento para el pensamiento.

Dejo como referencia una posible solución a la evaluación sumativa 1:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

 #define COMMANDMAXSIZE 50
 #define NAMEMAXSIZE 30
 char commandBuffer[COMMANDMAXSIZE + 1];

 const char makeDatabase[] = "mkdb";
 const char loadDatabase[] = "loaddb";
 const char saveDatabase[] = "savedb";
 const char readAllRegisters[] = "readall";
 const char readDbSize[] = "readsize";
 const char readDbCapacity[] = "readcapacity";
 const char makeRegister[] = "mkreg";
 const char readRegister[] = "readreg";
 const char quit[] = "exit";

 int makeDatabasefn(int);
 int loadDatabasefn(char *);
 void saveDatabasefn(char *);
 void readAllRegistersfn(void);
 int readsizefn(void);
 int makeRegisterfn(int, char *, int );
 int readRegisterfn(int);
 void quitfn(void);

 struct estudiante
 {
     int cedula;
     char nombre[30];
     int semestre;
 };

 char currentDataBaseName[50];
 int currentDataBaseSize = 0;
 struct estudiante *pcurrentDataBase;
 int currentDataBaseRegister = 0;

 int main()
 {
     while(1){
         printf(">");
         if ( fgets(commandBuffer, COMMANDMAXSIZE + 1,stdin) != NULL){

             int commandSize = strlen(commandBuffer);

             if(commandBuffer[commandSize - 1] != '\n'){
                 printf("Error: command too long \n");
                 return EXIT_FAILURE;
             }
             else{
             commandBuffer[commandSize - 1] = 0;
             }

             if( 0 == strncmp(makeDatabase, commandBuffer, strlen(makeDatabase) ) ){
                 char* token = strchr(commandBuffer, ' ');
                 if(token != NULL){
                     int result = sscanf(token + 1, "%s %d", currentDataBaseName, &currentDataBaseSize);
                     if(result != 2){
                     currentDataBaseName[0] = 0;
                     currentDataBaseSize = 0;
                     printf("Enter a valid arguments\n");
                     }
                     else{
                         if ( makeDatabasefn(currentDataBaseSize) != 1){
                             printf("Database can't be created\n");
                         }
                     }
                 }
                 else{
                     printf("Enter a valid arguments\n");
                 }
             }
             else if ( 0 == strncmp(readAllRegisters, commandBuffer, strlen(readAllRegisters) ) ){
                 readAllRegistersfn();
             }
             else if ( 0 == strncmp(makeRegister, commandBuffer, strlen(makeRegister) ) ){
                 char* token = strchr(commandBuffer, ' ');
                 char name[50];
                 int cedula;
                 int semestre;

                 if(token != NULL){
                     int result = sscanf(token + 1, "%d %s %d", &cedula, name,&semestre);
                     if(result != 3){
                     printf("Enter a valid register arguments\n");
                     }
                     else{
                         if( makeRegisterfn(cedula, name, semestre) != 1){
                             printf("Data base is full, register was not created\n");
                         }
                     }
                 }
             }
             else if( 0 == strncmp(saveDatabase, commandBuffer, strlen(saveDatabase) ) ){
                 if(currentDataBaseName[0] == 0){
                     printf("Load a data base first\n");
                     continue;
                 }
                 char* token = strchr(commandBuffer, ' ');
                 if(token != NULL){
                     int result = sscanf(token + 1, "%s", currentDataBaseName);
                     if(result != 1){
                     printf("Enter a name\n");
                     }
                     else{
                         saveDatabasefn(currentDataBaseName);
                     }
                 }
                 else{
                     printf("Enter a name\n");
                 }
             }
             else if( 0 == strncmp(quit, commandBuffer, strlen(quit) ) ){
                 if(currentDataBaseName[0] == 0){
                     printf("No active db\n");
                     continue;
                 }
                 printf("save data base with name %s? y/n: ",currentDataBaseName);
                 if (fgets(commandBuffer, COMMANDMAXSIZE + 1,stdin) != NULL){

                 int commandSize = strlen(commandBuffer);
                 commandBuffer[commandSize - 1] = 0;
                 if( 0 == strncmp("y", commandBuffer, 1) ){
                     saveDatabasefn(currentDataBaseName);
                 }
                 }
                 return EXIT_SUCCESS;
             }
             else if( 0 == strncmp(loadDatabase, commandBuffer, strlen(loadDatabase) ) ){
                 char* token = strchr(commandBuffer, ' ');
                 char name[50];

                 if(token != NULL){
                     int result = sscanf(token + 1, "%s",name);
                     if(result != 1){
                     printf("Enter a data base name\n");
                     }
                     else{
                         if( loadDatabasefn(name) == 0){
                             printf("Can't load de database\n");
                         }
                         else{
                             strncpy(currentDataBaseName, name, 50);
                         }
                     }
                 }
                 else{
                     printf("Enter a database name\n");
                 }
             }
             else if( 0 == strncmp(readDbSize, commandBuffer, strlen(readDbSize) ) ){
                 printf("%d\n",currentDataBaseRegister);
             }
             else if( 0 == strncmp(readDbCapacity, commandBuffer, strlen(readDbCapacity) ) ){
                 printf("%d\n",currentDataBaseSize);
             }
             else if( 0 == strncmp(readRegister, commandBuffer, strlen(readRegister) ) ){
                 char* token = strchr(commandBuffer, ' ');
                 int cedula;

                 if(token != NULL){
                     int result = sscanf(token + 1, "%d",&cedula);
                     if(result != 1){
                     printf("Enter a numeric id number\n");
                     }
                     else{
                         if( readRegisterfn(cedula)  == 0){
                             printf("Can't find the id number in the database\n");
                         }
                     }
                 }
                 else{
                     printf("Enter a id number\n");
                 }
             }
             else{
                 printf("Not valid command\n");
             }
         }
         else{
             perror("Error: ");
             return EXIT_FAILURE;
         }
     }
     return EXIT_SUCCESS;
 }


 int makeDatabasefn(int size){
     int success = 0;
     pcurrentDataBase = (struct estudiante *) malloc( sizeof(struct estudiante)*size );
     if(pcurrentDataBase != NULL) success = 1;
     return success;
 }

 void readAllRegistersfn(void){
     for(int i = 0; i < currentDataBaseRegister; i++){
         printf("registro %d cedula: %d, nombre: %s, semestre: %d\n", i+1, (pcurrentDataBase + i)->cedula,(pcurrentDataBase + i)->nombre, (pcurrentDataBase + i)->semestre);
     }
 }

 int makeRegisterfn(int cedula, char * nombre, int semestre){
     int result = 0;
     if(currentDataBaseRegister < currentDataBaseSize){
         (pcurrentDataBase+ currentDataBaseRegister)->cedula = cedula;
         strncpy( (pcurrentDataBase+ currentDataBaseRegister) ->nombre, nombre, NAMEMAXSIZE);
         (pcurrentDataBase+ currentDataBaseRegister)->semestre = semestre;
         currentDataBaseRegister++;
         result = 1;
     }
     return result;
 }

 void saveDatabasefn(char * name){
     FILE *fp = fopen(name, "w+");
     fprintf(fp,"%d\n",currentDataBaseSize);
     for(int i = 0; i < currentDataBaseRegister; i++){
         fprintf(fp, "%d %s %d\n", (pcurrentDataBase + i)->cedula,(pcurrentDataBase + i)->nombre, (pcurrentDataBase + i)->semestre);
     }
     fclose(fp);
 }


 int loadDatabasefn(char * dataBaseFileName){
     int currentDataBaseSizeTmp = 0;
     struct estudiante *pcurrentDataBaseTmp;
     int currentDataBaseRegisterTmp = 0;

     int result = 0;
     FILE *fp = fopen(dataBaseFileName, "r");
     if(fp == NULL){
         perror("Error: ");
     }
     else{
         int scanfStatus = fscanf(fp,"%d", &currentDataBaseSizeTmp);
         if(feof(fp) == 0){
             if(scanfStatus == 1){
                 pcurrentDataBaseTmp = (struct estudiante *) malloc( sizeof(struct estudiante)*currentDataBaseSizeTmp );
                 if(pcurrentDataBaseTmp != NULL){
                     while(1){
                         int scanfStatus = fscanf(fp,"%d %s %d", &((pcurrentDataBaseTmp + currentDataBaseRegisterTmp)->cedula), (pcurrentDataBaseTmp + currentDataBaseRegisterTmp)->nombre, &((pcurrentDataBaseTmp + currentDataBaseRegisterTmp)->semestre) );
                         if(feof(fp) == 0){
                             if(scanfStatus != 3){
                                 free(pcurrentDataBaseTmp);
                                 break;
                             }
                             else{
                                 currentDataBaseRegisterTmp++;
                             }
                         }
                         else{
                             result = 1;
                             pcurrentDataBase = pcurrentDataBaseTmp;
                             currentDataBaseSize = currentDataBaseSizeTmp;
                             currentDataBaseRegister = currentDataBaseRegisterTmp;
                             break;
                         }
                     }
                 }
             }
             else{
                 printf("Can't read database size\n");
             }
         }
         else{
             printf("Can't read database\n");
         }

         fclose(fp);
     }
     return result;
 }


 int readRegisterfn(int id){
     int result = 0;

     for(int i = 0; i < currentDataBaseRegister;i++){
         if( (pcurrentDataBase + i)->cedula == id ){
             result = 1;
             printf("registro %d cedula: %d, nombre: %s, semestre: %d\n", i+1, (pcurrentDataBase + i)->cedula,(pcurrentDataBase + i)->nombre, (pcurrentDataBase + i)->semestre);
             break;
         }
     }
     return result;
 }