home *** CD-ROM | disk | FTP | other *** search
- ~~C~~
- /*--------------------------------------------------------------*
- TableCurve C Library Module
- *--------------------------------------------------------------*/
-
- #include <dos.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
- /*--------------------------------------------------------------*
- Although the full calling routine for the functions is
- specific to the Borland Turbo-C++/2.0 and Microsoft C 5.x/6.x
- compilers, the generated code has been designed for simplicity
- and for portability using standard ANSI C and standard
- BIOS/DOS system calls. Only the system interrupt function
- int86() and the system registers union REGS are likely to be
- compiler-dependent.
- *--------------------------------------------------------------*/
-
- /*--------------------------------------------------------------*
- Floating point exceptions are math errors such as numeric
- overflow and division by zero that normally abort a program
- dumping the user back to DOS. Included in the TableCurve
- generated code is a way to mask these errors with the Turbo-C
- and Microsoft C compilers. This code uses _control87()
- to mask all exceptions and a user matherr() function to
- resolve such errors in math routine calls such as pow(),
- log(), exp(), and so forth.
- ------------------------
- Note for Microsoft compilers: The /NOE option is required
- with the linker since the matherr() function is duplicated.
- The following batch file commands can be used:
- cl -c -AS %1.c
- link %1 /NOE ;
- ------------------------
- This code for masking floating point exceptions will be added
- by uncommenting the following #define MASKFPERR.
- *--------------------------------------------------------------*/
-
- /* #define MASKFPERR */ /* uncomment for masking math errors */
-
- #ifdef MASKFPERR
- #include <float.h>
- #define CW_NEW (CW_DEFAULT | EM_ZERODIVIDE | EM_OVERFLOW)
- #define MASK_ALL (0xFFFF)
- #endif
-
- /*--------------------------------------------------------------*
- function prototypes
- *--------------------------------------------------------------*/
- void display(int cnt);
- void cursor(int row, int col);
- int getattr(void);
- int getvmode(int *iscolor);
- void cls(int attr);
- void clsblk(int top, int left, int btm, int right, int attr);
- void psa(char *str, int attr, int row, int col);
- void pca(char c, int attr, int row, int col);
- void setwin(int trow, int lcol, int brow, int rcol, int attr,
- int border, char *title);
- int numfld(char *fld, int row, int col, int maxlen, int attr);
- double rtbis(double y,int dir);
- double `FNAME`(double x);
-
- double _x[17],_y[17];
-
- /*--------------------------------------------------------------*/
- void main(void)
- /*--------------------------------------------------------------*/
- {
- int i,irow=6,j,gooddata,vmode,iscolor,dir=0;
- int attr0,attr1,attr2;
- char temp[80];
-
- #ifdef MASKFPERR
- _control87(CW_NEW|EM_INVALID,MASK_ALL); /* mask all exceptions */
- #endif
-
- attr0=getattr(); /* screen attribute at startup */
- getvmode(&iscolor); /* get video mode and color flag */
- if(iscolor){
- attr1=1|(7<<4); /* main window color attribute */
- attr2=15|(1<<4); /* xy data window color attribute */
- }
- else{
- attr1=15|(0<<4); /* main window monochr. attribute */
- attr2=0|(7<<4); /* xy data window monochr. attribute */
- }
- cls(attr1);
-
- strcpy(temp,
- " TableCurve `LANG` Function:`FILE` `DATE` `TIME` ");
- setwin(0,1,24,78,attr1,2,temp); /* main window */
- strcpy(temp," `TITLE` ");
- setwin(4,32,23,76,attr2,1,temp); /* x-y data window */
- psa("`XTITLE`",attr2,5,34);
- psa("`YTITLE`",attr2,5,56);
-
- psa("`EQSTR`",attr1,2,3); /* equation data summary */
- psa("Eqn# `EQNO`",attr1,3,5);
- psa("r2=`R2VAL`",attr1,4,5);
- psa("a= `ASTR`",attr1,5,5);
- psa("b= `BSTR`",attr1,6,5);
- psa("c= `CSTR`",attr1,7,5);
- psa("d= `DSTR`",attr1,8,5);
- psa("e= `ESTR`",attr1,9,5);
- psa("f= `FSTR`",attr1,10,5);
- psa("g= `GSTR`",attr1,11,5);
- psa("h= `HSTR`",attr1,12,5);
- psa("i= `ISTR`",attr1,13,5);
- psa("j= `JSTR`",attr1,14,5);
- psa("k= `KSTR`",attr1,15,5);
- psa("X= `XTITLE`",attr1,17,3);
- psa("Y= `YTITLE`",attr1,18,3);
- psa("Enter Value [x=,y=]",attr1,20,3);
- psa("Press Esc to End Program",attr1,23,3);
-
- while(1){ /* data entry loop */
- j=irow-6;
- gooddata=numfld(temp,21,3,25,attr2); /* numeric input */
- if(gooddata==0){
- cls(attr0);
- exit(-1); /* exit with escape */
- }
- else if(gooddata>0){
- _x[j]=atof(temp); /* x entry */
- _y[j]=`FNAME`(_x[j]); /* calculate y-value */
- }
- else{
- _y[j]=atof(temp); /* y entry */
- _x[j]=rtbis(_y[j],++dir%2); /* find x root, alternate dir */
- }
- if(irow==22)
- clsblk(22,33,22,75,attr2); /* clr row at btm of window */
- sprintf(temp,"%.12lg",_x[j]);
- psa(temp,attr2,irow,34);
- sprintf(temp,"%.12lg",_y[j]);
- psa(temp,attr2,irow,56);
- if(++irow>22)
- irow=22;
- }
- }
-
- #ifdef MASKFPERR
- /*--------------------------------------------------------------*/
- int matherr(struct exception *m)
- /*--------------------------------------------------------------*/
- {
- if(m->type==UNDERFLOW){
- m->retval=0;
- return(1);
- }
- m->retval=0; /* can change to any default return value */
- return(1);
- }
- #endif
-
- /*--------------------------------------------------------------*/
- void cursor(int row, int col)
- /*--------------------------------------------------------------*/
- { /* Sets Cursor Position (row=0,col=0 Origin) */
- union REGS rin,rout;
- rin.h.ah=2;
- rin.h.bh=0;
- rin.h.dh=row;
- rin.h.dl=col;
- int86(0x10,&rin,&rout);
- }
-
- /*--------------------------------------------------------------*/
- int getattr(void)
- /*--------------------------------------------------------------*/
- {
- union REGS rin,rout;
- rin.h.ah=8; /* get attribute at cursor */
- rin.h.bh=0;
- int86(0x10,&rin,&rout);
- return((int)rout.h.ah);
- }
-
- /*--------------------------------------------------------------*/
- int getvmode(int *iscolor)
- /*--------------------------------------------------------------*/
- {
- union REGS rin,rout;
- int mode;
- rin.h.ah=15; /* get display mode */
- int86(0x10,&rin,&rout);
- mode=(int)rout.h.al;
- *iscolor=1;
- if(mode==0 || mode==2 || mode==7)
- *iscolor=0;
- return(mode); /* sets color flag, returns video mode */
- }
-
- /*--------------------------------------------------------------*/
- void cls(int attr)
- /*--------------------------------------------------------------*/
- { /* Clears Screen w/Attribute, Cursor to Origin */
- union REGS rin,rout;
- rin.h.ah=6;
- rin.h.al=0;
- rin.h.bh=attr;
- rin.h.ch=0; /* top row */
- rin.h.cl=0; /* top column */
- rin.h.dh=24; /* bottom row */
- rin.h.dl=79; /* bottom column */
- int86(0x10,&rin,&rout);
- cursor(0,0);
- }
-
- /*--------------------------------------------------------------*/
- void clsblk(int top, int left, int btm, int right, int attr)
- /*--------------------------------------------------------------*/
- { /* Clears Screen Block w/Attribute, Cursor Inside */
- union REGS rin,rout;
- rin.h.ah=6;
- rin.h.al=0;
- rin.h.bh=attr;
- rin.h.ch=top; /* top row */
- rin.h.cl=left; /* top column */
- rin.h.dh=btm; /* bottom row */
- rin.h.dl=right; /* bottom column */
- int86(0x10,&rin,&rout);
- cursor(top+1,left+1);
- }
-
- /*--------------------------------------------------------------*/
- void psa(char *str, int attr, int row, int col)
- /*--------------------------------------------------------------*/
- { /* Write String w/attribute at row,col */
- while (*str)
- pca(*str++,attr,row,col++);
- }
-
- /*--------------------------------------------------------------*/
- void pca(char c, int attr, int row, int col)
- /*--------------------------------------------------------------*/
- {
- union REGS rin,rout;
- cursor(row,col); /* set cursor */
- rin.h.ah=9; /* print char with attribute */
- rin.h.al=c;
- rin.h.bh=0;
- rin.h.bl=attr;
- rin.x.cx=1;
- int86(0x10,&rin,&rout);
- }
-
- /*--------------------------------------------------------------*/
- void setwin(int trow, int lcol, int brow, int rcol, int attr,
- int border, char *title)
- /*--------------------------------------------------------------*/
- { /* Set Simple Window and Title */
- int tl,tr,bl,br,lr,tb,i,length;
- clsblk(trow,lcol,brow,rcol,attr);
- if(border==1){ /* Single Border */
- tl=218; tr=191; bl=192; br=217; lr=196; tb=179;
- }
- else{ /* Double Border */
- tl=201; tr=187; bl=200; br=188; lr=205; tb=186;
- }
- pca(tl,attr,trow,lcol);
- pca(bl,attr,brow,lcol);
- pca(tr,attr,trow,rcol);
- pca(br,attr,brow,rcol);
- for(i=lcol+1; i<rcol; i++){
- pca(lr,attr,trow,i);
- pca(lr,attr,brow,i);
- }
- for(i=trow+1; i<brow; i++){
- pca(tb,attr,i,lcol);
- pca(tb,attr,i,rcol);
- }
- i=0;
- while(title[i++]!='\0');
- length=i-1;
- psa(title,attr,trow,(rcol+lcol)/2-length/2);
- }
-
- /*--------------------------------------------------------------*/
- int numfld(char *fld, int row, int col, int maxlen, int attr)
- /*--------------------------------------------------------------*/
- /* Simple Numeric Input Routine *
- * fld storage for number as char string *
- * row,col starting cursor position for field *
- * maxlen field length *
- * attr entry field attribute *
- * returns entry length(x), -entry length(y), 0(ESC hit) */
- {
- int i,j,yflag,expflag,pass;
- char c;
-
- for(j=0;j<maxlen;j++)
- pca(32,attr,row,col+j);
- cursor(row,col);
- i=j=yflag=expflag=0;
- while(i<maxlen){
- c=getch();
- pass=0;
- if(i==0){
- if(c=='y' || c=='Y'){ /* allow Y only in first position */
- yflag=1; /* set flag for Y being entered */
- pass=1; /* do not include character in return string */
- }
- if(c=='x' || c=='X') /* allow X only in first position */
- pass=1;
- }
- if(i==1 && c=='=') /* allow = only in second position */
- pass=1;
- if((c>='0' && c<='9') || c=='-' || c=='.' || c=='+' ||
- ( (c=='E' || c=='e') && !expflag ) || pass){
- pca(c,attr,row,col+i); /* print character */
- cursor(row,col+i+1); /* advance cursor */
- i++;
- if(!pass)
- fld[j++]=c;
- if(c=='E' || c=='e') /* flag first occurence of E for sci notn */
- expflag=1;
- }
- else if((c==10 || c==13 || i==maxlen) && i!=0)
- break;
- else if(c=='\b' && i>0){
- i--;
- pca(32,attr,row,col+i);
- if(i==0)
- yflag=0;
- if(j!=0){
- j--;
- if(fld[j]=='E' || fld[j]=='e')
- expflag=0;
- fld[j]=0;
- }
- }
- else if(c==27)
- return(0);
- }
- fld[j]='\0';
- return(yflag?-j:j);
- }
-
- /*--------------------------------------------------------------*/
- double rtbis(double y,int dir)
- /*--------------------------------------------------------------*/
- /* root bisection routine */
- /* dir=0 starts at lowest partition, dir=1 starts at highest partition */
- /* last chance is partition from XatYmin to XatYmax */
- /* returns 0 upon failure to find root */
- {
- double x1,x2,xinc,dx,f,fmid,xmid,rtb,xacc;
- int i,j;
- xacc=1E-6*`XMEAN`; /* convergence limit */
- xinc=`XRANGE`/4.0;
- for(i=0;i<5;i++){ /* x-scale into 4 partitions */
- if(i==4){
- x1=`XATYMIN`;
- x2=`XATYMAX`;
- }
- else{
- if(dir){
- x2=`XMAXIMUM`-xinc*(double)i;
- x1=`XMAXIMUM`-xinc*(double)(i+1);
- }
- else{
- x1=`XMINIMUM`+xinc*(double)i;
- x2=`XMINIMUM`+xinc*(double)(i+1);
- }
- }
- f=y-`FNAME`(x1);
- fmid=y-`FNAME`(x2);
- if(f*fmid >=0)
- continue;
- rtb = (f<0.0) ? (dx=x2-x1,x1) : (dx=x1-x2,x2);
- for(j=1; j<=100; j++){
- fmid=y-`FNAME`(xmid=rtb+(dx*= 0.5));
- if(fmid<=0)
- rtb=xmid;
- if(fabs(dx)<xacc || fmid==0.0)
- return(rtb);
- }
- }
- return(0);
- }
-
- !!C!!
- #include <math.h> `SCOPE`
- double erf(double x)`ERF`
- {`ERF`
- double t,z,ans;`ERF`
- z=fabs(x);`ERF`
- t=1.0/(1.0+0.5*z);`ERF`
- ans=(t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+`ERF`
- t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+`ERF`
- t*(-0.82215223+t*0.17087277))))))))));`ERF`
- return x>=0.0 ? 1.0-ans : -1.0+ans;`ERF`
- }`ERF`
-
- /*--------------------------------------------------------------*/
- double `FNAME`(double x)
- /*--------------------------------------------------------------*
- TableCurve Function: `FILE` `DATE` `TIME`
- `TITLE`
- X= `XTITLE`
- Y= `YTITLE`
- Eqn# `EQNO` `EQSTR`
- r2=`R2VAL`
- r2adj=`R2ADJ`
- StdErr=`STDERR`
- Fstat=`FVAL`
- a= `ASTR`
- b= `BSTR`
- c= `CSTR`
- d= `DSTR`
- e= `ESTR`
- f= `FSTR`
- g= `GSTR`
- h= `HSTR`
- i= `ISTR`
- j= `JSTR`
- k= `KSTR`
- *--------------------------------------------------------------*/
- {
- double y;
- double n;`FDECLN`
- double `FLIST`;
- x=`FX`;
- n=`FBAL2`;
- n=`FAUX`;
- x1=`F1`;
- x2=`F2`;
- x3=`F3`;
- x4=`F4`;
- y=`EQNCODE`;
- return(`FY`);
- }
- !!C!!
- ~~C~~
-