home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / qtawk / calcin.exp < prev    next >
Text File  |  1990-09-27  |  9KB  |  290 lines

  1. # QTAwk Utility
  2. # CALCIN - INfix Expression Calculator
  3. #
  4. # Infix Expression Calculator.
  5. # (C) Copyright 1989, 1990 Terry D. Boldt, All Rights Reserved.
  6. #
  7. # With ANSI.SYS driver use
  8. #
  9. # input: expression in Algebraic form
  10. # output: value of each expression
  11. #
  12. #
  13. BEGIN {
  14.     _arg_chk = TRUE;
  15.  
  16.     monthn[ 1] = "January";
  17.     monthn[ 2] = "February";
  18.     monthn[ 3] = "March";
  19.     monthn[ 4] = "April";
  20.     monthn[ 5] = "May";
  21.     monthn[ 6] = "June";
  22.     monthn[ 7] = "July";
  23.     monthn[ 8] = "August";
  24.     monthn[ 9] = "September";
  25.     monthn[10] = "October";
  26.     monthn[11] = "November";
  27.     monthn[12] = "December";
  28.  
  29.     weekday[0] = "Sunday";
  30.     weekday[1] = "Monday";
  31.     weekday[2] = "Tuesday";
  32.     weekday[3] = "Wednesday";
  33.     weekday[4] = "Thursday";
  34.     weekday[5] = "Friday";
  35.     weekday[6] = "Saturday";
  36.  
  37.     stderr = "stderr";
  38.  
  39.     # set display colors
  40.     # highlight
  41.     hl_color = "\x01b[0;32;40m";
  42.     # normal display
  43.     nl_color = "\x01b[1;31;44m";
  44.  
  45.     OFMT = "%.14g";
  46.  
  47.     se_flag = TRUE;
  48.     recover_flag = TRUE;
  49.  
  50.     split(sdate(1),tdate,/\//);
  51.     today = jdn(tdate[3],tdate[1],tdate[2]);
  52.  
  53. #
  54. # Gregorian/Julian calender flag.
  55. #   TRUE == julian
  56. #   FALSE == gregorian
  57. #
  58.     greg_jul = FALSE;
  59.  
  60.     numeric = /^({_i}|{_f}|{_r})$/;
  61.  
  62.     Yess = /[Yy](es)?/;           # Yes string
  63.     Nos  = /[Nn]o?/;              # No  string
  64.     Yes  = /^{_w}*{Yess}{_w}*$/;      # Yes answer
  65.     No     = /^{_w}*{Nos}{_w}*$/;       # No  answer
  66.     Quit = /^{_w}*[Qq](uit)?({_w}+({Yess}|{Nos}))?{_w}*$/;  # define regular expression To  Quit
  67.     Help = /^{_w}*[Hh](elp)?{_w}*$/;    # define regular expression for Help
  68.     Stat = /^{_w}*[Ss](tat(us)?)?{_w}*$/; # define regular expression for status
  69.     Cls  = /^{_w}*[Cc](ls)?{_w}*$/;       # define regular expression To Clear Screen
  70.  
  71.     quit_status = TRUE;
  72.  
  73.     # find number of variables defined in calculator progam
  74.     # any new variables defined by user will be in addition to this number
  75.     # the number, vcnt, is then used in displaying status
  76.     vcnt = var_number(FALSE);
  77.  
  78.     copyright;
  79.     prompt;
  80. }
  81.  
  82. GROUP Quit {
  83.     if ( NF > 1 ) {
  84.     switch ( $2 ) {
  85.         case Yes:
  86.         quit_status = TRUE;
  87.         break;
  88.         case No:
  89.         quit_status = FALSE;
  90.         break;
  91.     }
  92.     }
  93.     exit 2;
  94. }
  95.  
  96. GROUP Help { help; prompt; next; }
  97.  
  98. GROUP Stat { stat; prompt; next; }
  99.  
  100. GROUP Cls  { cls;  copyright; prompt; next; }
  101.  
  102.     {
  103.     line = $0;
  104.     if ( $0 !~ /;$/ ) line ∩= ';'; #check for trailing ';'
  105.     jj = execute(line,se_flag,recover_flag);
  106.     if ( jj ~~ numeric ) jj = addcomma(jj);
  107.     print '\t' ∩ jj;
  108.     prompt;
  109. }
  110.  
  111. END {
  112.     if ( quit_status ) copyright;
  113. }
  114.  
  115. # function to convert year/month/day into julian day number
  116. function jdn(year,month,day) {
  117.     local yr;
  118.     local pfac = 0.6;
  119.     local ljdn;
  120.  
  121.     yr = year + (month - 3.0) / 12.0;
  122.     ljdn = int(367.0 * yr + pfac) - (2 * int(yr)) + int(yr/4.0)
  123.        + int(day) + 1721117;
  124.     if ( !greg_jul ) ljdn += -int(yr/100.0) + int(yr/400.0) + 2;
  125.     return ljdn;
  126. }
  127.  
  128. #  function to convert julian dday number to year/month/day
  129. function caln(cjdn) {
  130.     local n, ic, np, npp, mp;
  131.     local yr, mo, day;
  132.     local wkd = weekday[(cjdn + 1) % 7];
  133.  
  134.     n = int(cjdn) - 1721119;
  135.     ic = int((n - 0.2)/36524.25);
  136.     if ( greg_jul ) np = n + 2; else np = n + ic - (ic / 4);
  137.     yr = int((np - 0.2)/365.25);
  138.     npp = np - int(365.25 * yr);
  139.     mp = int((npp - 0.5)/30.6);
  140.     day = int(npp + 0.5 - 30.6 * mp);
  141.     if ( mp <= 9 ) mo = mp + 3;
  142.       else {
  143.     yr++;
  144.     mo = mp - 9;
  145.     }
  146.     return wkd ∩ ", " ∩ day ∩ " " ∩ monthn[mo] ∩ ", " ∩ yr;
  147. }
  148.  
  149. # function to set stack to today + days in future (past)
  150. function fdate(days) {
  151.     local tdate; # tdate[1] == month, tdate[2] == day, tdate[3] = year
  152.     local fd;
  153.     local wkday;
  154.  
  155.     fd = today + days;
  156.     return caln(fd);
  157. }
  158.  
  159. # function to provide header & copyright information
  160. function copyright() {
  161.     cls;
  162.     fprintf(stderr,"\n");
  163.     fprintf(stderr,"Infix Calculator\n\x01b[0;32;40m(C) Copyright 1989, 1990 Terry D. Boldt, All Rights Reserved.\x01b[1;31;44m\n");
  164. }
  165.  
  166. function help() {
  167.     local j;
  168.  
  169.     copyright;
  170.     fprintf(stderr,"Operators Available:\n");
  171.     fprintf(stderr,"n1 [\x01b[0;32;40m+ - * /\x01b[1;31;44m] n2, add subtract multiply divide n1 to n2\n");
  172.     fprintf(stderr,"n1 [\x01b[0;32;40m%\x01b[1;31;44m] n2, n1 remainder of n2\n");
  173.     fprintf(stderr,"n1 [\x01b[0;32;40m^\x01b[1;31;44m] n2, n1 to n2 power\n");
  174.     fprintf(stderr,"n1 [\x01b[0;32;40m& | @\x01b[1;31;44m] n2, n2 bit-wise [ and or xor ] n2\n");
  175.     fprintf(stderr,"n1 [\x01b[0;32;40m<< >>\x01b[1;31;44m] n2, n1 shifted [ left right ] n2 bits\n");
  176.     fprintf(stderr,"atan2(n1,n2), arc_tan(n1/n2), \x01b[0;32;40m-π to π\x01b[1;31;44m\n");
  177.     fprintf(stderr,"\x01b[0;32;40m~\x01b[1;31;44mn1, one's complement of n1\n");
  178.     fprintf(stderr,"\x01b[0;32;40mvar\x01b[1;31;44m, display value of variable var\n");
  179.     fprintf(stderr,"built-in single argument functions:\n");
  180.     fprintf(stderr,"sin asin cos acos sinh cosh log log10 int exp sqrt fract\n");
  181.     fprintf(stderr,"\x01b[0;32;40mjdn(yr,mo,day)\x01b[1;31;44m - compute julian day number from date\n");
  182.     fprintf(stderr,"\x01b[0;32;40mdow(yr,mo,day)\x01b[1;31;44m - compute day of week from from date, Sunday == 0\n");
  183.     fprintf(stderr,"\x01b[0;32;40mcaln(jdn)\x01b[1;31;44m - compute date from julian day number\n");
  184.     fprintf(stderr,"\x01b[0;32;40mfdate(days)\x01b[1;31;44m - compute date days into future(past)\n");
  185.     fprintf(stderr,"\x01b[0;32;40mhrs(hh,mm,ss)\x01b[1;31;44m - compute hours from hour, minute, second\n");
  186.     fprintf(stderr,"\x01b[0;32;40mhms(hr)\x01b[1;31;44m - compute hour, minute, second from hours\n");
  187.     fprintf(stderr,"\x01b[0;32;40mnow() or now\x01b[1;31;44m - return current time, decimal\n");
  188.     fprintf(stderr,"\n\x01b[0;32;40m[Hh](elp)?\x01b[1;31;44m to display help\n");
  189.     fprintf(stderr,"\x01b[0;32;40m[Ss](tatus)?\x01b[1;31;44m to display status\n");
  190.     fprintf(stderr,"\x01b[0;32;40m[Cc](ls)?\x01b[1;31;44m to clear screen\n");
  191.     fprintf(stderr,"\x01b[0;32;40m[Qq](uit)?\x01b[1;31;44m to quit\n");
  192. }
  193.  
  194. # function to display calculator status
  195. function stat() {
  196.     local j, jj, ostr;
  197.  
  198.     copyright;
  199.     fprintf(stderr,"Calculator Status:\n");
  200.     printf("Calender Set: \x01b[0;32;40m%s\x01b[1;31;44m.\n",greg_jul ? "Julian" : "Gregorian");
  201.     fprintf(stderr,"Assume \x01b[0;32;40m%s\x01b[1;31;44m for Trig. Functions\n",DEGREES ? "Degrees" : "Radians");
  202.     fprintf(stderr,"Defined variables:\n");
  203.     fprintf(stderr,"Pre-Defined:\n");
  204.     fprintf(stderr,"\x01b[0;32;40mse_flag\x01b[1;31;44m == %s\n",se_flag ? "TRUE" : "FALSE");
  205.     fprintf(stderr,"\x01b[0;32;40mrecover_flag\x01b[1;31;44m == %s\n",recover_flag ? "TRUE" : "FALSE");
  206.     fprintf(stderr,"\x01b[0;32;40mtoday\x01b[1;31;44m == %s\n",addcomma(today));
  207.     fprintf(stderr,"User-Defined:\n");
  208.     for ( j = vcnt , ostr = ud_sym(j++,jj) ; jj ; ostr = ud_sym(j++,jj) ) {
  209.     if ( ostr ~~ numeric ) ostr = addcomma(ostr);
  210.     fprintf(stderr,"\x01b[0;32;40m%s\x01b[1;31;44m == %s\n",jj,ostr);
  211.     }
  212. }
  213.  
  214. # function to convert hours, minutes, seconds to fractional hours
  215. function hrs(hrs,min,sec) {
  216.     return hrs += min/60.0 + sec/3600.0;
  217. }
  218.  
  219. # functions to convert fractional hours to hrs, min, sec
  220. function hms(hrs) {
  221.     local hr = int(hrs);
  222.     local mins = fract(hrs) * 60;
  223.     local secs = fract(mins) * 60;
  224.     local ampm;
  225.  
  226.     mins = int(mins);
  227.     if ( secs > 59 ) {
  228.     secs = 0;
  229.     mins++;
  230.     } else secs = int(secs);
  231.     if ( mins > 59 ) {
  232.     mins = 0;
  233.     hr++;
  234.     }
  235.     if ( hr >= 24 ) hr = 0;
  236.     if ( hr < 12 ) ampm = "am";
  237.       else {
  238.     ampm = "pm";
  239.     if ( hr > 12 ) hr -= 12;
  240.     }
  241.     return hr ∩ ":" ∩ mins ∩ ":" ∩ secs ∩ " (" ∩ ampm ∩ ")";
  242. }
  243.  
  244. # function to add commas to numbers
  245. function addcomma(x) {
  246.     local num;
  247.     local spat;
  248.     local bnum = /{_d}{3,3}([,.]|$)/;
  249.  
  250.     if ( x < 0 ) return "-" addcomma(-x);
  251.     num = sprintf("%.14g",x);        # num is dddddd.dd
  252.     spat = num ~~ /\./ ? /{_d}{4,4}[.,]/ : /{_d}{4,4}(,|$)/;
  253.     while ( num ~~ spat ) sub(bnum,",&",num);
  254.     return num;
  255. }
  256.  
  257. function prompt() {
  258.     printf("<>");
  259. }
  260.  
  261. # function to clear screen and home cursor
  262. function cls() {
  263.       # clear screen and home cursor string
  264.     local _cls_ = "\x01b[2J";
  265.  
  266.     fprintf(stderr,_cls_);
  267. }
  268.  
  269. # function to return the current number of GLOBAL variables defined in utility
  270. function var_number(display) {
  271.     local cnt, j, jj;
  272.  
  273.     for ( cnt = 1, j = ud_sym(cnt,jj) ; jj ; j = ud_sym(++cnt,jj) )
  274.     if ( display ) print cnt ∩ " : " ∩ jj ∩ " ==>" ∩ j ∩ "<==";
  275.     return cnt;
  276. }
  277.  
  278. # function to compute day of week from date
  279. function dow(yr,mo,day) {
  280.     return (jdn(yr,mo,day) + 1) % 7;
  281. }
  282.  
  283. # function return current time, decimal
  284. function now() {
  285.     local ntime;
  286.  
  287.     split(stime(1),ntime,/:/);
  288.     return hrs(ntime[1] + 0.0,ntime[2] + 0.0,ntime[3] + 0.0);
  289. }
  290.