-
Notifications
You must be signed in to change notification settings - Fork 0
/
clc.pl
81 lines (78 loc) · 2.05 KB
/
clc.pl
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
my $level=0;
sub debug {
my $txt = shift;
print ((" " x ($level*4)).$txt);
}
sub calc {
$level++;
$line = shift;
$line =~ s/\s+//g;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
$rlst = $line;
#debug "calc($line)\n";
if (length($line) == 0) {
$rslt = 0;
} elsif ($line =~ /^(\d+\.?\d*)$/) {
debug "value=$1\n";
$rslt = $1;
} elsif ($line =~ /^\d+(\:\d+)+/) {
#sexagesimal number
my $ln = $line;
$rslt=0;
while ($ln=~/(\d+)\:(.*)/) {
debug "$ln: $rslt >> ";
$rslt = (60*$rslt) + $1;
debug "$rslt\n";
$ln=$2;
}
$rslt = (60*$rslt) + $ln;
} elsif ($line =~ /(.*)\(([^\)]+)\)(.*)/) {
(my $a, my $b, my $c) = ($1, $2, $3);
debug "eval($b)\n";
my $d = calc($b);
$rslt = calc("$a $d $c");
} elsif ($line =~ /^(.*?)\s*\+\s*(\S.*?)$/) {
(my $a, my $b) = ($1, $2);
debug "add($a, $b)\n";
$rslt = calc($a) + calc($b);
debug " $a + $b = $rslt\n";
} elsif ($line =~ /^(.*)\s*\-\s*(\S.*?)$/) {
(my $a, my $b) = ($1, $2);
debug "sub($a, $b)\n";
$rslt = calc($a) - calc($b);
debug " $a - $b = $rslt\n";
} elsif ($line =~ /^(.*?)\*(.*)$/) {
(my $a, my $b) = ($1, $2);
debug "mult($a, $b)\n";
$rslt = calc($a) * calc($b);
debug " $a * $b = $rslt\n";
} elsif ($line =~ /(.+)\/(.+)/) {
(my $a, my $b) = ($1, $2);
debug "div($a, $b)\n";
$rslt = calc($a) / calc($b);
debug "$a / $b = $rslt\n";
#} elsif ($line =~ /^sexg(.*)\)$/i) {
# my $d = $1;
# my $r = 0;
# $rslt = "";
# while ($d > 60) {
# $r = $d % 60;
# $d = ($d - $r)/60;
# $rslt = (sprintf("%02d",$r)).":$rslt";
# }
# $rslt = $d . ":$rslt";
} else {
die "Invalid operand: ($line)\n";
}
$level--;
$rslt;
}
#while (@ARGS) {
# $cmd .= " ". shift @ARGS;
#}
print ("$cmd = " . calc($cmd) . "\n");
while(<>) {
chomp;
print ("= " . calc($_) . "\n");
}