use English; @ABBR = ("m", "u", "n", "p", "f", "k", "M", "G", "T"); @REPL = ("e-3", "e-6", "e-9", "e-12", "e-15", "e3", "e6", "e9", "e12"); @SUBROUTINES = (); $REPL = 1; printf << "~~~~~~~~~~"; ------------------------------------------------------------------------------- -- -- ADVANCED PERL STYLE EXPRESSION CALCULATOR. -- Design by Jan Uhrin (inspired by book) -- ~~~~~~~~~~ $logfile = $0 . ".log"; ############################################################################### # Definition of constants $EDITOR = 'D:\Apps\EditPlus\editplus.exe'; ### CONSTANT $PI = 4 * atan2(1,1); ### CONSTANT $pi = $PI; ### CONSTANT $E = exp(1); ### CONSTANT $e = $E; ### CONSTANT ########################################### ### CONSTANT &help(); system("color E0"); open(LOGFILE, ">>$logfile") or printf("\nERROR: Could not open $logfile for writing\n"); $prompt = 'ucalc.pl % > '; $now_string = gmtime; $ACC=0; $counter = 0; $prompt1 = $prompt; $prompt1 =~ s/%/$counter/gi; printf $prompt1; printf LOGFILE "\n------------------- " . $now_string . "\n> "; LINE: while (<>) { chop; # if someone writes e.g. 4x4 consider it as multiplication if ($_ =~ /\dx\d/) { s/x/*/gi; } # If replacement is ON then replace abbreviations e.g. k for 1e3, M for 1e6 if ($REPL) { $_ = &replace_abbr($_); } # Execute (evaluate) the expression; accumulate the result if ($_ =~ /each_line/) { s/each_line\s+//gi; &each_line($_); } else { $r = eval($_); } # Update accumulator $ACC += $r; # Execute system command if it starts with ! if ($_ =~ /\!/) { $cmd = $_; $cmd =~ s/\!//gi; $r = system($cmd); } # s/\n//gi; $len=length($prompt1); push(@C, $_); # History of Commands push(@R, $r); # History of Results $counter++; $prompt1 = $prompt; $prompt1 =~ s/%/$counter/gi; if (length($_)<1) { $_ = '$r'; $r = $r_last; $this_result = ' ' x $len . $_ . ' = ' . $r . "\t\t\$ACC = $ACC\n" . $prompt1; } else { $this_result = ' ' x $len . $_ . ' = ' . $r . "\n" . $prompt1; } print $this_result; $r_last = $r; print LOGFILE $_; print LOGFILE "\t\t= " . $this_result; } close(LOGFILE); ############################################################################### sub repl { ### Toggles replacement mode (i.e. m=2e-3, u=2e-6, ... k=2e3, M=2e9) if ($REPL eq 0) { $REPL = 1; printf("Info: Replacement of abbreviations (e.g. m, u, n, s) is now ON.\n"); return 1; } else { $REPL = 0; printf("Info: Replacement of abbreviations (e.g. m, u, n, s) is now OFF.\n"); return 0; } } ############################################################################### sub abbr { ### Prints out abbreviations my($i)=0; my($abbr); foreach $abbr (@ABBR) { printf("--\t%s\t%s\n", $ABBR[$i], $REPL[$i]); $i++; } return 1; } ############################################################################### sub replace_abbr { #-- Replaces abbreviations, eg. MHz->e6 (see @ABBR array) my($in) = (@_); my($i)=0; my($abbr); foreach $abbr (@SUBROUTINES) { #printf("-- " . $abbr . "\n"); if ($in =~ /$abbr/) { return $in; } } foreach $abbr (@ABBR) { if ($in =~ /\d$ABBR[$i]/) { printf("Info: Replacing string $& ... To turn off/on replacements enter command \"repl\".\n"); $in =~ s/$ABBR[$i]/$REPL[$i]/g; } $i++; } return($in); } ############################################################################### sub help { ### Writes out help about Constants and Functions printf "-" x 79 . "\n"; printf("-- LOGFILE = $logfile\t(commands, results are logged here)\n--\n"); printf("-- CONSTANT AND FUNCTION DEFINITIONS:\n"); printf("-- \$r = last result\n"); printf("-- \$R[n] = n-th result\n"); printf("-- \$ACC = accumulated result (can be reset by assigning 0 to it)\n"); printf("-- !command = execute system (DOS/Windows) command\n"); if (open(SCRIPT, $0)) { @SUBROUTINES = (); while (