/* ******************************************************************* * * * Copyright (c) L-DGO/MIT/JGOFS * * * * * * File : mathex.c * * * * Purpose : * * * * Version Number : 1.1 * * * * Revision History : * * * * Date Developer * * ---- --------- * * * * Sat Oct 17 1992 10.00 Glenn Flierl * * * * * ******************************************************************* */ /* P-code form: 128..255 push variable j-128 [negative #'s on Sun] 1: ascii number :2 push constant 3: quote next variable s,c,e,l,'1','i': prefix ops =,+,-,*,/,^: infix ops 0: end */ #include #include #include #include double atof(); double strtod(); #define TOKEN 40 #define NPREFIX 6 #define PRIORITY " $ (), = +-*/^ " static char prefix[NPREFIX][TOKEN]={"sin","cos","exp","ln","log10","int"}; static char prefixcode[NPREFIX][2]={"s","c","e","l","1","i"}; int parsex(s,names,ntotal) char *s,names[][TOKEN]; int *ntotal; { int opstkp; float f; char opstack[20][TOKEN]; int opprior[20]; char *t,*t1,tok[TOKEN],tmp[255]; int i,j,k,lookop,tmpc; /* printf("%s\n",s); */ tmpc=0; tmp[0]='\0'; opstkp= 0; opstack[0][0]=0; opprior[0]=0; lookop=0; strcat(s,"$"); t=s; while(t != NULL) { if(isalpha(*t)){ i=strcspn(t,"+-*^/()$="); strncpy(tok,t,i); tok[i]='\0'; t += i; if(lookop){ sprintf(s,"Looking for operator, found %s\n",tok); return 1; } else { for(i=0;i127){ stack[++stkp]=conv(values[i-128]); #else if (i<0){ stack[++stkp]=conv(values[128+i]); #endif } else switch(*sp){ case 1:stack[++stkp]=(float)strtod(sp+1,&t);sp=t;break; case 3:stack[++stkp]=(float)*(++sp);break; case 's':if (oneop(stack[stkp]))stack[stkp]=sin(stack[stkp]);break; case 'c':if (oneop(stack[stkp]))stack[stkp]=cos(stack[stkp]);break; case 'e':if (oneop(stack[stkp]))stack[stkp]=exp(stack[stkp]);break; case 'l':if (oneop(stack[stkp]))stack[stkp]=log(stack[stkp]);break; case '1':if (oneop(stack[stkp]))stack[stkp]=log10(stack[stkp]);break; #if IBM case 'i':if (oneop(stack[stkp]))stack[stkp]=trunc(stack[stkp]);break; #elif HP || SOL case 'i':if (oneop(stack[stkp]))i=stack[stkp];stack[stkp]=i;break; #else case 'i':if (oneop(stack[stkp]))stack[stkp]=aint(stack[stkp]);break; #endif case '#':if (oneop(stack[stkp]))stack[stkp]= -stack[stkp];break; #if IRIX || IBM case '=':if(oneop(stack[stkp])) {stkp--; sprintf(values[(int)stack[stkp]-128],"%f",stack[stkp+1]); stkp--;} else {stkp -= 2;strcpy(values[(int)stack[stkp+1]-128],"nd");}; break; #else case '=':if(oneop(stack[stkp])) {stkp--; sprintf(values[128+(int)stack[stkp]],"%f",stack[stkp+1]); stkp--;} else {stkp -= 2;strcpy(values[128+(int)stack[stkp+1]],"nd");}; break; #endif case '+':stkp--;if(twoop(stack[stkp],stack[stkp+1])) stack[stkp]=stack[stkp]+stack[stkp+1];else stack[stkp]= -9999.0;break; case '-':stkp--;if(twoop(stack[stkp],stack[stkp+1])) stack[stkp]=stack[stkp]-stack[stkp+1];else stack[stkp]= -9999.0;break; case '*':stkp--;if(twoop(stack[stkp],stack[stkp+1])) stack[stkp]=stack[stkp]*stack[stkp+1];else stack[stkp]= -9999.0;break; case '/':stkp--;if(twoop(stack[stkp],stack[stkp+1])) stack[stkp]=stack[stkp]/stack[stkp+1];else stack[stkp]= -9999.0;break; case '^':stkp--;if(twoop(stack[stkp],stack[stkp+1])) {if(stack[stkp]>0)stack[stkp]=pow(stack[stkp],stack[stkp+1]); else if(stack[stkp]==0)stack[stkp]=0.0; else stack[stkp]= -pow(-stack[stkp],stack[stkp+1]); } else stack[stkp]= -9999.0; break; }; sp++; /* for(i=0;i<=stkp;i++)printf("%f ",stack[i]); */ /* printf("\n"); */ }; return 0; } /* main(argc,argv) int argc; char *argv[]; { static char names[][TOKEN]={"press","temp","sal","",""}; static char values[][TOKEN]={"100","12.55","33.462","",""}; int ntotal=3; char tmp[255]; int i; strcpy(tmp,argv[1]); parse(tmp,names,&ntotal); for (i=0;i