/* --------------------------------------------------- */ /* Simuladores Capítulo 4 */ /* Editorial Alfaomega - 2009 */ /* --------------------------------------------------- */ /* */ /* Archivo: jcs.js */ /* */ /* Descripción: Funciones Útiles Javascript */ /* */ /* Autor(es): Ariel Calzada */ /* acalzada@alfaomega.com.co */ /* */ /* --------------------------------------------------- */ /* --------------------------------------------------- */ /* Variables */ /* --------------------------------------------------- */ var RELOJ; var NUMERO_LINEAS; var LINEA_ACTUAL; var ULTIMA_LINEA; var VELOCIDAD = 1000; var PGM_I = 0; var PGM_J = 0; var PGM_K = 0; var PGM_L = 0; var PGM_M = 0; var TAM = 3; var CELL_SIZE = 3; var FILAS = TAM; var COLUMNAS = TAM; var ESPACIO = "    "; /* --------------------------------------------------- */ /* changeOperation */ /* --------------------------------------------------- */ /* */ /* Descripción: organiza los elementos para la nueva */ /* operación */ /* */ /* --------------------------------------------------- */ function changeOperation () { var operacion = getOperation (); mostrarCodigo (); limpiarLineas(); limpiarMatrizes(); limpiarVariables(); } /* --------------------------------------------------- */ /* getOperation */ /* --------------------------------------------------- */ /* */ /* Descripción: retorna la operación seleccionada */ /* */ /* --------------------------------------------------- */ function getOperation () { return "transpuesta"; } /* --------------------------------------------------- */ /* adicionarElementoMatriz */ /* --------------------------------------------------- */ /* */ /* Descripción: adiciona un valor a un matriz */ /* */ /* --------------------------------------------------- */ function adicionarElementoMatriz ( matriz, id, posicionF, posicionC ) { var mensaje = "Ingrese número para la " + matriz + " en la posición " + posicionF + ", " + posicionC + ":"; var obj = document.getElementById ( id ); crearPromptAdicionarMatriz ( matriz, mensaje, matriz, id, posicionF, posicionC ); } function crearPromptAdicionarMatriz ( thetitle, thetext, matriz, id, posicionF, posicionC ) { jPrompt ( thetext, '', thetitle, function ( respuesta ) { var obj = document.getElementById ( id + "_" + posicionF + "_" + posicionC ); if ( respuesta ) { if ( validateNumber ( respuesta ) ) { obj.value = respuesta; LINEA_ACTUAL = LINEA_ACTUAL + 1; leerLinea (); } else { alert ( "Número Inválido" ); crearPromptAdicionarMatriz ( thetitle, thetext, matriz, id, posicionF, posicionC ); } } else { finalizarPrograma (); } } ); } /* --------------------------------------------------- */ /* changeLocation */ /* --------------------------------------------------- */ /* */ /* Descripción: cambiar la url actual */ /* */ /* --------------------------------------------------- */ function changeLocation ( url ) { window.location = url; } /* --------------------------------------------------- */ /* validateNumber */ /* --------------------------------------------------- */ /* */ /* Descripción: validar si un valor es un número */ /* entero positivo */ /* */ /* --------------------------------------------------- */ function validateNumber ( n ) { if ( n == "" ) return false; if ( !/^([0-9])*$/.test ( n ) ) return false; return true; } /* --------------------------------------------------- */ /* obtenerCodigo */ /* --------------------------------------------------- */ /* */ /* Descripción: retorna el código asociado a la */ /* operación */ /* */ /* --------------------------------------------------- */ function obtenerCodigo () { var operacion = getOperation (); var codigo; if ( operacion == "transpuesta" ) codigo = obtenerCodigo_transpuesta (); else codigo = new Array (); NUMERO_LINEAS = codigo.length; return codigo; } /* --------------------------------------------------- */ /* mostrarCodigo */ /* --------------------------------------------------- */ /* */ /* Descripción: muestra el código asociado a la */ /* operación */ /* */ /* --------------------------------------------------- */ function mostrarCodigo () { var codigo = obtenerCodigo (); var content = document.getElementById ( "content" ); var i; var nLinea; var linea; var nombreLinea; content.innerHTML = ""; for ( i = 0; i < codigo.length; i++ ) { if ( i + 1 < 10 ) { nLinea = "00" + String ( i + 1 ); } else if ( i + 1 > 9 && i + 1 < 100 ) { nLinea = "0" + String ( i + 1 ); } else nLinea = String ( i + 1 ); nLinea = "
" + nLinea + "
"; nombreLinea = "codeline_" + String ( i + 1 ); linea = nLinea + "
" + codigo [ i ] + "
"; content.innerHTML += linea; } } /* --------------------------------------------------- */ /* resaltarLinea */ /* --------------------------------------------------- */ /* */ /* Descripción: resalta una línea */ /* */ /* --------------------------------------------------- */ function resaltarLinea ( id ) { var obj = document.getElementById ( "codeline_" + String ( id ) ); obj.className = "codeline_highlight"; } /* --------------------------------------------------- */ /* limpiarLinea */ /* --------------------------------------------------- */ /* */ /* Descripción: limpiar una línea */ /* */ /* --------------------------------------------------- */ function limpiarLinea ( id ) { var obj = document.getElementById ( "codeline_" + String ( id ) ); obj.className = "codeline"; } /* --------------------------------------------------- */ /* limpiarLineas */ /* --------------------------------------------------- */ /* */ /* Descripción: limpia el código */ /* */ /* --------------------------------------------------- */ function limpiarLineas () { var obj; var i; for ( i = 0; i < NUMERO_LINEAS; i++ ) limpiarLinea ( i + 1 ); } /* --------------------------------------------------- */ /* limpiarMatrizes */ /* --------------------------------------------------- */ /* */ /* Descripción: limpia los matrizes */ /* */ /* --------------------------------------------------- */ function limpiarMatrizes () { var i, j; var matriz1; var matriz2; var matriz3; for ( i = 0; i < FILAS; i++ ) for ( j = 0; j < COLUMNAS; j++ ) { document.getElementById ( "matriz1" + "_" + i + "_" + j ).value = ""; document.getElementById ( "matriz2" + "_" + i + "_" + j ).value = ""; } } /* --------------------------------------------------- */ /* limpiarVariables */ /* --------------------------------------------------- */ /* */ /* Descripción: limpia variables */ /* */ /* --------------------------------------------------- */ function limpiarVariables () { var PGM_I = 0; var PGM_J = 0; var PGM_K = 0; var PGM_L = 0; var PGM_M = 0; } /* --------------------------------------------------- */ /* bloquearControles */ /* --------------------------------------------------- */ /* */ /* Descripción: bloquea los controles para que no */ /* puedan ser manipulados */ /* */ /* --------------------------------------------------- */ function bloquearControles () { } /* --------------------------------------------------- */ /* desbloquearControles */ /* --------------------------------------------------- */ /* */ /* Descripción: desbloquea los controles para que no */ /* puedan ser manipulados */ /* */ /* --------------------------------------------------- */ function desbloquearControles () { } /* --------------------------------------------------- */ /* iniciarPrograma */ /* --------------------------------------------------- */ /* */ /* Descripción: inicia la ejecución */ /* */ /* --------------------------------------------------- */ function iniciarPrograma () { var boton = document.getElementById ( "boton_realizar" ); LINEA_ACTUAL = 0; ULTIMA_LINEA = 0; PGM_I = 0; PGM_J = 0; PGM_K = 0; PGM_L = 0; boton.value = "Detener"; boton.className = "boton_rojo_menu"; if ( NUMERO_LINEAS > 0 ) { limpiarLineas (); limpiarMatrizes (); limpiarVariables (); bloquearControles (); LINEA_ACTUAL = 1; leerLinea (); } else { jAlert ( "No existe código para ejecutar" ); } } /* --------------------------------------------------- */ /* finalizarPrograma */ /* --------------------------------------------------- */ /* */ /* Descripción: finaliza la ejecución */ /* */ /* --------------------------------------------------- */ function finalizarPrograma () { var boton = document.getElementById ( "boton_realizar" ); jAlert ( "El programa ha finalizado su ejecución" ); clearTimeout ( RELOJ ); boton.value = "Realizar Operación"; boton.className = "boton_menu"; desbloquearControles (); } /* --------------------------------------------------- */ /* pausarPrograma */ /* --------------------------------------------------- */ /* */ /* Descripción: pausa la ejecución */ /* */ /* --------------------------------------------------- */ function pausarPrograma () { clearTimeout ( RELOJ ); } /* --------------------------------------------------- */ /* procesarLinea */ /* --------------------------------------------------- */ /* */ /* Descripción: procesarLinea */ /* */ /* --------------------------------------------------- */ function procesarLinea () { var operacion; var respuesta; ULTIMA_LINEA = LINEA_ACTUAL; operacion = getOperation (); if ( operacion == "transpuesta" ) respuesta = procesarLinea_transpuesta ( LINEA_ACTUAL ); if ( respuesta == -1 ) { return false; } else { LINEA_ACTUAL = respuesta; if ( LINEA_ACTUAL >= NUMERO_LINEAS + 1 ) { finalizarPrograma (); return false; } } return true; } /* --------------------------------------------------- */ /* leerLinea */ /* --------------------------------------------------- */ /* */ /* Descripción: lee una linea del algoritmo */ /* */ /* --------------------------------------------------- */ function leerLinea () { var resultado; if ( ULTIMA_LINEA != 0 ) limpiarLinea ( ULTIMA_LINEA ); resaltarLinea ( LINEA_ACTUAL ); resultado = procesarLinea (); if ( resultado ) RELOJ = setTimeout ( "leerLinea()", VELOCIDAD ); } /* --------------------------------------------------- */ /* procesarBotonRealizar */ /* --------------------------------------------------- */ /* */ /* Descripción: procesa el botón de realizar */ /* */ /* --------------------------------------------------- */ function procesarBotonRealizar () { var boton = document.getElementById ( "boton_realizar" ); if ( boton.value == "Realizar Operación" ) iniciarPrograma (); else finalizarPrograma (); } /* --------------------------------------------------- */ /* dibujarMatriz */ /* --------------------------------------------------- */ /* */ /* Descripción: dibuja una matriz en pantalla */ /* */ /* --------------------------------------------------- */ function dibujarMatriz ( id ) { var i; var j; var nombre; var ctl; document.write ( "" ); for ( i = 0; i < FILAS; i++ ) { document.write ( "" ); for ( j = 0; j < COLUMNAS; j++ ) { nombre = id + "_" + String ( i ) + "_" + String ( j ); ctl = "" document.write ( ctl ); } document.write ( "" ); } document.write ( "
" ); }