File: decimalToBinary

package info (click to toggle)
kvirc2 2.1.3-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 16,308 kB
  • ctags: 9,533
  • sloc: cpp: 86,257; sh: 10,532; makefile: 631; perl: 419; ansic: 315; sed: 16
file content (63 lines) | stat: -rw-r--r-- 1,407 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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