home *** CD-ROM | disk | FTP | other *** search
- # QTAwk Utility
- # CALCIN - INfix Expression Calculator
- #
- # Infix Expression Calculator.
- # (C) Copyright 1989, 1990 Terry D. Boldt, All Rights Reserved.
- #
- # With ANSI.SYS driver use
- #
- # input: expression in Algebraic form
- # output: value of each expression
- #
- #
- BEGIN {
- _arg_chk = TRUE;
-
- monthn[ 1] = "January";
- monthn[ 2] = "February";
- monthn[ 3] = "March";
- monthn[ 4] = "April";
- monthn[ 5] = "May";
- monthn[ 6] = "June";
- monthn[ 7] = "July";
- monthn[ 8] = "August";
- monthn[ 9] = "September";
- monthn[10] = "October";
- monthn[11] = "November";
- monthn[12] = "December";
-
- weekday[0] = "Sunday";
- weekday[1] = "Monday";
- weekday[2] = "Tuesday";
- weekday[3] = "Wednesday";
- weekday[4] = "Thursday";
- weekday[5] = "Friday";
- weekday[6] = "Saturday";
-
- stderr = "stderr";
-
- # set display colors
- # highlight
- hl_color = "\x01b[0;32;40m";
- # normal display
- nl_color = "\x01b[1;31;44m";
-
- OFMT = "%.14g";
-
- se_flag = TRUE;
- recover_flag = TRUE;
-
- split(sdate(1),tdate,/\//);
- today = jdn(tdate[3],tdate[1],tdate[2]);
-
- #
- # Gregorian/Julian calender flag.
- # TRUE == julian
- # FALSE == gregorian
- #
- greg_jul = FALSE;
-
- numeric = /^({_i}|{_f}|{_r})$/;
-
- Yess = /[Yy](es)?/; # Yes string
- Nos = /[Nn]o?/; # No string
- Yes = /^{_w}*{Yess}{_w}*$/; # Yes answer
- No = /^{_w}*{Nos}{_w}*$/; # No answer
- Quit = /^{_w}*[Qq](uit)?({_w}+({Yess}|{Nos}))?{_w}*$/; # define regular expression To Quit
- Help = /^{_w}*[Hh](elp)?{_w}*$/; # define regular expression for Help
- Stat = /^{_w}*[Ss](tat(us)?)?{_w}*$/; # define regular expression for status
- Cls = /^{_w}*[Cc](ls)?{_w}*$/; # define regular expression To Clear Screen
-
- quit_status = TRUE;
-
- # find number of variables defined in calculator progam
- # any new variables defined by user will be in addition to this number
- # the number, vcnt, is then used in displaying status
- vcnt = var_number(FALSE);
-
- copyright;
- prompt;
- }
-
- GROUP Quit {
- if ( NF > 1 ) {
- switch ( $2 ) {
- case Yes:
- quit_status = TRUE;
- break;
- case No:
- quit_status = FALSE;
- break;
- }
- }
- exit 2;
- }
-
- GROUP Help { help; prompt; next; }
-
- GROUP Stat { stat; prompt; next; }
-
- GROUP Cls { cls; copyright; prompt; next; }
-
- {
- line = $0;
- if ( $0 !~ /;$/ ) line ∩= ';'; #check for trailing ';'
- jj = execute(line,se_flag,recover_flag);
- if ( jj ~~ numeric ) jj = addcomma(jj);
- print '\t' ∩ jj;
- prompt;
- }
-
- END {
- if ( quit_status ) copyright;
- }
-
- # function to convert year/month/day into julian day number
- function jdn(year,month,day) {
- local yr;
- local pfac = 0.6;
- local ljdn;
-
- yr = year + (month - 3.0) / 12.0;
- ljdn = int(367.0 * yr + pfac) - (2 * int(yr)) + int(yr/4.0)
- + int(day) + 1721117;
- if ( !greg_jul ) ljdn += -int(yr/100.0) + int(yr/400.0) + 2;
- return ljdn;
- }
-
- # function to convert julian dday number to year/month/day
- function caln(cjdn) {
- local n, ic, np, npp, mp;
- local yr, mo, day;
- local wkd = weekday[(cjdn + 1) % 7];
-
- n = int(cjdn) - 1721119;
- ic = int((n - 0.2)/36524.25);
- if ( greg_jul ) np = n + 2; else np = n + ic - (ic / 4);
- yr = int((np - 0.2)/365.25);
- npp = np - int(365.25 * yr);
- mp = int((npp - 0.5)/30.6);
- day = int(npp + 0.5 - 30.6 * mp);
- if ( mp <= 9 ) mo = mp + 3;
- else {
- yr++;
- mo = mp - 9;
- }
- return wkd ∩ ", " ∩ day ∩ " " ∩ monthn[mo] ∩ ", " ∩ yr;
- }
-
- # function to set stack to today + days in future (past)
- function fdate(days) {
- local tdate; # tdate[1] == month, tdate[2] == day, tdate[3] = year
- local fd;
- local wkday;
-
- fd = today + days;
- return caln(fd);
- }
-
- # function to provide header & copyright information
- function copyright() {
- cls;
- fprintf(stderr,"\n");
- fprintf(stderr,"Infix Calculator\n\x01b[0;32;40m(C) Copyright 1989, 1990 Terry D. Boldt, All Rights Reserved.\x01b[1;31;44m\n");
- }
-
- function help() {
- local j;
-
- copyright;
- fprintf(stderr,"Operators Available:\n");
- fprintf(stderr,"n1 [\x01b[0;32;40m+ - * /\x01b[1;31;44m] n2, add subtract multiply divide n1 to n2\n");
- fprintf(stderr,"n1 [\x01b[0;32;40m%\x01b[1;31;44m] n2, n1 remainder of n2\n");
- fprintf(stderr,"n1 [\x01b[0;32;40m^\x01b[1;31;44m] n2, n1 to n2 power\n");
- fprintf(stderr,"n1 [\x01b[0;32;40m& | @\x01b[1;31;44m] n2, n2 bit-wise [ and or xor ] n2\n");
- fprintf(stderr,"n1 [\x01b[0;32;40m<< >>\x01b[1;31;44m] n2, n1 shifted [ left right ] n2 bits\n");
- fprintf(stderr,"atan2(n1,n2), arc_tan(n1/n2), \x01b[0;32;40m-π to π\x01b[1;31;44m\n");
- fprintf(stderr,"\x01b[0;32;40m~\x01b[1;31;44mn1, one's complement of n1\n");
- fprintf(stderr,"\x01b[0;32;40mvar\x01b[1;31;44m, display value of variable var\n");
- fprintf(stderr,"built-in single argument functions:\n");
- fprintf(stderr,"sin asin cos acos sinh cosh log log10 int exp sqrt fract\n");
- fprintf(stderr,"\x01b[0;32;40mjdn(yr,mo,day)\x01b[1;31;44m - compute julian day number from date\n");
- fprintf(stderr,"\x01b[0;32;40mdow(yr,mo,day)\x01b[1;31;44m - compute day of week from from date, Sunday == 0\n");
- fprintf(stderr,"\x01b[0;32;40mcaln(jdn)\x01b[1;31;44m - compute date from julian day number\n");
- fprintf(stderr,"\x01b[0;32;40mfdate(days)\x01b[1;31;44m - compute date days into future(past)\n");
- fprintf(stderr,"\x01b[0;32;40mhrs(hh,mm,ss)\x01b[1;31;44m - compute hours from hour, minute, second\n");
- fprintf(stderr,"\x01b[0;32;40mhms(hr)\x01b[1;31;44m - compute hour, minute, second from hours\n");
- fprintf(stderr,"\x01b[0;32;40mnow() or now\x01b[1;31;44m - return current time, decimal\n");
- fprintf(stderr,"\n\x01b[0;32;40m[Hh](elp)?\x01b[1;31;44m to display help\n");
- fprintf(stderr,"\x01b[0;32;40m[Ss](tatus)?\x01b[1;31;44m to display status\n");
- fprintf(stderr,"\x01b[0;32;40m[Cc](ls)?\x01b[1;31;44m to clear screen\n");
- fprintf(stderr,"\x01b[0;32;40m[Qq](uit)?\x01b[1;31;44m to quit\n");
- }
-
- # function to display calculator status
- function stat() {
- local j, jj, ostr;
-
- copyright;
- fprintf(stderr,"Calculator Status:\n");
- printf("Calender Set: \x01b[0;32;40m%s\x01b[1;31;44m.\n",greg_jul ? "Julian" : "Gregorian");
- fprintf(stderr,"Assume \x01b[0;32;40m%s\x01b[1;31;44m for Trig. Functions\n",DEGREES ? "Degrees" : "Radians");
- fprintf(stderr,"Defined variables:\n");
- fprintf(stderr,"Pre-Defined:\n");
- fprintf(stderr,"\x01b[0;32;40mse_flag\x01b[1;31;44m == %s\n",se_flag ? "TRUE" : "FALSE");
- fprintf(stderr,"\x01b[0;32;40mrecover_flag\x01b[1;31;44m == %s\n",recover_flag ? "TRUE" : "FALSE");
- fprintf(stderr,"\x01b[0;32;40mtoday\x01b[1;31;44m == %s\n",addcomma(today));
- fprintf(stderr,"User-Defined:\n");
- for ( j = vcnt , ostr = ud_sym(j++,jj) ; jj ; ostr = ud_sym(j++,jj) ) {
- if ( ostr ~~ numeric ) ostr = addcomma(ostr);
- fprintf(stderr,"\x01b[0;32;40m%s\x01b[1;31;44m == %s\n",jj,ostr);
- }
- }
-
- # function to convert hours, minutes, seconds to fractional hours
- function hrs(hrs,min,sec) {
- return hrs += min/60.0 + sec/3600.0;
- }
-
- # functions to convert fractional hours to hrs, min, sec
- function hms(hrs) {
- local hr = int(hrs);
- local mins = fract(hrs) * 60;
- local secs = fract(mins) * 60;
- local ampm;
-
- mins = int(mins);
- if ( secs > 59 ) {
- secs = 0;
- mins++;
- } else secs = int(secs);
- if ( mins > 59 ) {
- mins = 0;
- hr++;
- }
- if ( hr >= 24 ) hr = 0;
- if ( hr < 12 ) ampm = "am";
- else {
- ampm = "pm";
- if ( hr > 12 ) hr -= 12;
- }
- return hr ∩ ":" ∩ mins ∩ ":" ∩ secs ∩ " (" ∩ ampm ∩ ")";
- }
-
- # function to add commas to numbers
- function addcomma(x) {
- local num;
- local spat;
- local bnum = /{_d}{3,3}([,.]|$)/;
-
- if ( x < 0 ) return "-" addcomma(-x);
- num = sprintf("%.14g",x); # num is dddddd.dd
- spat = num ~~ /\./ ? /{_d}{4,4}[.,]/ : /{_d}{4,4}(,|$)/;
- while ( num ~~ spat ) sub(bnum,",&",num);
- return num;
- }
-
- function prompt() {
- printf("<>");
- }
-
- # function to clear screen and home cursor
- function cls() {
- # clear screen and home cursor string
- local _cls_ = "\x01b[2J";
-
- fprintf(stderr,_cls_);
- }
-
- # function to return the current number of GLOBAL variables defined in utility
- function var_number(display) {
- local cnt, j, jj;
-
- for ( cnt = 1, j = ud_sym(cnt,jj) ; jj ; j = ud_sym(++cnt,jj) )
- if ( display ) print cnt ∩ " : " ∩ jj ∩ " ==>" ∩ j ∩ "<==";
- return cnt;
- }
-
- # function to compute day of week from date
- function dow(yr,mo,day) {
- return (jdn(yr,mo,day) + 1) % 7;
- }
-
- # function return current time, decimal
- function now() {
- local ntime;
-
- split(stime(1),ntime,/:/);
- return hrs(ntime[1] + 0.0,ntime[2] + 0.0,ntime[3] + 0.0);
- }
-