void exec(const char *str, char *err) { // this function executes a given instruction. // all important functions are called from here. void evali(const char *str, char *err, int *eval); char asgn[ID]=""; /* assignment variable name container*/ strcpy(err,""); /* empty error container string */ int integer=0; /* integer to store integer evaluation results */ double real=0; /* double to store double evaluation results */ if(prechar(asgn,str,"=")=='=') /* check for = operator */ { if(strtrims(asgn,' ')) { // if assignment string is empty strcpy(err,"Assignment variable is not specified."); return; } char isid = id_check(asgn); if(!isid) { // assign *************************************************** printf("\nassgin: %s\n",asgn); return; } else /* is not an identifier */ { if(isid==1) /* var name starts with a number */ { strcpy(err,"Variable name cannot start with a number."); return; } else /* var name has illegal chars*/ { stradd(err,"Variable cannot contain ["); straddc(err,isid); stradd(err,"] character"); } return; } } else /* there is no assigment involved */ { // if there is no assignment then integer is considered // to be the default assignment data type for evaluation. evali(str,err,&integer); if(!strlen(err)) { printf("\nint: %d\n",integer); return; } else { return; } } return; }
static void ploti(const char *line ) { int t, tt, g, n, h, S, E; plotv *y, *x, *dx, *dy; forecast r; if ( (n=sscanf( line, "%*s %d %d %d", &g, &S, &E ))!=EOF ) { if ( n == 1 ) { S = 0; E = datalen( DATA_TARGET ); } else { S = max( 0, S ); E = min( E, datalen( DATA_TARGET ) ); } if ( 0 < g && g <= status.curgen ) { g--; n = E - S; x = ymalloc( status.length * n * sizeof(plotv) ); y = ymalloc( status.length * n * sizeof(plotv) ); dx = ymalloc( n * sizeof(plotv) ); dy = ymalloc( n * sizeof(plotv) ); tt = 0; for ( t = S; t < E; t++ ) { dy[t-S] = getdata( DATA_TARGET, t ); dx[t-S] = t; r = evali( run[g].besti, t ); for ( h = 0; h < status.length; h++ ) { x[tt] = t + h + status.horizon; y[tt] = r.v[h]; tt++; } } gp_plotarray( port, GP_PLOT, GP_TITLE "\"data\"" GP_DOTS, dx, dy, n ); gp_plotarray( port, GP_REPLOT, GP_TITLE "\"forecast\"" GP_DOTS, x, y, n * status.length ); yfree( x ); yfree( y ); yfree( dx ); yfree( dy ); } else fprintf( stderr, "individual must lie in [1,%d]\n", status.curgen ); } else p_error( "no individual" ); }
void evali(const char *str, char *err, int *eval) { // Mathematical Expression Evaluation Function (INT) // --------------------------------------------------- // This functions solves mathematical expressions // based on integer numbers. When a complex expression // is passed via *str, the expression is broken // into parts, and the simplest part in accordance // with the BODMAS theorum is passed on recursively // onto the function itself. This recurvise process is // repeated until the whole expression has been solved. // Results of the evaluation are stored in int *eval. if((*str=='\0')||(strlen(str)==0)) { strcpy(err,"Void Expression"); return; } int i = 0; /* common loop counter */ char bcheck = 0; for(i=0;*str!='\0';str++,i++) { /* check if there are brackets in an equation. */ if(*str=='('||*str==')') /* bcheck is 0 if there are no brackets & vice-versa. */ { bcheck = 1; break; } } for(;i>0;i--) str--; /* reverse str pointer to original position */ if(!bcheck) { /* this is the core of eval where the calculations are done. at this level, the equation does not have any brackets. */ const char symbols[]="^*/%+-&"; const char se[]="Invalid Syntax"; const char ue[]="Unknown Symbol"; char *tmp = NULL; if((tmp=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *tmp='\0'; /* check wether str has reached the absolute stage */ if(prechar(tmp,str,symbols)==0) { /* char to store num_check results */ char ncheck = 0; ncheck = num_check(str); if(!ncheck) { /* str is a number (real or integer) */ *eval = atoi(str); free(tmp); tmp = NULL; return; } else if(!id_check(str)) { free(tmp); tmp = NULL; // char *vals = NULL; // vals = valstr(str); // *eval = atoi(vals); // free(vals); // vals = NULL; *eval = 0; return; } else { /* not a variable, number nor function; hence an error */ strcpy(err,se); strcat(err,": "); strcat(err,str); free(tmp); tmp = NULL; return; } } else /* there are symbols in str */ { free(tmp); tmp = NULL; } /* now the real maths */ char *pre = NULL; /* string preceding the operator */ char *pos = NULL; /* string succeding the operator */ /* now allocate the variables */ if((pre=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *pre='\0'; if((pos=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *pos='\0'; char symbol = 0; if(prechar(pre,str,"^")) { if(postchar(pos,str,"^")) symbol = '^'; } else if(prechar(pre,str,"*")) { if(postchar(pos,str,"*")) symbol = '*'; } else if(prechar(pre,str,"/")) { if(postchar(pos,str,"/")) symbol = '/'; } else if(prechar(pre,str,"%")) { if(postchar(pos,str,"%")) symbol = '%'; } else if(prechar(pre,str,"+")) { if(postchar(pos,str,"+")) symbol = '+'; } else if(prechar(pre,str,"-")) { if(postchar(pos,str,"-")) symbol = '-'; } else if(prechar(pre,str,"&")) { if(postchar(pos,str,"&")) symbol = '&'; } else { strcpy(err,ue); strcat(err,": "); *++err=symbol; *++err='\0'; return; } char *ax = NULL; /* value preceding of operator */ char *bx = NULL; /* value succeding the operator */ char *cx = NULL; /* value of ax and bx processed */ /* now allocate ax and bx */ if((ax=(char *)(malloc(sizeof(char)* (strlen(pre)+1))))==NULL) allocerr(); *ax='\0'; if((bx=(char *)(malloc(sizeof(char)* (strlen(pos)+1))))==NULL) allocerr(); *bx='\0'; /* find out the contents of bx */ char *ebx = NULL; /* temp string to build bx */ if((ebx=(char *)(malloc(sizeof(char)* (strlen(pos)+1))))==NULL) allocerr(); *ebx='\0'; strcpy(bx,pos); strcpy(ebx,bx); for(;;) /* infinite loop */ { if(!prechar(bx,ebx,symbols)) { strcpy(bx,ebx); free(ebx); ebx = NULL; /* de-allocate ebx */ break; } else /* here ebx is build */ strcpy(ebx,bx); } /* find out the contents of ax */ char *eax = NULL; /* temp string to build ax */ if((eax=(char *)(malloc(sizeof(char)* (strlen(pre)+1))))==NULL) allocerr(); *eax='\0'; strcpy(ax,pre); strcpy(eax,ax); for(;;) /* infinite loop */ { if(!postchar(ax,eax,symbols)) { strcpy(ax,eax); free(eax); eax = NULL; /* de-allocate eax */ break; } else /* here eax is build */ strcpy(eax,ax); } /* variables to store (pre-ax) and (pre-bx) */ char *prex = NULL; /* string of (pre-ax) */ char *posx = NULL; /* string of (pos-ax) */ /* now allocate prex and posx */ if((prex=(char *)(malloc(sizeof(char)* (strlen(pre)+1))))==NULL) allocerr(); *prex='\0'; if((posx=(char *)(malloc(sizeof(char)* (strlen(pos)+1))))==NULL) allocerr(); *posx='\0'; /* find prex and posx */ strlft(prex,pre,(strlen(pre)-strlen(ax))); strrht(posx,pos,(strlen(pos)-strlen(bx))); /* de-allocate pre & pos */ free(pre); pre = NULL; free(pos); pos = NULL; /* process ax and bx to find cx */ // printf("\nax=%s",ax); // printf("\nbx=%s",bx); int evala=0; int evalb=0; int evalc=0; if(num_check(ax)) { // tmp = valstr(ax); // if((cp=(char *)(realloc(ax,sizeof(char)* // (strlen(tmp)+1))))!=NULL) { ax = cp; } // else { allocerr(); } // strcpy(ax,tmp); // free(tmp); // tmp = NULL; strcpy(ax,""); } if(num_check(bx)) { // tmp = valstr(bx); // if((cp=(char *)(realloc(bx,sizeof(char)* // (strlen(tmp)+1))))!=NULL) { bx = cp; } // else { allocerr(); } // strcpy(bx,tmp); // free(tmp); // tmp = NULL; strcpy(bx,""); } evala = atoi(ax); evalb = atoi(bx); switch(symbol) { case '^': /* power */ evalc = (int)(pow(evala,evalb)); break; case '*': /* multiplication */ evalc = evala * evalb; break; case '/': /* division */ evalc = evala / evalb; break; case '%': /* modulus */ evalc = evala % evalb; break; case '+': /* addition */ evalc = evala + evalb; break; case '-': /* subtraction */ evalc = evala - evalb; break; case '&': /* concatenation */ if((cp=(char *)(realloc(cx,sizeof(char)* (strlen(ax)+strlen(bx)+1))))!=NULL) { cx = cp; } else { allocerr(); } strcpy(cx,ax); strcat(cx,bx); evalc = atoi(cx); break; default: strcpy(err,ue); strcat(err,": "); *++err=symbol; *++err='\0'; return; } /* find the length of evalc */ int cxlen = 8; if(evalc<0) { cxlen = 16; } else if(evalc==0) { cxlen = 2; } else { cxlen = 0; int ctmp = evalc; while(ctmp>0) { ctmp/=10; cxlen++; } } /* allocate cx having the length of evalc */ if((cx=(char *)(malloc(sizeof(char)* ((cxlen)+1))))==NULL) allocerr(); *cx='\0'; itoa(evalc,cx,10); // printf("\ncx=%s\n",cx); /* de-allocate ax & bx */ free(ax); ax = NULL; free(bx); bx = NULL; /* variable to store one-step solved equation */ char *ex = NULL; if((ex=(char *)(malloc(sizeof(char)* (strlen(prex)+strlen(cx)+strlen(posx)+1))))==NULL) allocerr(); *ex='\0'; /* find ex using cx in prex and posx */ strcpy(ex,prex); strcat(ex,cx); strcat(ex,posx); /* now de-allocate cx, prex & posx */ free(cx); cx = NULL; free(prex); prex = NULL; free(posx); posx = NULL; /* recurse ex on eval for next-step solving */ int integer=0; evali(ex,err,&integer); *eval = integer; /* de-allocate ex & return */ free(ex); ex = NULL; return; } else { // check if an equation is correct in no. of brackets. unsigned int brackets[2]={0,0}; for(i=0;*str!='\0';str++,i++) { /* find the no. of brackets */ if(*str=='(') brackets[0]+=1; if(*str==')') brackets[1]+=1; } for(;i>0;i--) str--; /* reverse str pointer to original position */ if(brackets[0]==brackets[1]) { char *pres = NULL; char *poss = NULL; char *mids = NULL; char *midx = NULL; char *newx = NULL; /* now allocate the above string */ if((pres=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *pres='\0'; if((poss=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *poss='\0'; if((mids=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *mids='\0'; if((midx=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *midx='\0'; if((newx=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *newx='\0'; /* now split the string */ unsigned long int cs = 0; unsigned long int bcount = 0; /* make pres */ for(cs=0;*str!='\0';cs++) { if(*str=='(') bcount++; if(bcount==brackets[0]) break; *pres++ = *str++; } for(*pres = '\0';cs>0;cs--) pres--; /* make mids */ for(cs=0,str++;((*str!=')')&&(*str!='\0'));cs++) *mids++ = *str++; for(*mids = '\0';cs>0;cs--) mids--; /* make poss */ for(cs=0,str++;*str!='\0';cs++) *poss++ = *str++; for(*poss = '\0';cs>0;cs--) poss--; /* ok, now evaluate */ int integer = 0; evali(mids,err,&integer); itoa(integer,midx,10); strcpy(newx,pres); strcat(newx,midx); strcat(newx,poss); evali(newx,err,&integer); *eval = integer; return; } else { strcpy(err,"Invalid Equation, inequal number of brackets."); return; } } }
void exec(const char *str, char *err) { // this function executes a given instruction. // all important functions are done from here. int eval(const char *str); void * evals(const char *str, const char data_type); strcpy(err,""); /* empty error container string */ char *exp = NULL; /* string containing evaluation expression */ char *asgn = NULL; /* string containing assignment variable name */ char data_type = '\0'; /* data type of variable to evaluate */ /* assignment analysis & error handling */ if((asgn=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *asgn='\0'; if(prechar(asgn,str,"=")=='=') /* check for = operator */ { /* ok, it is a variable assignment */ strtrm(asgn,' ','S'); if((*asgn==0)||(strlen(asgn)==0)) { /* error: assignment string is empty */ strcpy(err,"Assignment variable is not specified."); free(asgn); return; } else /* assignment string is NOT empty */ { char isid[2] = {'\0','\0'}; /* check identifier validity*/ isid[0] = id_check(asgn); if(isid[0]!=0) /* error: is not an identifier */ { if(isid[0]==1) /* error: var name starts with a number */ { strcpy(err,"Variable name cannot start with a number."); } else if(isid[0]==32) /* error: var name contains spaces */ { strcpy(err,"Variable name cannot contain spaces."); } else /* error: var name has illegal chars*/ { strcpy(err,"Variable name cannot contain ["); strcat(err,isid); strcat(err,"] character."); } free(asgn); return; } else /* success: assignment string is valid */ { /* allocate string exp */ if((exp=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *exp='\0'; /* now split the equation to get the exp. */ postchar(exp,str,"="); strtrm(exp,' ','S'); // find out the data type of the assignment // variable & store it in char data_type } } } else /* there is no assigment involved */ { /* allocate string exp */ if((exp=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *exp='\0'; strcpy(exp,str); strtrm(exp,' ','S'); /* de-allocate asgn */ free(asgn); asgn = NULL; } /* variable assignment */ if(asgn!=NULL) { /* possible variable data types */ long *i = NULL; // <- (int) short *n = NULL; // <- (int-) char *c = NULL; // <- (chr) double *d = NULL; // <- (real) float *f = NULL; // <- (real-) unsigned long *j = NULL; // <- (int+) unsigned short *m = NULL; // <- (int-+) unsigned char *a = NULL; // <- (chr+) switch(data_type) { case 'i': break; default: strcpy(err,"Fatal Data Transfer Error on ["); strcat(err,asgn); strcat(err,"] variable."); } /* de-allocate memory of data type */ switch(data_type) { case 'i': free(i); i = NULL; break; case 'n': free(n); n = NULL; break; case 'c': free(c); c = NULL; break; case 'd': free(d); d = NULL; break; case 'f': free(f); f = NULL; break; case 'j': free(j); j = NULL; break; case 'm': free(m); m = NULL; break; case 'a': free(a); a = NULL; break; } /* de-allocate asgn */ free(asgn); asgn = NULL; } else /* no assigment */ { // if there is no assignment then integer is considered // to be the default assignment data type for evaluation. puts(""); int _i=0; _i = eval(exp); printf("eval: %d\n",_i); data_type = 'i'; long *i = NULL; i=(long *)(evals(exp,data_type)); printf("evals: %ld\n",*i); free(i); i = NULL; void evali(const char *str, char *err, int *eval); evali(exp,err,&_i); printf("evali: %d\n",_i); printf("evali was invoked %d time(s).\n",evali_count); evali_count = 0; } free(exp); exp = NULL; }