home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / qtawkos2.zip / QTAUTL.ZIP / calcin.exp < prev    next >
Text File  |  1991-10-08  |  8KB  |  246 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.     ECHO_INPUT = TRUE;
  38.  
  39.     stderr = "stderr";
  40.  
  41.     # set display colors
  42.     # highlight
  43.     hl_color = "\x01b[0;32;40m";
  44.     # normal display
  45.     nl_color = "\x01b[1;31;44m";
  46.  
  47.     OFMT = "%.14g";
  48.  
  49.     se_flag = TRUE;
  50.     recover_flag = TRUE;
  51.  
  52.     split(sdate(1),tdate,/\//);
  53.     today = jdn(tdate[3],tdate[1],tdate[2]);
  54.  
  55.     numeric = /^({_i}|{_f}|{_r})$/;
  56.  
  57.     Yess = /[Yy](es)?/;           # Yes string
  58.     Nos  = /[Nn]o?/;              # No  string
  59.     Yes  = /^{_w}*{Yess}{_w}*$/;      # Yes answer
  60.     No     = /^{_w}*{Nos}{_w}*$/;       # No  answer
  61.     Quit = /^{_w}*[Qq](uit)?({_w}+({Yess}|{Nos}))?{_w}*$/;  # define regular expression To  Quit
  62.     Help = /^{_w}*[Hh](elp)?{_w}*$/;    # define regular expression for Help
  63.     Stat = /^{_w}*[Ss](tat(us)?)?{_w}*$/; # define regular expression for status
  64.     Cls  = /^{_w}*[Cc](ls)?{_w}*$/;       # define regular expression To Clear Screen
  65.  
  66.     quit_status = TRUE;
  67.  
  68.     # find number of variables defined in calculator progam
  69.     # any new variables defined by user will be in addition to this number
  70.     # the number, vcnt, is then used in displaying status
  71.     vcnt = var_number(FALSE);
  72.  
  73.     copyright;
  74.     prompt;
  75. }
  76.  
  77. GROUP Quit {
  78.     if ( NF > 1 ) {
  79.     switch ( $2 ) {
  80.         case Yes:
  81.         quit_status = TRUE;
  82.         break;
  83.         case No:
  84.         quit_status = FALSE;
  85.         break;
  86.     }
  87.     }
  88.     exit 2;
  89. }
  90.  
  91. GROUP Help { help; prompt; next; }
  92.  
  93. GROUP Stat { stat; prompt; next; }
  94.  
  95. GROUP Cls  { cls;  copyright; prompt; next; }
  96.  
  97.     {
  98.     line = $0;
  99.     if ( $0 !~ /;$/ ) line ∩= ';'; #check for trailing ';'
  100.     jj = execute(line,se_flag,recover_flag);
  101.     if ( jj ~~ numeric ) jj = addcomma(jj,"");
  102.     print '\t' ∩ jj;
  103.     prompt;
  104. }
  105.  
  106. END {
  107.     if ( quit_status ) copyright;
  108. }
  109.  
  110. function calndr(cjdn) {
  111.     local date;
  112.     local wkd = weekday[(cjdn + 1) % 7];
  113.  
  114.     split(cal(cjdn,1),date,/\//);
  115.     return wkd ∩ ", " ∩ date[3] ∩ " " ∩ monthn[date[2]] ∩ ", " ∩ date[1];
  116. }
  117.  
  118. # function to set stack to today + days in future (past)
  119. function fdate(days) {
  120.     local fd;
  121.  
  122.     fd = today + days;
  123.     return calndr(fd);
  124. }
  125.  
  126. # function to provide header & copyright information
  127. function copyright() {
  128.     cls;
  129.     fprintf(stderr,"\n");
  130.     fprintf(stderr,"Infix Calculator\n\x01b[0;32;40m(C) Copyright 1989, 1990 Terry D. Boldt, All Rights Reserved.\x01b[1;31;44m\n");
  131. }
  132.  
  133. function help() {
  134.     local j;
  135.  
  136.     copyright;
  137.     fprintf(stderr,"Operators Available:\n");
  138.     fprintf(stderr,"n1 [\x01b[0;32;40m+ - * /\x01b[1;31;44m] n2, add subtract multiply divide n1 to n2\n");
  139.     fprintf(stderr,"n1 [\x01b[0;32;40m%\x01b[1;31;44m] n2, n1 remainder of n2\n");
  140.     fprintf(stderr,"n1 [\x01b[0;32;40m^\x01b[1;31;44m] n2, n1 to n2 power\n");
  141.     fprintf(stderr,"n1 [\x01b[0;32;40m& | @\x01b[1;31;44m] n2, n2 bit-wise [ and or xor ] n2\n");
  142.     fprintf(stderr,"n1 [\x01b[0;32;40m<< >>\x01b[1;31;44m] n2, n1 shifted [ left right ] n2 bits\n");
  143.     fprintf(stderr,"atan2(n1,n2), arc_tan(n1/n2), \x01b[0;32;40m-π to π\x01b[1;31;44m\n");
  144.     fprintf(stderr,"\x01b[0;32;40m~\x01b[1;31;44mn1, one's complement of n1\n");
  145.     fprintf(stderr,"\x01b[0;32;40mvar\x01b[1;31;44m, display value of variable var\n");
  146.     fprintf(stderr,"built-in single argument functions:\n");
  147.     fprintf(stderr,"sin asin cos acos sinh cosh log log10 int exp sqrt fract\n");
  148.     fprintf(stderr,"\x01b[0;32;40mjdn(yr,mo,day)\x01b[1;31;44m - compute julian day number from date\n");
  149.     fprintf(stderr,"\x01b[0;32;40mdow(yr,mo,day)\x01b[1;31;44m - compute day of week from from date, Sunday == 0\n");
  150.     fprintf(stderr,"\x01b[0;32;40mcalndr(jdn)\x01b[1;31;44m - compute date from julian day number\n");
  151.     fprintf(stderr,"\x01b[0;32;40mfdate(days)\x01b[1;31;44m - compute date days into future(past)\n");
  152.     fprintf(stderr,"\x01b[0;32;40mhrs(hh,mm,ss)\x01b[1;31;44m - compute hours from hour, minute, second\n");
  153.     fprintf(stderr,"\x01b[0;32;40mhms(hr)\x01b[1;31;44m - compute hour, minute, second from hours\n");
  154.     fprintf(stderr,"\x01b[0;32;40mnow() or now\x01b[1;31;44m - return current time, decimal\n");
  155.     fprintf(stderr,"\n\x01b[0;32;40m[Hh](elp)?\x01b[1;31;44m to display help\n");
  156.     fprintf(stderr,"\x01b[0;32;40m[Ss](tatus)?\x01b[1;31;44m to display status\n");
  157.     fprintf(stderr,"\x01b[0;32;40m[Cc](ls)?\x01b[1;31;44m to clear screen\n");
  158.     fprintf(stderr,"\x01b[0;32;40m[Qq](uit)?\x01b[1;31;44m to quit\n");
  159. }
  160.  
  161. # function to display calculator status
  162. function stat() {
  163.     local j, jj, ostr;
  164.  
  165.     copyright;
  166.     fprintf(stderr,"Calculator Status:\n");
  167.     fprintf(stderr,"Assume \x01b[0;32;40m%s\x01b[1;31;44m for Trig. Functions\n",DEGREES ? "Degrees" : "Radians");
  168.     fprintf(stderr,"Defined variables:\n");
  169.     fprintf(stderr,"Pre-Defined:\n");
  170.     fprintf(stderr,"\x01b[0;32;40mse_flag\x01b[1;31;44m == %s\n",se_flag ? "TRUE" : "FALSE");
  171.     fprintf(stderr,"\x01b[0;32;40mrecover_flag\x01b[1;31;44m == %s\n",recover_flag ? "TRUE" : "FALSE");
  172.     fprintf(stderr,"\x01b[0;32;40mtoday\x01b[1;31;44m == %s\n",addcomma(today,""));
  173.     fprintf(stderr,"User-Defined:\n");
  174.     for ( j = vcnt , ostr = ud_sym(j++,jj) ; jj ; ostr = ud_sym(j++,jj) ) {
  175.     if ( ostr ~~ numeric ) ostr = addcomma(ostr,"");
  176.     fprintf(stderr,"\x01b[0;32;40m%s\x01b[1;31;44m == %s\n",jj,ostr);
  177.     }
  178. }
  179.  
  180. # function to convert hours, minutes, seconds to fractional hours
  181. function hrs(hrs,min,sec) {
  182.     return hrs += min/60.0 + sec/3600.0;
  183. }
  184.  
  185. # functions to convert fractional hours to hrs, min, sec
  186. function hms(hrs) {
  187.     local hr = int(hrs);
  188.     local mins = fract(hrs) * 60;
  189.     local secs = fract(mins) * 60;
  190.     local ampm;
  191.  
  192.     mins = int(mins);
  193.     if ( secs > 59 ) {
  194.     secs = 0;
  195.     mins++;
  196.     } else secs = int(secs);
  197.     if ( mins > 59 ) {
  198.     mins = 0;
  199.     hr++;
  200.     }
  201.     if ( hr >= 24 ) hr = 0;
  202.     if ( hr < 12 ) ampm = "am";
  203.       else {
  204.     ampm = "pm";
  205.     if ( hr > 12 ) hr -= 12;
  206.     }
  207.     return hr ∩ ":" ∩ mins ∩ ":" ∩ secs ∩ " (" ∩ ampm ∩ ")";
  208. }
  209.  
  210. # function to add commas to numbers
  211. #include <addcomma.exp>
  212.  
  213. function prompt() {
  214.     printf("<>");
  215. }
  216.  
  217. # function to clear screen and home cursor
  218. function cls() {
  219.       # clear screen and home cursor string
  220.     local _cls_ = "\x01b[2J";
  221.  
  222.     fprintf(stderr,_cls_);
  223. }
  224.  
  225. # function to return the current number of GLOBAL variables defined in utility
  226. function var_number(display) {
  227.     local cnt, j, jj;
  228.  
  229.     for ( cnt = 1, j = ud_sym(cnt,jj) ; jj ; j = ud_sym(++cnt,jj) )
  230.     if ( display ) print cnt ∩ " : " ∩ jj ∩ " ==>" ∩ j ∩ "<==";
  231.     return cnt;
  232. }
  233.  
  234. # function to compute day of week from date
  235. function dow(yr,mo,day) {
  236.     return (jdn(yr,mo,day) + 1) % 7;
  237. }
  238.  
  239. # function return current time, decimal
  240. function now() {
  241.     local ntime;
  242.  
  243.     split(stime(1),ntime,/:/);
  244.     return hrs(ntime[1] + 0.0,ntime[2] + 0.0,ntime[3] + 0.0);
  245. }
  246.