// The main function that adds two linked lists represented by head1 and head2. // The sum of two lists is stored in a list referred by result void addList(node* head1, node* head2, node** result) { node *cur; // first list is empty if (head1 == NULL) { *result = head2; return; } // second list is empty else if (head2 == NULL) { *result = head1; return; } int size1 = getSize(head1); int size2 = getSize(head2) ; int carry = 0; // Add same size lists if (size1 == size2) *result = addSameSize(head1, head2, &carry); else { int diff = abs(size1 - size2); // First list should always be larger than second list. // If not, swap pointers if (size1 < size2) swapPointer(&head1, &head2); // move diff. number of nodes in first list for (cur = head1; diff--; cur = cur->next); // get addition of same size lists *result = addSameSize(cur, head2, &carry); // get addition of remaining first list and carry addCarryToRemaining(head1, cur, &carry, result); } // if some carry is still there, add a new node to the fron of // the result list. e.g. 999 and 87 if (carry) push(result, carry); }
//--------------------------------------------------------------------- // Function: main(void) // Title: Main // Description: Main function for the game of life // Programmer: TSZ HIN FUNG // Date: 12/1/2014 // Version: 1.00 // Environment: Intel Xeon PC // Software: MS Windows 7 for execution; // Compiles under Microsoft Visual Studio.Net 2010 // Parameter: void // Input: NONE // Output: Generation with frame // Calls: printGeneration // swapPointer // calcNextGeneration // readlife // Return: EXIT_SUCCESS // History Log: // 12/1/2014 THF completed version 1.0 // -------------------------------------------------------------------- int main(void) { int generation[ROW][COLUMN] = {0}; int nextGeneration[ROW][COLUMN] = {0}; int (*pointerOne)[COLUMN] = generation; int (*pointerTwo)[COLUMN] = nextGeneration; int numberOfGeneration = 0; char token = ' '; readlife(pointerOne); do { system("cls"); printf("Generation: %d\n", numberOfGeneration); numberOfGeneration++; printGeneration(pointerOne); calcNextGeneration(pointerOne, pointerTwo); swapPointer(&pointerOne, &pointerTwo); printf("Press Enter to continue, other keys to quit..."); token = getch(); } while(token == '\r' || token == '\n' ); return EXIT_SUCCESS; }
int main(int argc, char *argv[]){ char nombre[MAX_LONG]; // Nombre de esta Bomba char *nombre_pointer= &nombre[0]; // para poder enviarlo por RPC int capMax; // Capacidad Máxima (Litros) char archivo[MAX_LONG]; // Nombre de archivo "DNS" // Datos de los servidores char* nombres[MAX_SERVERS]; char* direcciones[MAX_SERVERS]; int tiempos[MAX_SERVERS]; //tiempos de respuesta CLIENT *clnts[MAX_SERVERS]; // Validar y obtener argumentos del cliente argumentos_cliente(argc,argv,nombre,&inventario,&consumo,&capMax,archivo); obtener_lista_dns(archivo, nombres,direcciones); // creacion del archivo LOG del cliente char nombre_LOG[MAX_LONG]; sprintf(nombre_LOG,"log_%s.txt",nombre); LOG = fopen(nombre_LOG,"w"); fprintf(LOG,"Inventario inicial %d \n ", inventario); if(inventario == 0) fprintf(LOG,"Tanque vacio: 0 minutos \n"); if(inventario == capMax) fprintf(LOG,"Tanque full: 0 minutos \n"); // PEDIR TIEMPOS int k = 0; while ((direcciones[k]) != NULL){ clnts[k]= clnt_create (direcciones[k], SERVICIOPDVSA, SERVICIOPDVSAVERS, "tcp"); if(clnts[k] == NULL){ clnt_pcreateerror( direcciones[k] ); tiempos[k] = 500; k = k + 1; continue; } int *result = pedir_tiempo_1(NULL,clnts[k]); if ( result == (int *)NULL){ clnt_perror( clnts[k], "Error al conectar con servidor"); tiempos[k] = 500; }else{ tiempos[k]= *result; } k = k + 1; } // ORDENAR EL ARREGLO DE TIEMPOS y TODOS LOS DEMAS int i = 0 ; int minimo; int j; while (nombres[i]!=NULL){ minimo = i; j = i + 1; while (nombres[j]!=NULL){ if (tiempos[j] < tiempos[minimo]){ minimo = j; } j = j +1; } swap(&tiempos[i],&tiempos[minimo]); swapLetras(&nombres[i],&nombres[minimo]); swapLetras(&direcciones[i],&direcciones[minimo]); swapPointer(&clnts[i],&clnts[minimo]); i=i+1; } // Iniciar contador de tiempo pthread_t contador_tiempo; int tiempo = 0; pthread_create(&contador_tiempo,NULL,llevar_tiempo,&tiempo); /**** INICIO DE LA SIMULACION ****/ int r = 0; while (tiempo <= 480){ //Iterar sobre los servidores pidiendo gasolina if(direcciones[r] == NULL){ // Si llegamos al final de la lista, reiniciar. r = 0; usleep(100000); } if ((capMax-inventario)>=38000){ // Verificar si el servidor no respondió al pedir tiempos if (tiempos[r] == 500){ r = r +1; continue; } // Pedir gasolina al servidor num r, almacenar respuesta en buffer gasolina char gasolina[20]; char **result2 = pedir_gasolina_1( &nombre_pointer, clnts[r] ); if ( result2 == (char **)NULL){ clnt_perror( clnts[r], "Error al conectar con servidor"); r = r+1; continue; }else{ strcpy(gasolina,*result2); } // Procesar respuesta del servidor if (strcmp(gasolina,"noDisponible") == 0){ fprintf(LOG,"Peticion: %d minutos, %s , No disponible, %d litros \n", tiempo, nombres[r],inventario); // Pedir gasolina al siguiente servidor en la lista r = r + 1; continue; // si su ticket no esta vigente o no tiene ticket } else if ( strcmp(gasolina,"noTicket") == 0 ){ int *retoInt = pedir_reto_1(NULL,clnts[r]); // convertir el reto de int a string char retoStr[10]; sprintf(&retoStr[0],"%d",*retoInt); // Aplicar el algoritmo MD5 unsigned char respUChar[16]; MDString (&retoStr[0],&respUChar[0]); // Convertir la respuesta a String char respString[33]; int i; for(i = 0; i < 16; ++i) sprintf(&respString[i*2], "%02x", (unsigned int)respUChar[i]); // Para que RPC nos permita pasarlo como argumento char *respStr= (char*) &respString[0]; int *resp = enviar_respuesta_1(&respStr, clnts[r]); if ( *resp == -1){ fprintf(LOG,"Autenticacion Fallida \n"); r = r + 1; continue; }else{ fprintf(LOG,"Autenticacion Correcta\n"); continue; } } else { // El ticket aun sigue vigente . Esperar y recibir gasolina. fprintf(LOG,"Peticion: %d minutos, %s, OK, %d litros \n", tiempo, nombres[r],inventario); usleep((tiempos[r]+1)*100000); pthread_mutex_lock(&mutex); inventario = inventario + 38000; pthread_mutex_unlock(&mutex); fprintf(LOG,"Llegada Gandola: %d minutos, %d litros \n", tiempo,inventario); if (inventario==capMax){ fprintf(LOG,"Tanque Full: %d minutos\n",tiempo);} // Reiniciar la busqueda de servidores activos r = 0; } }// fin del if (que verifica si se necesita gasolina) }// Fin del while (Se acabó el tiempo) fclose(LOG); return 0; }