// /* --------------------------------------------------- */ /* 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 TAM = 3; /* --------------------------------------------------- */ /* changeOperation */ /* --------------------------------------------------- */ /* */ /* Descripción: organiza los elementos para la nueva */ /* operación */ /* */ /* --------------------------------------------------- */ function changeOperation () { var operacion = getOperation (); mostrarCodigo (); limpiarLineas(); limpiarVectores(); limpiarVariables(); } /* --------------------------------------------------- */ /* getOperation */ /* --------------------------------------------------- */ /* */ /* Descripción: retorna la operación seleccionada */ /* */ /* --------------------------------------------------- */ function getOperation () { var operacion = document.getElementById ( "operacion" ); return operacion.options [ operacion.options.selectedIndex ].value; } /* --------------------------------------------------- */ /* adicionarElementoVector */ /* --------------------------------------------------- */ /* */ /* Descripción: adiciona un valor a un vector */ /* */ /* --------------------------------------------------- */ function adicionarElementoVector ( vector, id, posicion ) { var mensaje = "Ingrese número para el " + vector + " en la posición " + posicion + ":"; var obj = document.getElementById ( id ); crearPromptAdicionarVector ( vector, mensaje, vector, id, posicion ); } function crearPromptAdicionarVector ( thetitle, thetext, vector, id, posicion ) { jPrompt ( thetext, '', thetitle, function ( respuesta ) { var obj = document.getElementById ( id ); if ( respuesta ) { if ( validateNumber ( respuesta ) ) { obj.options [ obj.options.length ] = new Option ( respuesta, respuesta ); LINEA_ACTUAL = LINEA_ACTUAL + 1; leerLinea (); } else { alert ( "Número Inválido" ); crearPromptAdicionarVector ( thetitle, thetext, vector, id, posicion ); } } 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 == "suma" ) codigo = obtenerCodigo_suma (); else if ( operacion == "resta" ) codigo = obtenerCodigo_resta (); else if ( operacion == "multiplicacion" ) codigo = obtenerCodigo_multiplicacion (); 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 ); } /* --------------------------------------------------- */ /* limpiarVectores */ /* --------------------------------------------------- */ /* */ /* Descripción: limpia los vectores */ /* */ /* --------------------------------------------------- */ function limpiarVectores () { vector1 = document.getElementById ( "vector1" ); vector2 = document.getElementById ( "vector2" ); vector3 = document.getElementById ( "vector3" ); while ( vector1.options.length != 0 ) vector1.options [ vector1.options.length-1 ] = null; while ( vector2.options.length != 0 ) vector2.options [ vector2.options.length-1 ] = null; while ( vector3.options.length != 0 ) vector3.options [ vector3.options.length-1 ] = null; } /* --------------------------------------------------- */ /* limpiarVariables */ /* --------------------------------------------------- */ /* */ /* Descripción: limpia variables */ /* */ /* --------------------------------------------------- */ function limpiarVariables () { var PGM_I = 0; var PGM_J = 0; var PGM_K = 0; var PGM_L = 0; } /* --------------------------------------------------- */ /* bloquearControles */ /* --------------------------------------------------- */ /* */ /* Descripción: bloquea los controles para que no */ /* puedan ser manipulados */ /* */ /* --------------------------------------------------- */ function bloquearControles () { document.getElementById ( "operacion" ).disabled = true; } /* --------------------------------------------------- */ /* desbloquearControles */ /* --------------------------------------------------- */ /* */ /* Descripción: desbloquea los controles para que no */ /* puedan ser manipulados */ /* */ /* --------------------------------------------------- */ function desbloquearControles () { document.getElementById ( "operacion" ).disabled = false; } /* --------------------------------------------------- */ /* 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 (); limpiarVectores (); 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 == "suma" ) respuesta = procesarLinea_suma ( LINEA_ACTUAL ); else if ( operacion == "resta" ) respuesta = procesarLinea_resta ( LINEA_ACTUAL ); else if ( operacion == "multiplicacion" ) respuesta = procesarLinea_multiplicacion ( 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 (); }