jueves, marzo 29, 2007

Buscar con pageup en Bash

Esta era de mis funciones favoritas del bash en Mandrake, pero poco a poco se ha ido perdiendo entre las diferentes distribuciones de linux. Ahora uso Ubuntu y tampoco lo tiene, así que buscando por la red por fin supe donde había que poner los aliases. Lo copio aquí que seguro lo necesitaré después.

~/.inputrc
"\e[1~": beginning-of-line
"\e[2~": yank
"\e[3~": delete-char
"\e[4~": end-of-line
"\e[5~": history-search-backward
"\e[6~": history-search-forward
$if term=xterm
"\e[2;5~": yank
"\e[3;5~": delete-char
"\e[5;5~": history-search-backward
"\e[6;5~": history-search-forward
$endif

sábado, marzo 17, 2007

Javascript en elementos html con Studio Creator

Es común utilizar Javascript para muchas operaciones en el cliente.

Por ejemplo, queremos que nos calcule una multiplicación entre precio unitario (p.u.) y cantidad (cant) y que ponga el resultado en total durante el proceso de captura.



Para poder hacerlo, tengo que saber los nombres con los que Studio Creator creará los componentes html de la forma. Puedo ver el "preview" con el botón de arriba en el editor.


Luego pido ver la fuente, localizo mis componentes y apunto los nombres.


El resultado cambia cuando cualquiera de los operandos se modifica, entonces escuchamos los dos inputs involucrados. Precio unitario y cantidad.

Según el preview, el input de precio unitario lo llamará form1:tabSet1:tab5:tf_pu, cantidad form1:tabSet1:tab5:tf_cantidad y total form1:tabSet1:tab5:tf_total el Javascript es muy sencillo:
document.getElementById('form1:tabSet1:tab5:tf_total').value =
document.getElementById('form1:tabSet1:tab5:tf_pu').value *
document.getElementById('form1:tabSet1:tab5:tf_cantidad').value;

Hay que insertarlo en los dos inputs bajo el evento onChange. Volvemos a ver el preview para confirmar los cambios en los dos inputs.

<td><input class="TxtFld" id="form1:tabSet1:tab5:tf_pu" name="form1:tabSet1:tab5:tf_pu" onchange=" document.getElementById('form1:tabSet1:tab5:tf_total').value =
document.getElementById('form1:tabSet1:tab5:tf_pu').value *
document.getElementById('form1:tabSet1:tab5:tf_cantidad').value;" size="15" type="text" value="0.0" /></td>
<td><input class="TxtFld" id="form1:tabSet1:tab5:tf_cantidad" name="form1:tabSet1:tab5:tf_cantidad" onchange=" document.getElementById('form1:tabSet1:tab5:tf_total').value =
document.getElementById('form1:tabSet1:tab5:tf_pu').value *
document.getElementById('form1:tabSet1:tab5:tf_cantidad').value;" size="15" type="text" value="0.0" /></td>
<td><input class="TxtFld" id="form1:tabSet1:tab5:tf_total" name="form1:tabSet1:tab5:tf_total" size="15" type="text" value="0.0" /></td>


Listo, quedó agregado el código Javascript y todo lo hicimos dentro del IDE.

Una solución más elegante sería crear una funcion que haga la operación y sólo llamar a la funcion cuando se active el evento.

viernes, marzo 02, 2007

FizzFuzz

Mi solución al FizzFuzz en C (la de Java es muy parecida):

/*
* Solucion FizzFuzz
* Juparave
* 2 de marzo 2007
*/
#include <stdio.h>

int main(int argc, char** argv){
int i;

for (i = 0; i < 100; i++) {
if( !(i % 3) )
printf("fizz");
if( !(i % 5) )
printf("fuzz");
if( (i % 3) * (i % 5) )
printf("%d", i);
printf("\n");
}
}

El FizzFuzz no es mas que listar números del 1 al 100, pero cuando este número sea múltiplo de 3 en lugar imprimir fizz y cuando sea multiplo de 5 imprimir fuzz. Cuando sea múltiplo de 3 y de 5 imprimir fizzfuzz. Parece un problema sencillo y cualquiera que se jacte de ser programador debe de tardar menos de 2 minutos en resolverlo... sin comentarios

Repaso de diagramas de clase

UML es la mejor forma de comunicar ideas y conceptos entre un equipo de programación. Es muy fácil de usar una vez que se tiene clara la simbología.

Los diagramas de clase los usamos para describir las clases de un sistema y sus relaciones entre ellas.

En este diagrama se muestra a la Clase 1, que hereda características de la Clase A e implementa operaciones de la interfaz Q

La Clase 2 instancia un objeto de la Clase 1 llamado atributo3, a esto se le llama composición (composition)

Esto en Java quedaría así:
/**
 * Class Clase_1
 * 
 */
public class Clase_1 extends Clase_A implements Interfaz_Q {
// Fields
// 
private int atributo1;
// 
private int atributo2;
// Methods
// Constructors
// Accessor Methods
/**
   * Get the value of atributo1
   * 
   * @return the value of atributo1
   */
private int getAtributo1 (  ) {
return atributo1;
}
/**
   * Set the value of atributo1
   * 
   * 
   */
private void setAtributo1 ( int value  ) {
atributo1 = value;
}
/**
   * Get the value of atributo2
   * 
   * @return the value of atributo2
   */
private int getAtributo2 (  ) {
return atributo2;
}
/**
   * Set the value of atributo2
   * 
   * 
   */
private void setAtributo2 ( int value  ) {
atributo2 = value;
}
// Operations
/**
   * 
   * @return   
   */
public  operacion1 ( ) {

}
}



/**
 * Class Clase_2
 * 
 */
public class Clase_2 {
// Fields
// 
private int atributo1;
// 
private int atributo2;
// 
private Clase_1 atributo3;
// Methods
// Constructors
// Accessor Methods
/**
   * Get the value of atributo1
   * 
   * @return the value of atributo1
   */
private int getAtributo1 (  ) {
return atributo1;
}
/**
   * Set the value of atributo1
   * 
   * 
   */
private void setAtributo1 ( int value  ) {
atributo1 = value;
}
/**
   * Get the value of atributo2
   * 
   * @return the value of atributo2
   */
private int getAtributo2 (  ) {
return atributo2;
}
/**
   * Set the value of atributo2
   * 
   * 
   */
private void setAtributo2 ( int value  ) {
atributo2 = value;
}
/**
   * Get the value of atributo3
   * 
   * @return the value of atributo3
   */
private Clase_1 getAtributo3 (  ) {
return atributo3;
}
/**
   * Set the value of atributo3
   * 
   * 
   */
private void setAtributo3 ( Clase_1 value  ) {
atributo3 = value;
}
// Operations
}


/**
 * Class Clase_A
 * 
 */
public class Clase_A {
// Fields
// 
private int atributoA1;
// Methods
// Constructors
// Accessor Methods
/**
   * Get the value of atributoA1
   * 
   * @return the value of atributoA1
   */
private int getAtributoA1 (  ) {
return atributoA1;
}
/**
   * Set the value of atributoA1
   * 
   * 
   */
private void setAtributoA1 ( int value  ) {
atributoA1 = value;
}
// Operations
}



/**
 * Interface Interfaz_Q
 * 
 */
public interface Interfaz_Q {
// Methods
// Constructors
// Accessor Methods
// Operations
/**
   * 
   * @return   
   */
public  operacion1 ( );
}

Todo muy sencillito, es el primer paso para comprender las relaciones. Todos los archivos fueron generados automáticamente con Umbrello después de dibujar el diagrama de clases.