# This script creates aliases that
# transform a decimal number to a binary form
# And calls it for the first time with a dialog.

alias decimalToBinary1 {
	if("$1"==""){
		echo You must call this alias with at least one parameter.
		halt;
	}
	if(!$isnumber($1)){
		echo You must call this alias with a DECIMAL NUMBER.
		halt;
	}
	%tmp = $1
	if(%tmp != 0)%result = ;
	else %result = 0;
	while(%tmp){
		%result = $strcat($calc(%tmp % 2),%result);
		%tmp = $calc(%tmp / 2);
	}
	echo The binary rappresentation of $1 is %result;
}

alias decimalToBinary2 {

	# Alternative form of the same algorythm

	if("$1"==""){
		echo You must call this alias with at least one parameter.
		halt;
	}
	if(!$isnumber($1)){
		echo You must call this alias with a DECIMAL NUMBER.
		halt;
	}
	%tmp = $1
	if(%tmp != 0){
		%result1 = ;
		%result2 = ;
	} else %result1 = 0;
	%tag = 1;
	while(%tmp){
		if(%tag == 1){
			%result1 = $calc(%tmp % 2);
			%result1 <+ %result2;
			%tag = 2;
		} else {
			%result2 = $calc(%tmp %2);
			%result2 <+ %result1;
			%tag = 1;
		}
		%tmp = $calc(%tmp / 2);
	}
	if(%tag == 2)echo The binary rappresentation of $1 is %result1;
	else echo The binary rappresentation of $1 is %result2;
}

if("$1"!="")decimalToBinary1 $dialogresult
else dialog (                          \
				lineinput,             \
				Decimal To Binary,     \
				Insert a decimal number \
			) decimalToBinary1 $dialogresult
