Asked by:
MathParser

General discussion
-
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
// using Dilma's algorithm from www.hiasm.com
#define TokName 1
#define TokReal 2
#define TokNumber 3
#define TokSymbol 4
#define TokMath 5
#define TokEnd 6
#define TokHex 7double pi=3.14159265358979;//32384626433832795;
double digE = 2.718281828459045;
typedef struct {
char Line[1024];
char Token[1024];
int TokType;
int LPos;
int Err;
double FResult;
double FDataCount;
int Flags [10];
int Xnumb;
double ArrayX [200];
} TheData;
int FDataCount=4;
int DataCount=4;
double FResult;
double FDefault=0;
double Default=0;
double AngleMode=1;
void Level0(TheData *p , double *x ) ;
void Level1a(TheData *p , double *x ) ;
void Level1b(TheData *p , double *x) ;
void Level2 (TheData *p , double *x) ;
void Level3 (TheData *p , double *x) ;
void Level4 (TheData *p , double *x) ;
void Level5 (TheData *p , double *x) ;
void Level6 (TheData *p , double *x ) ;void SetAngleMode ( int Value)
{
if (Value==0)
AngleMode =1;
else
AngleMode =pi/180;
return;
}void doInputVars(TheData *p)
{
int ncounts,i;
double x;
printf ("\n Inpun number of counts ");
scanf("%d",&ncounts);
p->FDataCount=ncounts;
for (i=0;i<ncounts;i++)
{
printf ("\n Inpun variable # %d : ",i);
scanf("%lf",&x);p->ArrayX[i]=x;
}
return;
}
void doSetVars(TheData *p, int ncounts, double Argums[200])
{
int i;
double x;
printf ("\n Inpun number of counts ");
scanf("%d",&ncounts);
p->FDataCount=ncounts;
for (i=0;i<ncounts;i++)
{
p->ArrayX[i]=Argums[200];
}
return;
}
void doInputMathStr(TheData *p )
{
int i;char SafeLine [1024];
char string1[1024];
printf("\n Input math string ");
scanf("%s",string1);for (i=0;i<strlen(string1) ;i++) p->Line[i]=string1[i];
p->Line[strlen(p->Line)]='\x01';
for (i=0;i<strlen(p->Line);i++) SafeLine[i]=p->Line[i];
for (i=0;i<strlen(p->Line);i++)
{
if ((p->Line[i]=='\n')||(p->Line[i]=='\r'))
p->Line[i]=' ';
}return;
}
void doSetMathStr( char string1[1024], TheData *p )
{
int i;
char SafeLine [1024];
for (i=0;i<strlen(string1);i++) p->Line[i]=string1[i];p->Line[strlen(p->Line)]='\x01';
for (i=0;i<strlen(p->Line);i++) SafeLine[i]=p->Line[i];
for (i=0;i<strlen(p->Line);i++)
{
if ((p->Line[i]=='\n')||(p->Line[i]=='\r'))
p->Line[i]=' ';
}return;
}void doClear(TheData *p)
{
p->FResult=FDefault;
p->LPos=0;
p->Err =-1;
return;
}void doCalc(TheData *p, double *y, int *Error )
{
*Error=0;
double Y;
doClear(p) ;
Level0(p, &Y);
if (p->Err < 0)
{
p->LPos=0;
*y=Y;
}
else
{
p->LPos--;
*y=0;
*Error=p->Err;
}
return;
}void PrintToken( TheData *p )
{
int i;
printf ("\n" );
for (i=0;i<strlen(p->Token);i++) printf (" %c",p->Token[i]);
printf ("\n" );
return;
}/* ************parser*************** */
void GetToken( TheData *p )
{
int i;
for( i=0;i<1024;i++) p->Token[i]='\0';
p->TokType=0;
while ((p->Line[p->LPos]==' ' )||(p->Line[p->LPos]=='\t') ) p->LPos++;
switch (p->Line[p->LPos])
{
case 'a' ... 'z':
case 'A' ... 'Z':
case '_':
// case 'a' ... 'y':
// case 'A' ... '?':
while(isalnum(p->Line[p->LPos])||(p->Line[p->LPos]=='_'))
{
/*strappen*/
i=strlen(p->Token);
if (p->Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
for(i=0;i<strlen(p->Token); i++)
{
p->Token[i]=tolower(p->Token[i]);
}
p->TokType=TokName;
break;Saturday, February 21, 2015 8:43 PM
All replies
-
case '0'...'9':
case '.':
case '$':
if ((p-> Line[p->LPos]=='$')||((p-> Line[p->LPos]=='0')&&(p->Line[p->LPos+1]=='x')) )
{
if(p-> Line[p->LPos]!='$') p-> LPos++;
p-> LPos++;while (isxdigit (p-> Line[p->LPos]))
{ /*append */
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
if(p->Token[0]!='\0')
{
p->TokType= TokHex;
break;
}
}while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
p->TokType=TokNumber;
if (p-> Line[p->LPos]=='.')
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]='.';
i++;
}
p->Token[i]='\0';
p-> LPos++;
while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
if (!(p->Token[0]!='.'))
{
p->TokType=0;
break;
}
p->TokType=TokReal;
}
while( (p-> Line[p->LPos]==' ')||(p-> Line[p->LPos]=='\t') )p-> LPos++;
if ((p-> Line[p->LPos]=='e')||(p-> Line[p->LPos]=='E'))
{
/*append string*/
printf("\n Exp ");
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0' )
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
while ((p-> Line[p->LPos] ==' ')|| (p-> Line[p->LPos]=='\t')) p-> LPos++;if((p-> Line[p->LPos]=='+')||(p-> Line[p->LPos]=='-'))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
while ((p-> Line[p->LPos] ==' ')||( p-> Line[p->LPos] =='\t'))p-> LPos++;
}
if (!(isdigit(p-> Line[p->LPos])))
{
p->TokType=0;
break;
}
while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
p->TokType=TokReal;
}
break;
case '(':
case ')':
case '%':
case ',':
case '[':
case ']':
p->Token[0]=p-> Line[p->LPos];
p-> LPos++;
p->TokType=TokSymbol;
break;
case '<':
case '>':
case '=':
p->Token[0]=p-> Line[p->LPos];
p-> LPos++;
p->TokType=TokSymbol;
if((p->Token[0] == '<')&&(p-> Line[p->LPos]=='='))
{
p->Token[0]='{';
p-> LPos++;
}
else
{
if((p->Token[0]=='>')&&(p-> Line[p->LPos]=='='))
{
p->Token[0]='}';
p-> LPos++;
}
}
break;
case '+':
case '-':
case '/':
case '*':
case '^':
p->Token[0] = p-> Line[p->LPos];
p-> LPos++;
while ((p-> Line[p->LPos] == ' ')||(p-> Line[p->LPos]=='\t')) p-> LPos++;
if (!((p-> Line[p->LPos] =='+')||(p-> Line[p->LPos]=='-')))
{
p->TokType=TokMath;
}
else
{
p->Token[0] = '\0';
}
break;
case '\x01':
p->TokType=TokEnd;
break;
}return;
}Saturday, February 21, 2015 8:43 PM -
case '0'...'9':
case '.':
case '$':
if ((p-> Line[p->LPos]=='$')||((p-> Line[p->LPos]=='0')&&(p->Line[p->LPos+1]=='x')) )
{
if(p-> Line[p->LPos]!='$') p-> LPos++;
p-> LPos++;while (isxdigit (p-> Line[p->LPos]))
{ /*append */
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
if(p->Token[0]!='\0')
{
p->TokType= TokHex;
break;
}
}while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
p->TokType=TokNumber;
if (p-> Line[p->LPos]=='.')
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]='.';
i++;
}
p->Token[i]='\0';
p-> LPos++;
while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
if (!(p->Token[0]!='.'))
{
p->TokType=0;
break;
}
p->TokType=TokReal;
}
while( (p-> Line[p->LPos]==' ')||(p-> Line[p->LPos]=='\t') )p-> LPos++;
if ((p-> Line[p->LPos]=='e')||(p-> Line[p->LPos]=='E'))
{
/*append string*/
printf("\n Exp ");
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0' )
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
while ((p-> Line[p->LPos] ==' ')|| (p-> Line[p->LPos]=='\t')) p-> LPos++;if((p-> Line[p->LPos]=='+')||(p-> Line[p->LPos]=='-'))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
while ((p-> Line[p->LPos] ==' ')||( p-> Line[p->LPos] =='\t'))p-> LPos++;
}
if (!(isdigit(p-> Line[p->LPos])))
{
p->TokType=0;
break;
}
while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
p->TokType=TokReal;
}
break;Saturday, February 21, 2015 8:46 PM -
case '0'...'9':
case '.':
case '$':
if ((p-> Line[p->LPos]=='$')||((p-> Line[p->LPos]=='0')&&(p->Line[p->LPos+1]=='x')) )
{
if(p-> Line[p->LPos]!='$') p-> LPos++;
p-> LPos++;while (isxdigit (p-> Line[p->LPos]))
{ /*append */
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
if(p->Token[0]!='\0')
{
p->TokType= TokHex;
break;
}
}while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}Saturday, February 21, 2015 8:46 PM -
p->TokType=TokNumber;
if (p-> Line[p->LPos]=='.')
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]='.';
i++;
}
p->Token[i]='\0';
p-> LPos++;
while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
if (!(p->Token[0]!='.'))
{
p->TokType=0;
break;
}
p->TokType=TokReal;
}
while( (p-> Line[p->LPos]==' ')||(p-> Line[p->LPos]=='\t') )p-> LPos++;
if ((p-> Line[p->LPos]=='e')||(p-> Line[p->LPos]=='E'))
{
/*append string*/
printf("\n Exp ");
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0' )
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
while ((p-> Line[p->LPos] ==' ')|| (p-> Line[p->LPos]=='\t')) p-> LPos++;if((p-> Line[p->LPos]=='+')||(p-> Line[p->LPos]=='-'))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
while ((p-> Line[p->LPos] ==' ')||( p-> Line[p->LPos] =='\t'))p-> LPos++;
}
if (!(isdigit(p-> Line[p->LPos])))
{
p->TokType=0;
break;
}
while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
p->TokType=TokReal;
}
break;Saturday, February 21, 2015 8:46 PM -
case '(':
case ')':
case '%':
case ',':
case '[':
case ']':
p->Token[0]=p-> Line[p->LPos];
p-> LPos++;
p->TokType=TokSymbol;
break;
case '<':
case '>':
case '=':
p->Token[0]=p-> Line[p->LPos];
p-> LPos++;
p->TokType=TokSymbol;
if((p->Token[0] == '<')&&(p-> Line[p->LPos]=='='))
{
p->Token[0]='{';
p-> LPos++;
}
else
{
if((p->Token[0]=='>')&&(p-> Line[p->LPos]=='='))
{
p->Token[0]='}';
p-> LPos++;
}
}
break;
case '+':
case '-':
case '/':
case '*':
case '^':
p->Token[0] = p-> Line[p->LPos];
p-> LPos++;
while ((p-> Line[p->LPos] == ' ')||(p-> Line[p->LPos]=='\t')) p-> LPos++;
if (!((p-> Line[p->LPos] =='+')||(p-> Line[p->LPos]=='-')))
{
p->TokType=TokMath;
}
else
{
p->Token[0] = '\0';
}
break;
case '\x01':
p->TokType=TokEnd;
break;
}return;
}Saturday, February 21, 2015 8:47 PM -
void Level0( TheData *p, double *x)
{
int i;
for (i=0 ; i<p->FDataCount;i++) p->Flags[i]=0;
if (p->LPos>=0)
{
p->FResult=FDefault;
p->LPos= 0; //1
p->Err=-1;
// printf ("\n Level 0 x=%lf ", *x);
GetToken(p);
// PrintToken(p ); // 1
Level1a(p,x);
if (p->Err>=0)
return;
if (p->TokType!=TokEnd) p->Err =0;
}
return;
}void Level1a(TheData *p , double *x ) // < > <= >= = //
{
double y;
char op ;
double x2 ;
int b ;
Level1b(p, x);
// printf ("\n Level 1a (afterLevel1b) *x=%lf",*x);
// PrintToken(p);
if (p->Err>=0) return;
while ((p->Token[0] == '<')||(p->Token[0] == '>')||(p->Token[0] == '{')||(p->Token[0] == '}')||(p->Token[0] == '='))
{
op=p->Token[0];
// printf ("\n Level 1a op=%c \n",op);
GetToken(p);
// PrintToken(p);
Level1b(p, &x2);
// printf ("\n Level 1a x2=%lf",x2);
if (p->Err>=0)
return;
b=0;
switch (op)
{
case '<':
if(*x<x2) b=1;
break;
case '>':
if(*x>x2) b=1;
break;
case '{':
if(*x<=x2) b=1;
break;
case '}':
if(*x>=x2) b=1;
break;
case '=':
if(*x==x2) b=1;
break;
default : b=0;
break;
}if (b==1) { *x= 1; }
else
{ *x= 0; }
}
return;
}
void Level1b(TheData *p , double *x) // + -
{
//printf ("\n Level 1b x=%lf\n",*x);
char op ;
double x2 ;
Level2(p, x);
// printf ("\n Level 1b x=%lf\n",*x);
//printf ("\n Level 1b x2=%lf\n ",x2);
if (p->Err>=0)
return;while ((p->Token[0]=='-')||(p->Token[0]=='+'))
{
op = p->Token[0];
//printf ("\n Level 1b op=%c \n",op);
GetToken(p);
// PrintToken(p);
Level2(p,&x2);
// printf ("\n Level 1b x2=%lf\n",x2);
if (p->Err>=0)
return;
if (op=='+') { *x=*x+x2; }
else *x=*x-x2;
// printf ("\n Level 1b x=%lf\n",*x);
}
return;
}void Level2 ( TheData *p , double *x) /* / * */
{
char op;
double x2;Level3(p,x);
// printf ("\n Level 2, x=%lf\n ",*x ); //x=10
if (p->Err>=0) return;
//Token[0]=*
while ((p->Token[0]=='/')||(p->Token[0]=='*') ||(strcmp(p->Token, "div")==0)||(strcmp(p->Token, "mod")==0))
{
op=p->Token[0]; // *
GetToken(p); // Token of 2nd arg-cos
// PrintToken(p);
Level3(p, &x2); //
//printf ("\n Level 2, x2=%lf\n ",x2 );//arg 2 0
if (p->Err>=0) return;
p->Err=1;
switch (op)
{
case '*':
*x = *x*x2;
break;
case '/':
if (x2==0)
return;
else
*x=*x/x2;
break;
/*
case 'd':
if (round(x2))=0
return
else
x=round(x)%round(x2);
break;
*/
default :
if (round(x2)==0) { return; }
else *x=fmod(round(*x), round(x2));
break;
}
p->Err =-1;
return;
}
return;
}void Level3( TheData *p , double *x) // ^
{
double x2;Level4(p,x);
if (p->Err>=0)
return;
while(p->Token[0]=='^')
{
GetToken(p);
Level4(p,&x2);
if (p->Err>=0) return;
if ((round(x2)!=x2)&&(*x<=0)) { p->Err=1; }
else *x=pow(*x, x2);
}
return;
}Saturday, February 21, 2015 8:47 PM -
void Level4( TheData *p , double *x) // unar + - not //
{
//printf ("\n Level 4, x=%lf\n ",*x );
char op;
op=' ';
if ((p->Token[0] =='-')||(p->Token[0] == '+'))
{
op= p->Token[0];
GetToken(p);
// PrintToken(p);
}
Level5(p,x);
//PrintToken(p);
if (op =='-') *x=-(*x);
return;
}
void Level5(TheData *p , double *x) // and or xor //
{
char op ;
double x2;
int b ;
//printf ("\n Level 5, x=%lf",*x );
// ( function
Level6(p, x); // parsing %1,%2,%3 data and functions
//PrintToken(p);
//printf ("\n Level 5(after Level 6), x=%lf \n",*x );
if (p->Err>=0) return;
p->Err=-1;
return;
}
int hextoint(char buffer[])
{
return 0; //atoi (buffer);
}void Level6( TheData *p , double *x ) // ( function
{
int i,j ,k;
double Y=0;
double Arr[200];
double Mtx [200];
double Args[200];
// printf ("\n Level 6 x=%lf\n",*x);
if (p->Token[0] == '%')
{
GetToken(p); //
if (p->TokType==TokNumber)
{
i= atoi(p->Token)-1;
if (i<p->FDataCount) //
{
GetToken(p); // node number
{
if (p->Flags[i]==1) { *x=Args[i]; }
else
{
Args[i]=p->ArrayX[i];
*x=Args[i];
p->Flags[i]=1;
// printf ("\n i= %d Args [i]=%lf p->Flags[i]=%d ",i, Args[i],p->Flags[i]);
}
return;
}// end if token =(-else
}
else { p->Err=0; return; }
} //end If tokNumber
else { p->Err=0; return; }
}//end if %
else
if (p->Token[0]== '(')
{
GetToken(p); //get arg in the ()
// PrintToken(p);
Level1a(p,x); // PrintToken(p);
if (p->Err>=0) return;
if (p->Token[0]!= ')')
{
p->Err=0; return;
}
}
else
switch (p->TokType)
{
case TokName:
{ //Begin TokName
// printf ("\n p->TokType =TokName ");
// PrintToken(p);
if (strcmp(p->Token, "pi")==0 ) { *x = pi; }
else if (strcmp(p->Token, "e")==0 ) { *x = digE; }
else if (strcmp(p->Token, "cos")==0 ) { ReadFunc(p, x,1); }
else if (strcmp(p->Token, "sin")==0 ) { ReadFunc(p,x,2); }
else if (strcmp(p->Token, "tg")==0 ) { ReadFunc(p,x,3); }
else if (strcmp(p->Token, "ctg")==0 ) { ReadFunc(p,x,4); }
else if (strcmp(p->Token, "arccos")==0 ) { ReadFunc(p,x,5); }
else if (strcmp(p->Token, "arcsin")==0 ) { ReadFunc(p,x,6); }
else if (strcmp(p->Token, "arctg")==0 ) { ReadFunc(p,x,7); }
else if (strcmp(p->Token, "arcctg")==0 ) { ReadFunc(p,x,8); }
else if (strcmp(p->Token, "atan")==0 ) { ReadFunc(p,x,9); }
else if (strcmp(p->Token, "ch")==0 ) { ReadFunc(p,x,10); }
else if (strcmp(p->Token, "sh")==0 ) { ReadFunc(p,x,11); }
else if (strcmp(p->Token, "th")==0 ) { ReadFunc(p,x,12); }
else if (strcmp(p->Token, "cth")==0 ) { ReadFunc(p,x,13); }
else if (strcmp(p->Token, "arcch")==0 ) { ReadFunc(p,x,14); }
else if (strcmp(p->Token, "arcsh")==0 ) { ReadFunc(p,x,15); }
else if (strcmp(p->Token, "arcth")==0 ) { ReadFunc(p,x,16); }
else if (strcmp(p->Token, "arccth")==0 ) { ReadFunc(p,x,17); }
else if (strcmp(p->Token, "log")==0 ) { ReadFunc(p,x,18); }
else if (strcmp(p->Token, "lg")==0 ) { ReadFunc(p,x,19); }
else if (strcmp(p->Token, "ln") ==0 ) { ReadFunc(p,x,20); }
else if (strcmp(p->Token, "exp")==0 ) { ReadFunc(p,x,21); }
else if (strcmp(p->Token, "sqr") ==0 ) { ReadFunc(p,x,22); }
else if (strcmp(p->Token, "sqrt")==0 ) { ReadFunc(p,x,23); }
else if (strcmp(p->Token, "abs") ==0 ) { ReadFunc(p,x,24); }
else if (strcmp(p->Token, "sign")==0 ) { ReadFunc(p,x,25); }
else if (strcmp(p->Token, "round")==0 ) { ReadFunc(p,x,26); }
// else if (strcmp(p->Token, "frac") ==0 ) { ReadFunc(p,x,27); }
else if (strcmp(p->Token, "trunc")==0 ) { ReadFunc(p,x,28); }
// else if (strcmp(p->Token, "min")==0 ) { ReadFunc(p,x,29); }
// else if (strcmp(p->Token, "max") ==0 ) { ReadFunc(p,x,30); }
else if (strcmp(p->Token, "floor")==0 ) { ReadFunc(p,x,31); }
else if (strcmp(p->Token, "ceil")==0 ) { ReadFunc(p,x,32); }
// else if (strcmp(p->Token, "odd") ==0 ) { ReadFunc(p,x,33); }
// else if (strcmp(p->Token, "even")==0 ) { ReadFunc(p,x,34); }
//else else if (strcmp(p->Token, "integr")==0 ) { ReadFunc(p,x,35); }
else { p->Err =0; if (p->Err>=0) { break; }
}break;
} //End TokName
case TokReal:
case TokNumber:
// *x=atol(p->Token);
*x=atof(p->Token); //double
// printf ("\n TokNumber ,*x=%lf \n ",*x);
break;case TokHex:
*x = hextoint(p->Token);
break;
default: { p->Err =0; break; }
} // End Switch-Case
GetToken(p);
return;
}Saturday, February 21, 2015 8:48 PM -
void ReadFunc (TheData *p , double *x , int f)
{
printf ("\n ReadFunc, f=%d ",f);
int i;
double y=0;
GetToken(p);
//PrintToken(p ); //cos
if (p->Token[0]!= '(')
{
p->Err=0;
return;
}//PrintToken(p); // cos
GetToken(p); // (
// PrintToken(p);
Level1a(p, x);
// printf ( "\n ReadFunc PrintToken afterLevel 1a");
// PrintToken(p);
if (p->Err>=0) return;
//printf ("\n ReadFunc,Case , f=%d ",f);
// printf ("\n Before switch x=%lf\n",*x);
switch (f)
{
case 1 : //printf ("\n ReadFunc,Case , f=%d ",f);
//printf ("\n Before cos x=%lf\n",*x);
*x =cos( *x*AngleMode);
//printf ("\n After cos x=%lf\n",*x);
break;
case 2 : *x = sin(*x*AngleMode);
break;
case 3:
if (cos(*x*AngleMode)==0)
{
p->Err=1;
break;
}
else
*x=tan(*x*AngleMode);
break;case 4:
if(sin(*x*AngleMode)==0)
{
p->Err=1;
break;
}
else
*x = 1/tan(*x*AngleMode);
break;
case 5:
if ((*x>1)||(*x<-1) )
{
p->Err=1;
break;
}
else *x = atan2(sqrt(1-(*x)*(*x)),*x)/AngleMode;
break;
case 6:
if((*x>1)||(*x<-1))
{
p->Err=1;
break;
}
else
*x = atan2(*x,sqrt(1-(*x)*(*x)))/AngleMode;
break;
case 7: *x = atan2(*x,1)/AngleMode;
break;
case 8: *x =atan2(1,*x)/AngleMode;
break;
case 9:
if (p->Token[0]!=',')
{
p->Err=0;
break;
}
GetToken(p);
Level1a(p,&y); //arg 2
if (p->Err>=0)
break;
*x=atan2(*x,y)/AngleMode;
break;case 10:
y = exp(*x);
*x = (y+1/y)/2;
break;
case 11:
y = exp(*x);
*x = (y-1/y)/2;
break;
case 12:
y = exp(2* (*x) );
*x = (y-1)/(y+1);
break;
case 13:
if (*x==0)
{
p->Err=1;
break;
}
else
{
y = exp(2*(*x));
*x = (y+1)/(y-1) ;
}
break;case 14:
if (*x<1)
{
p->Err=1;
break;
}
else
*x = log(*x+sqrt(*x*(*x)-1))/log(digE);
break;
case 15:
*x= log(*x+sqrt(*x*(*x)+1))/log(digE);
break;
case 16:
if ((*x>=1)||(*x<=-1))
{
p->Err=1;
break;
}
else
*x =0.5*( log((1+*x)/(1-*x))/log(digE) ) ;
break;
case 17:
if ((*x<=1)&&(*x>=-1))
{
p->Err=1;
break;
}
else
*x = 0.5*( log((*x+1)/(*x-1))/log (digE) );
break;
case 18:
if (p->Token[0]!=',')
{
p->Err=0;
break;
}
GetToken(p);
//PrintToken(p );
Level1a(p,&y);
if (p->Err>=0) { break; }
if ((*x<=0)||(y<=0))
{
p->Err=1;
break;
}
*x = log(y)/log(*x);
break;
case 19:
if (*x<=0)
{
p->Err=1;
break;
}
else
*x = log10(*x);
break;
case 20:
if (*x<=0)
{
p->Err=1;
break;
}
else *x= log(*x);
break;
case 21: *x = exp(*x);
break;
case 22: *x =(*x)*(*x);
break;
case 23:
if (x<0)
{
p->Err=1;
break;
}
else *x = pow(*x,0.5);
break;
case 24: if (*x<0) {*x = -(*x);
break;
} //else {x =*x;break; }
case 25: if (*x<0) { *x =-1; }
else if (*x>0) {*x=1; }
break;
case 26:
case 27:
case 28:
{
y=1;
if (p->Token[0]=',')
{
GetToken(p);
//PrintToken(p );
Level1a(p, &y);
if (p->Err>=0) break;
}
switch (f) {
case 26: *x = y*round(*x/y);
break;
case 27: *x = y*((*x/y)-floor (*x/y));
break;
default: *x = y*trunc(*x/y);
break;
}}
case 29:
case 30:
while (p->Token[0]==',')
{
GetToken(p);
// PrintToken(p );
Level1a(p,&y);
if (p->Err>=0)
break;
if (f==29)
{
if (*x>y)
*x=y;
else if (*x<y) {*x=y; }
}
}break;
case 31: //floor
*x = floor(*x);
break;
case 32: //ceil
*x = ceil(*x);
break;
case 33:
//odd
// if ((x%2)!=0) { *x =1; }
// else {*x=0; }
break;
case 34:
//even
// if ((x%2)==0) { *x =1 ; }
// else {*x=0; }
break;
//case 35:
//
//GetToken(p);
// PrintToken(p );
// Level1a(p,&a);
// if (p->Err>=0)
// break;
//GetToken(p);
// PrintToken(p );
// Level1a(p,&b);
// if (p->Err>=0)
// break;
// getfxfromstring(p,f(x))
// *x=getromberg(a,b, f(x));
// break;
} /*end of switch-case */if (p->Token[0]!= ')') p->Err=0;
return;
}Saturday, February 21, 2015 8:49 PM -
typedef struct
{
char Str[200];
double p[10];
int pnumb;
int xvarindex;
} Param;
// y(x,{p} )=(sin(%1)+%1/%2)^%3 ; d%1double fun(double x , Param *par)
{
double y;
int i,err;
double vars[10];
TheData p1;
for (i=0;i<par->pnumb;i++)
{
vars[i]=par->p[i];
}
par->p[par->xvarindex]=x;doSetVars(&p1,par->pnumb,vars);
doSetMathStr( par->Str, &p1 );
doCalc(&p1, &y, &err );
if(err>0) { printf ("Error "); return -1;}
return y;
}
double getderive(double x0, double eps, Param *par, int narg )
{
par->xvarindex=narg;
double step,s, sn;
double (*fun) (double , Param * );
step=0.1;
s=1;
sn=101;while (fabs(s-sn)>eps)
{
s=(fun(x0+step,par)-fun(x0,par))/step;
step=step/2;
sn=(fun(x0+step,par)-fun(x0,par))/step;
}
return sn;
}
double trapintparam(double a,double b,double eps, Param *par , int narg )
{
par->xvarindex=narg;
double (*fun) (double , Param * ) ;double h1,s,s0,s1,sn, t;
int i,n;
s=1;
sn=101;
n=4;
s0=(fun(a,par)+fun(b,par))/2;
s1=fun(((a+b)/2),par);
while (fabs(s-sn)>eps){
sn=s;
h1=(b-a)/n;
for (i=0; i<n/2; i++)
s1+=fun( (a+(2*i+1)*h1),par);
s=h1*(s0+s1);
n*=2;
}
return s;
}double p(double tau)
{
/*dU/dt, t=tau;*/
return -20000*exp(-200*tau);
}double R=1000;
double C=0.00000001;
double k=0.25;
double h(double t ) {return 0.005-0.0025*exp(-100*t);
}
double f(double tau, double t )
{
return p(tau)*h(t-tau);
}
double trap(double a,double b,double eps, double(*f) (double , double ))
{
double h1,s,s0,s1,sn, t;
int i,n;
s=1;
sn=101;
n=4;
s0=(f(a,t)+f(b,t))/2;
s1=f(((a+b)/2),t);
while (fabs(s-sn)>eps){
sn=s;
h1=(b-a)/n;
for (i=0; i<n/2; i++)
s1+=f((a+(2*i+1)*h1),t);
s=h1*(s0+s1);
n*=2;
}
return s;
}double DuhamelInt( double t)
{
double a,b,er,eps;
double f(double, double),s,trap(double,double,double,double(*) (double,double)) ;
eps=0.0000001;return trap(0,t,eps,f);
}
typedef struct
{ float p[16];
float xre[16];
float xim[16];
float x[16]; // discrete-time signal, x
//float Xre[16], Xim[16]; // DFT of x (real and imaginary parts)
//float P[16]; // power spectrum of x
} DFTTD;
typedef struct
{
float X[16];
float Xre[16], Xim[16]; // DFT of x (real and imaginary parts)
float P[16]; // power spectrum of x
} DFTFD;
void DFT(DFTTD *x ,DFTFD *y, int Ndft )
{
Ndft =16;
#define PI2 6.283185
// time and frequency domain data arrays
int n, k; // indices for time and frequency domains
/*
N-1
__
1 \
---/ X[n](cos((2Pi*n*m)/N) + j*sin((2Pi*n*m)/N))
N --
n = 0
cxDft = complex( cos((2Pi*n*m)/N), sin((2Pi*n*m)/N) )
*/
// Generate random discrete-time signal x in range (-1,+1)
// srand(time(0));
// for (n=0 ; n<Ndft ; ++n) x[n] = ((2.0 * rand()) / RAND_MAX) - 1.0;
// Calculate DFT of x using brute force
for (k=0 ; k<Ndft ; ++k)
{
// Real part of X[k]
y->Xre[k] = 0;
for (n=0 ; n<Ndft ; ++n) y->Xre[k] += x->x[n] * cos(n * k * PI2 / Ndft);
// Imaginary part of X[k]
y->Xim[k] = 0;
for (n=0 ; n<Ndft ; ++n) y->Xim[k] -= x->x[n] * sin(n * k * PI2 / Ndft);
// Power at kth frequency bin
y->P[k] = y->Xre[k]*y->Xre[k] + y->Xim[k]*y->Xim[k];
}
return;
}
void IDFT(DFTFD *y ,DFTTD *x, int Ndft )
{
Ndft =16;
#define PI2 6.283185
// time and frequency domain data arrays
int n, k; // indices for time and frequency domains
/*
N-1
__
1 \
---/ X[n](cos((2Pi*n*m)/N) + j*sin((2Pi*n*m)/N))
N --
n = 0
cxDft = complex( cos((2Pi*n*m)/N), sin((2Pi*n*m)/N) )
*/
// Generate random discrete-time signal x in range (-1,+1)
// srand(time(0));
// for (n=0 ; n<Ndft ; ++n) x[n] = ((2.0 * rand()) / RAND_MAX) - 1.0;
// Calculate DFT of x using brute force
for (k=0 ; k<Ndft ; ++k)
{
// Real part of X[k]
x->xre[k] = 0;
for (n=0 ; n<Ndft ; ++n) x->xre[k] += y->X[n] * cos(n * k * PI2 / Ndft);
// Imaginary part of X[k]
x->xim[k] = 0;
for (n=0 ; n<Ndft ; ++n) x->xim[k] += y->X[n] * sin(n * k * PI2 / Ndft);
// Power at kth frequency bin
x->p[k] = x->xre[k]*x->xre[k] + x->xim[k]*x->xim[k];
}
return;
}Saturday, February 21, 2015 8:50 PM -
/*
{
FILE *fp;
double time,Tend,step,s;
printf ("\n s(t)= trap(0,t,eps,f); \n f(tau)=p(tau)*h(t-tau) ");
printf ("\n Input Tend, ms (120) ");
scanf("%lf",&Tend);
Tend=Tend*0.001;
printf ("\n Input step ,ms,(0.01) ");
scanf("%lf",&step);step=step*0.001;
if ((fp=fopen("Duhamel.txt","a"))==NULL) printf ("\n File opening error");fprintf (fp,"\n s(t)= trap(0,t,eps,f); \n f(tau)=p(tau)*h(t-tau) ");
fprintf (fp,"\n Tbeg=0 , Tend=%le s ",Tend);
fprintf (fp,"\n step=%le s ", step);
fprintf (fp,"\n p(tau)=-20000*exp(-200*tau); ");
fprintf (fp,"\n h(t)=0.005-0.0025*exp(-100*t); \n");for(time=0; time<=Tend; time=time+step)
{
s=DuhamelInt(time);
printf("\n t= %le s u(t)=%le ",time,s) ;
fprintf (fp,"\n t= %le s u(t)=%le ",time,s);
}
fclose(fp);
getch() ;
}
*/Saturday, February 21, 2015 8:50 PM -
how to integrate trapintparam(double a,double b,double eps, Param *par , int narg ) and getderivefunc() to parser ?
Saturday, February 21, 2015 8:52 PM -
How to convert it to class, how to return values of structure *p from methods ( if TheData *p declared in the class as public variable with pointer, using extra definitions of methods , anounce of some methods)?
How tgo create methods-runners for
MParserTag::doSetMathStr(char *string1 , TheData *p1 );
MParserTag::doInputMathStr( TheData *p1 );MParserTag::doInputVars(TheData *p1);
doCalc(p1, &y1, &Error );Friday, December 2, 2016 11:28 PM -
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>// using Dilma's algorithm from www.hiasm.com
#define TokName 1
#define TokReal 2
#define TokNumber 3
#define TokSymbol 4
#define TokMath 5
#define TokEnd 6
#define TokHex 7double pi=3.14159265358979;//32384626433832795;
double digE = 2.718281828459045;
typedef struct {
char Line[1024];
char Token[1024];
int TokType;
int LPos;
int Err;
double FResult;
int FDataCount;
int Flags [10];
int Xnumb;
double ArrayX [200];
} TheData;
int FDataCount=4;
int DataCount=4;
double FResult;
double FDefault=0;
double Default=0;
double AngleMode=1;
void Level0(TheData *p , double *x ) ;
void Level1a(TheData *p , double *x ) ;
void Level1b(TheData *p , double *x) ;
void Level2 (TheData *p , double *x) ;
void Level3 (TheData *p , double *x) ;
void Level4 (TheData *p , double *x) ;
void Level5 (TheData *p , double *x) ;
void Level6 (TheData *p , double *x ) ;void SetAngleMode ( int Value)
{
if (Value==0)
AngleMode =1;
else
AngleMode =pi/180;
return;
}void doInputVars(TheData *p)
{
int ncounts,i;
double x;
printf ("\n Inpun number of counts ");
scanf("%d",&ncounts);
p->FDataCount = ncounts;
for (i=0;i <ncounts;i++)
{
printf ("\n Input variable # %d : ",i);
scanf("%lf",&x);p->ArrayX[i]=x;
}
return;
}void doSetVars(TheData *p, int ncounts, double Argums[200])
{
int i;
double x;
printf ("\n Inpun number of counts ");
scanf("%d",&ncounts);
p->FDataCount=ncounts;
for (i=0;i<ncounts;i++)
{
p->ArrayX[i]=Argums[200];
}
return;
}
void doInputMathStr(TheData *p )
{
int i;char SafeLine [1024];
char string1[1024] ;
//char string;
printf("\n Input math string ");
for (i=0;i<1024;i++) { string1[i]='\0'; p->Line[i]='\0'; }
// string1[0]='1';
// string1[1]='+';
// string1[2]='%';
// string1[3]='1';
scanf("%100s",&string1 );
printf("\n %s",string1);
//for (i=0;i<strlen(string ) ;i++) string1[i]=string[i];
for (i=0;i<strlen(string1) ;i++) { p->Line[i]=string1[i]; }
// p->Line[strlen(p->Line-1)]='\x01';
p->Line[strlen(p->Line)]='\x01';
p->Line[strlen(p->Line+1)]='\x01';for (i=0;i<strlen(p->Line);i++) SafeLine[i]=p->Line[i];
for (i=0;i<strlen(p->Line);i++)
{
if ((p->Line[i]=='\n')||(p->Line[i]=='\r'))
p->Line[i]=' ';
}
return;
}
void doSetMathStr( char string1[1024], TheData *p )
{
int i;
char SafeLine [1024];
for (i=0;i<strlen(string1);i++) p->Line[i]=string1[i];p->Line[strlen(p->Line)]='\x01';
for (i=0;i<strlen(p->Line);i++) SafeLine[i]=p->Line[i];
for (i=0;i<strlen(p->Line);i++)
{
if ((p->Line[i]=='\n')||(p->Line[i]=='\r'))
p->Line[i]=' ';
}return;
}
void doClear(TheData *p)
{
p->FResult=FDefault;
p->LPos=0;
p->Err =-1;
return;
}void doCalc(TheData *p, double *y, int *Error )
{
*Error=0;
double Y;
doClear(p) ;
Level0(p, &Y);
if (p->Err < 0)
{
p->LPos=0;
*y=Y;
}
else
{
p->LPos--;
*y=0;
*Error=p->Err;
}
return ;
}void PrintToken( TheData *p )
{
int i;
printf ("\n" );
for (i=0;i<strlen(p->Token);i++) printf (" %c",p->Token[i]);
printf ("\n" );
return;
}Friday, December 2, 2016 11:28 PM -
/* ************parser*************** */
void GetToken( TheData *p )
{
int i;for( i=0;i<1024;i++) p->Token[i]='\0';
p->TokType=0;
while ((p->Line[p->LPos]==' ' )||(p->Line[p->LPos]=='\t') ) p->LPos++;
switch (p->Line[p->LPos])
{
case 'a' ... 'z':
case 'A' ... 'Z':
case '_':
// case 'a' ... 'y':
// case 'A' ... '?':
while(isalnum(p->Line[p->LPos])||(p->Line[p->LPos]=='_'))
{
/*strappen*/
i=strlen(p->Token);
if (p->Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
for(i=0;i<strlen(p->Token); i++)
{
p->Token[i]=tolower(p->Token[i]);
}
p->TokType=TokName;
break;case '0'...'9':
case '.':
case '$':
if ((p-> Line[p->LPos]=='$')||((p-> Line[p->LPos]=='0')&&(p->Line[p->LPos+1]=='x')) )
{
if(p-> Line[p->LPos]!='$') p-> LPos++;
p-> LPos++;while (isxdigit (p-> Line[p->LPos]))
{ /*append */
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p-> LPos++;
}
if(p->Token[0]!='\0')
{
p->TokType= TokHex;
break;
}
}while (isdigit(p->Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p->Line[p->LPos];
i++;
}
p->Token[i]='\0';
p->LPos++;
}p->TokType=TokNumber;
if (p-> Line[p->LPos]=='.')
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]='.';
i++;
}
p->Token[i]='\0';
p->LPos++;
while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p->LPos++;
}
if (!(p->Token[0]!='.'))
{
p->TokType=0;
break;
}
p->TokType=TokReal;
}
while( (p->Line[p->LPos]==' ')||(p->Line[p->LPos]=='\t') ) p-> LPos++;
if (( p->Line[p->LPos]=='e' )||( p->Line[p->LPos]=='E' ))
{
/*append string*/
// printf("\n Exp ");
i=strlen( p->Token );
if ( p-> Line[p->LPos]!='\0' )
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p->LPos++;
while ((p->Line[p->LPos] ==' ')|| (p->Line[p->LPos]=='\t')) p-> LPos++;if((p-> Line[p->LPos]=='+')||(p->Line[p->LPos]=='-'))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p->LPos++;
while ((p->Line[p->LPos] ==' ')||( p->Line[p->LPos] =='\t')) p->LPos++;
}
if (!(isdigit(p-> Line[p->LPos])))
{
p->TokType=0;
break;
}
while (isdigit(p-> Line[p->LPos]))
{
i=strlen(p->Token);
if (p-> Line[p->LPos]!='\0')
{
p->Token[i]=p-> Line[p->LPos];
i++;
}
p->Token[i]='\0';
p->LPos++;
}
p->TokType=TokReal;
}
break;
case '(':
case ')':
case '%':
case ',':
case '[':
case ']':
p->Token[0]=p-> Line[p->LPos];
p->LPos++;
p->TokType=TokSymbol;
break;
case '<':
case '>':
case '=':
p->Token[0]=p-> Line[p->LPos];
p->LPos++;
p->TokType=TokSymbol;
if((p->Token[0] == '<')&&(p-> Line[p->LPos]=='='))
{
p->Token[0]='{';
p->LPos++;
}
else
{
if((p->Token[0]=='>')&&(p-> Line[p->LPos]=='='))
{
p->Token[0]='}';
p->LPos++;
}
}
break;
case '+':
case '-':
case '/':
case '*':
case '^':
p->Token[0] = p->Line[p->LPos];
p->LPos++;
while ((p->Line[p->LPos]==' ')||(p->Line[p->LPos]=='\t')) p->LPos++;
if ( !((p->Line[p->LPos]=='+')||(p->Line[p->LPos]=='-')) )
{
p->TokType=TokMath;
}
else
{
p->Token[0] = '\0';
}
break;
case '\x01':
p->TokType=TokEnd;
break;
}return;
}
void Level0( TheData *p, double *x)
{
int i;
for (i=0 ; i<p->FDataCount;i++) p->Flags[i]=0;
if (p->LPos>=0)
{
p->FResult=FDefault;
p->LPos= 0; //1
p->Err=-1;
// printf ("\n Level 0 x=%lf ", *x);
GetToken(p);
// PrintToken(p ); // 1
Level1a(p,x);
if (p->Err>=0) return;
if (p->TokType!=TokEnd) { p->Err =0; }
}
return;
}
void Level1a(TheData *p , double *x ) // < > <= >= = //
{
double y;
char op ;
double x2 ;
int b ;
Level1b(p, x);
// printf ("\n Level 1a (afterLevel1b) *x=%lf",*x);
// PrintToken(p);
if (p->Err>=0) return;
while ((p->Token[0] == '<')||(p->Token[0] == '>')||(p->Token[0] == '{')||(p->Token[0] == '}')||(p->Token[0] == '='))
{
op=p->Token[0];
// printf ("\n Level 1a op=%c \n",op);
GetToken(p);
// PrintToken(p);
Level1b(p, &x2);
// printf ("\n Level 1a x2=%lf",x2);
if (p->Err>=0)
return;
b=0;
switch (op)
{
case '<':
if(*x<x2) b=1;
break;
case '>':
if(*x>x2) b=1;
break;
case '{':
if(*x<=x2) b=1;
break;
case '}':
if(*x>=x2) b=1;
break;
case '=':
if(*x==x2) b=1;
break;
default : b=0;
break;
}if (b==1) { *x= 1; }
else
{ *x= 0; }
}
return;
}Friday, December 2, 2016 11:29 PM -
void Level6( TheData *p , double *x ) // ( function
{
int i,j ,k;
double Y=0;
double Arr[200];
double Mtx [200];
double Args[200];
// printf ("\n Level 6 x=%lf\n",*x);
if (p->Token[0] == '%')
{
GetToken(p); //
if (p->TokType==TokNumber)
{
i= atoi(p->Token)-1;
if (i<p->FDataCount) //
{
GetToken(p); // node number
{
if (p->Flags[i]==1) { *x=Args[i]; }
else
{
Args[i]=p->ArrayX[i];
*x=Args[i];
p->Flags[i]=1;
// printf ("\n i= %d Args [i]=%lf p->Flags[i]=%d ",i, Args[i],p->Flags[i]);
}
return;
}// end if token =(-else
}
else { p->Err=0; return; }
} //end If tokNumber
else { p->Err=0; return; }
}//end if %
else
if (p->Token[0]== '(')
{
GetToken(p); //get arg in the ()
// PrintToken(p);
Level1a(p,x); // PrintToken(p);
if (p->Err>=0) return;
if (p->Token[0]!= ')')
{
p->Err=0; return;
}
}
else
switch (p->TokType)
{
case TokName:
{ //Begin TokName
// printf ("\n p->TokType =TokName ");
// PrintToken(p);
if (strcmp(p->Token, "pi")==0 ) { *x = pi; }
else if (strcmp(p->Token, "e")==0 ) { *x = digE; }
else if (strcmp(p->Token, "cos")==0 ) { ReadFunc(p, x,1); }
else if (strcmp(p->Token, "sin")==0 ) { ReadFunc(p,x,2); }
else if (strcmp(p->Token, "tg")==0 ) { ReadFunc(p,x,3); }
else if (strcmp(p->Token, "ctg")==0 ) { ReadFunc(p,x,4); }
else if (strcmp(p->Token, "arccos")==0 ) { ReadFunc(p,x,5); }
else if (strcmp(p->Token, "arcsin")==0 ) { ReadFunc(p,x,6); }
else if (strcmp(p->Token, "arctg")==0 ) { ReadFunc(p,x,7); }
else if (strcmp(p->Token, "arcctg")==0 ) { ReadFunc(p,x,8); }
else if (strcmp(p->Token, "atan")==0 ) { ReadFunc(p,x,9); }
else if (strcmp(p->Token, "ch")==0 ) { ReadFunc(p,x,10); }
else if (strcmp(p->Token, "sh")==0 ) { ReadFunc(p,x,11); }
else if (strcmp(p->Token, "th")==0 ) { ReadFunc(p,x,12); }
else if (strcmp(p->Token, "cth")==0 ) { ReadFunc(p,x,13); }
else if (strcmp(p->Token, "arcch")==0 ) { ReadFunc(p,x,14); }
else if (strcmp(p->Token, "arcsh")==0 ) { ReadFunc(p,x,15); }
else if (strcmp(p->Token, "arcth")==0 ) { ReadFunc(p,x,16); }
else if (strcmp(p->Token, "arccth")==0 ) { ReadFunc(p,x,17); }
else if (strcmp(p->Token, "log")==0 ) { ReadFunc(p,x,18); }
else if (strcmp(p->Token, "lg")==0 ) { ReadFunc(p,x,19); }
else if (strcmp(p->Token, "ln") ==0 ) { ReadFunc(p,x,20); }
else if (strcmp(p->Token, "exp")==0 ) { ReadFunc(p,x,21); }
else if (strcmp(p->Token, "sqr") ==0 ) { ReadFunc(p,x,22); }
else if (strcmp(p->Token, "sqrt")==0 ) { ReadFunc(p,x,23); }
else if (strcmp(p->Token, "abs") ==0 ) { ReadFunc(p,x,24); }
else if (strcmp(p->Token, "sign")==0 ) { ReadFunc(p,x,25); }
else if (strcmp(p->Token, "round")==0 ) { ReadFunc(p,x,26); }
// else if (strcmp(p->Token, "frac") ==0 ) { ReadFunc(p,x,27); }
else if (strcmp(p->Token, "trunc")==0 ) { ReadFunc(p,x,28); }
// else if (strcmp(p->Token, "min")==0 ) { ReadFunc(p,x,29); }
// else if (strcmp(p->Token, "max") ==0 ) { ReadFunc(p,x,30); }
else if (strcmp(p->Token, "floor")==0 ) { ReadFunc(p,x,31); }
else if (strcmp(p->Token, "ceil")==0 ) { ReadFunc(p,x,32); }
// else if (strcmp(p->Token, "odd") ==0 ) { ReadFunc(p,x,33); }
// else if (strcmp(p->Token, "even")==0 ) { ReadFunc(p,x,34); }
//else else if (strcmp(p->Token, "integr")==0 ) { ReadFunc(p,x,35); }
else { p->Err =0; if (p->Err>=0) { break; }
}break;
} //End TokName
case TokReal:
case TokNumber:
// *x=atol(p->Token);
*x=atof(p->Token); //double
// printf ("\n TokNumber ,*x=%lf \n ",*x);
break;case TokHex:
*x = hextoint(p->Token);
break;
default: { p->Err =0; break; }
} // End Switch-Case
GetToken(p);
return;
}
void ReadFunc (TheData *p , double *x , int f)
{
printf ("\n ReadFunc, f=%d ",f);
int i;
double y=0;
GetToken(p);
//PrintToken(p ); //cos
if (p->Token[0]!= '(')
{
p->Err=0;
return;
}//PrintToken(p); // cos
GetToken(p); // (
// PrintToken(p);
Level1a(p, x);
// printf ( "\n ReadFunc PrintToken afterLevel 1a");
// PrintToken(p);
if (p->Err>=0) return;
//printf ("\n ReadFunc,Case , f=%d ",f);
// printf ("\n Before switch x=%lf\n",*x);
switch (f)
{
case 1 : //printf ("\n ReadFunc,Case , f=%d ",f);
//printf ("\n Before cos x=%lf\n",*x);
*x =cos( *x*AngleMode);
//printf ("\n After cos x=%lf\n",*x);
break;
case 2 : *x = sin(*x*AngleMode);
break;
case 3:
if (cos(*x*AngleMode)==0)
{
p->Err=1;
break;
}
else
*x=tan(*x*AngleMode);
break;case 4:
if(sin(*x*AngleMode)==0)
{
p->Err=1;
break;
}
else
*x = 1/tan(*x*AngleMode);
break;
case 5:
if ((*x>1)||(*x<-1) )
{
p->Err=1;
break;
}
else *x = atan2(sqrt(1-(*x)*(*x)),*x)/AngleMode;
break;
case 6:
if((*x>1)||(*x<-1))
{
p->Err=1;
break;
}
else
*x = atan2(*x,sqrt(1-(*x)*(*x)))/AngleMode;
break;
case 7: *x = atan2(*x,1)/AngleMode;
break;
case 8: *x =atan2(1,*x)/AngleMode;
break;
case 9:
if (p->Token[0]!=',')
{
p->Err=0;
break;
}
GetToken(p);
Level1a(p,&y); //arg 2
if (p->Err>=0)
break;
*x=atan2(*x,y)/AngleMode;
break;case 10:
y = exp(*x);
*x = (y+1/y)/2;
break;
case 11:
y = exp(*x);
*x = (y-1/y)/2;
break;
case 12:
y = exp(2* (*x) );
*x = (y-1)/(y+1);
break;
case 13:
if (*x==0)
{
p->Err=1;
break;
}
else
{
y = exp(2*(*x));
*x = (y+1)/(y-1) ;
}
break;case 14:
if (*x<1)
{
p->Err=1;
break;
}
else
*x = log(*x+sqrt(*x*(*x)-1))/log(digE);
break;
case 15:
*x= log(*x+sqrt(*x*(*x)+1))/log(digE);
break;
case 16:
if ((*x>=1)||(*x<=-1))
{
p->Err=1;
break;
}
else
*x =0.5*( log((1+*x)/(1-*x))/log(digE) ) ;
break;
case 17:
if ((*x<=1)&&(*x>=-1))
{
p->Err=1;
break;
}
else
*x = 0.5*( log((*x+1)/(*x-1))/log (digE) );
break;
case 18:
if (p->Token[0]!=',')
{
p->Err=0;
break;
}
GetToken(p);
//PrintToken(p );
Level1a(p,&y);
if (p->Err>=0) { break; }
if ((*x<=0)||(y<=0))
{
p->Err=1;
break;
}
*x = log(y)/log(*x);
break;
case 19:
if (*x<=0)
{
p->Err=1;
break;
}
else
*x = log10(*x);
break;
case 20:
if (*x<=0)
{
p->Err=1;
break;
}
else *x= log(*x);
break;
case 21: *x = exp(*x);
break;
case 22: *x =(*x)*(*x);
break;
case 23:
if (x<0)
{
p->Err=1;
break;
}
else *x = pow(*x,0.5);
break;
case 24: if (*x<0) {*x = -(*x);
break;
} //else {x =*x;break; }
case 25: if (*x<0) { *x =-1; }
else if (*x>0) {*x=1; }
break;
case 26:
case 27:
case 28:
{
y=1;
if (p->Token[0]=',')
{
GetToken(p);
//PrintToken(p );
Level1a(p, &y);
if (p->Err>=0) break;
}
switch (f) {
case 26: *x = y*round(*x/y);
break;
case 27: *x = y*((*x/y)-floor (*x/y));
break;
default: *x = y*trunc(*x/y);
break;
}}
case 29:
case 30:
while (p->Token[0]==',')
{
GetToken(p);
// PrintToken(p );
Level1a(p,&y);
if (p->Err>=0)
break;
if (f==29)
{
if (*x>y)
*x=y;
else if (*x<y) {*x=y; }
}
}break;
case 31: //floor
*x = floor(*x);
break;
case 32: //ceil
*x = ceil(*x);
break;
case 33:
//odd
// if ((x%2)!=0) { *x =1; }
// else {*x=0; }
break;
case 34:
//even
// if ((x%2)==0) { *x =1 ; }
// else {*x=0; }
break;
//case 35:
//
//GetToken(p);
// PrintToken(p );
// Level1a(p,&a);
// if (p->Err>=0)
// break;
//GetToken(p);
// PrintToken(p );
// Level1a(p,&b);
// if (p->Err>=0)
// break;
// getfxfromstring(p,f(x))
// *x=getromberg(a,b, f(x));
// break;
} /*end of switch-case */if (p->Token[0]!= ')') p->Err=0;
return;
}int main()
{
TheData *p1;
int Error;
double y,y1;
double Arg[200];
doClear( p1);
char string1[1024];
// doSetMathStr( string1 , p1 );
doInputMathStr( p1 );doInputVars( p1);
doCalc(p1, &y1, &Error );
printf("%lf",y1 );
getch();
return 0;
}Friday, December 2, 2016 11:31 PM -
If i use it in class, it gets error
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <iostream>// using Dilma's algorithm from www.hiasm.com
#define TokName 1
#define TokReal 2
#define TokNumber 3
#define TokSymbol 4
#define TokMath 5
#define TokEnd 6
#define TokHex 7double pi=3.14159265358979;//32384626433832795;
double digE = 2.718281828459045;
typedef struct {
char Line[1024];
char Token[1024];
int TokType;
int LPos;
int Err;
double FResult;
int FDataCount ;
int Flags [10];
int Xnumb;
double ArrayX [200];
} TheData;
int FDataCount=4;
int DataCount=4;
double FResult;
double FDefault=0;
double Default=0;
double AngleMode=1;class MathParser {
TheData *MathParser_p;
int MathParser_Error;
double MathParser_y;
double MathParser_Arg[200];
public:
TheData* doInputVars1(TheData *MathParser_p);
void doSetVars(TheData *MathParser_p, int ncounts, double MathParser_Arg[200]);
TheData* doInputMathStr1(TheData *MathParser_p );
void doSetMathStr( char string1[1024], TheData *MathParser_p );
TheData* doClear(TheData * MathParser_p);
TheData* doCalc1(TheData *MathParser_p, double *MathParser_y, int *MathParser_Error );
void doInit(void);
void doInputMathStr(void );
double doCalc ( void);
void doInputVars(void);private:
void SetAngleMode ( int Value);
void PrintToken( TheData *MathParser_p );void GetToken( TheData *MathParser_p );
void Level0(TheData *MathParser_p , double *x ) ;
void Level1a(TheData *MathParser_p , double *x ) ;
void Level1b(TheData *MathParser_p , double *x) ;
void Level2 (TheData *MathParser_p , double *x) ;
void Level3 (TheData *MathParser_p , double *x) ;
void Level4 (TheData *MathParser_p , double *x) ;
void Level5 (TheData *MathParser_p , double *x) ;
void Level6 (TheData *MathParser_p , double *x ) ;
void ReadFunc (TheData *MathParser_p , double *x , int f);
};void MathParser::SetAngleMode ( int Value)
{
if (Value==0)
AngleMode =1;
else
AngleMode =pi/180;
return;
}TheData* MathParser::doInputVars1(TheData * _p )
{
TheData * _p1;
_p1= _p;
printf("do input vars");
int ncount ,i;
double x;
printf ("\n Inpun number of counts ");
scanf("%d", &ncount );
printf(" ncounts");
_p1->FDataCount= (int) ncount ;
printf(" ncounts 1");
for (i=0;i <ncount ; i++)
{
printf ("\n Input variable # %d : ",i);
scanf("%le",&x);
_p1->ArrayX[i]=x;
}
return _p1;
}void MathParser::doInputVars ( ){
MathParser_p=doInputVars1( MathParser_p );
return;
}
void MathParser::doSetVars(TheData *MathParser_p, int ncounts, double MathParser_Arg[200])
{
int i;
double x;
printf ("\n Inpun number of counts ");
scanf("%d",&ncounts);
MathParser_p->FDataCount=ncounts;
for (i=0;i<ncounts;i++)
{
MathParser_p->ArrayX[i]=MathParser_Arg[200];
}
return;
}
TheData* MathParser::doInputMathStr1(TheData *_p1 )
{
int i;
TheData *_p ;
_p =_p1;
char SafeLine [1024];
char string1[1024];
printf("\n Input math string ");
for (i=0;i<1024;i++) { string1[i]='\0'; _p->Line[i]='\0'; }
//scanf("%s",string1);
string1[0]='1';
string1[1]='+';
string1[2]='%';
string1[3]='1';
printf("\n %s",string1);
for (i=0;i< strlen(string1) ;i++) { _p->Line[i]=string1[i]; }_p->Line[strlen( _p->Line)]='\x01';
for (i=0;i<strlen( _p->Line);i++) { SafeLine[i]= _p->Line[i];}
for (i=0;i<strlen( _p->Line);i++)
{
if (( _p->Line[i]=='\n')||( _p->Line[i]=='\r'))
_p->Line[i]=' ';
}return _p ;
}
void MathParser::doInputMathStr (void)
{MathParser::MathParser_p=doInputMathStr1( MathParser::MathParser_p );
return;
}
void MathParser:: doSetMathStr( char string1[1024], TheData *MathParser_p )
{
int i;
char SafeLine [1024];
for (i=0;i<strlen(string1);i++) MathParser_p->Line[i]=string1[i];MathParser_p->Line[strlen(MathParser_p->Line)]='\x01';
for (i=0;i<strlen(MathParser_p->Line);i++) SafeLine[i]=MathParser_p->Line[i];
for (i=0;i<strlen(MathParser_p->Line);i++)
{
if ((MathParser_p->Line[i]=='\n')||(MathParser_p->Line[i]=='\r'))
MathParser_p->Line[i]=' ';
}return;
}
TheData* MathParser::doClear(TheData * _p1)
{
TheData *_p;
_p=_p1;
_p->FResult=FDefault;
_p->LPos=0;
_p->Err =-1;
return _p1;
}void MathParser::doInit(void)
{
MathParser_p=doClear( MathParser_p);
return;
}TheData* MathParser::doCalc1(TheData *MathParser_p, double *MathParser_y, int *MathParser_Error )
{
printf("Do calc 1");
*MathParser_Error=0;
double Y;
TheData *MathParser_p1;
MathParser_p1=MathParser_p;
MathParser_p1->FResult= FDefault;
MathParser_p1->LPos=0;
MathParser_p1->Err =-1;
printf(" Do level 0");
Level0( MathParser_p1 , &Y);
printf(" Do level 1");
if ( MathParser_p1->Err < 0)
{
printf(" Do if");
MathParser_p1->LPos=0;
*MathParser_y=Y;
}
else
{
printf(" Do else");
MathParser_p1->LPos--;
printf(" y=0");
*MathParser_y=0;
printf(" err");
getch();
// *Error=MathParser_p->Err;
printf(" err=");
getch();
}
printf(" LPOS= " );
getch();
return MathParser_p1 ;
}double MathParser::doCalc ( void)
{//TheData *MathParser_p1;
doCalc1( MathParser_p , &MathParser_y , &MathParser_Error );
printf(" calculated");
getch();
return MathParser_y;
}void MathParser::PrintToken( TheData * MathParser_p )
{
int i;
printf ("\n" );
for (i=0;i<strlen( MathParser_p->Token);i++) printf (" %c", MathParser_p->Token[i]);
printf ("\n" );
return;
}Friday, December 2, 2016 11:34 PM -
/* ************parser*************** */
void MathParser::GetToken( TheData *MathParser_p )
{
int i;
for( i=0;i<1024;i++) MathParser_p->Token[i]='\0';
MathParser_p->TokType=0;
while ((MathParser_p->Line[MathParser_p->LPos]==' ' )||(MathParser_p->Line[MathParser_p->LPos]=='\t') ) MathParser_p->LPos++;
switch (MathParser_p->Line[MathParser_p->LPos])
{
case 'a' ... 'z':
case 'A' ... 'Z':
case '_':
// case 'a' ... 'y':
// case 'A' ... '?':
while(isalnum(MathParser_p->Line[MathParser_p->LPos])||(MathParser_p->Line[MathParser_p->LPos]=='_'))
{
/*strappen*/
i=strlen(MathParser_p->Token);
if (MathParser_p->Line[MathParser_p->LPos]!='\0')
{
MathParser_p->Token[i]=MathParser_p-> Line[MathParser_p->LPos];
i++;
}
MathParser_p->Token[i]='\0';
MathParser_p->LPos++;
}
for(i=0;i<strlen(MathParser_p->Token); i++)
{
MathParser_p->Token[i]=tolower(MathParser_p->Token[i]);
}
MathParser_p->TokType=TokName;
break;case '0'...'9':
case '.':
case '$':
if ((MathParser_p->Line[MathParser_p->LPos]=='$')||((MathParser_p->Line[MathParser_p->LPos]=='0')&&(MathParser_p->Line[MathParser_p->LPos+1]=='x')) )
{
if(MathParser_p->Line[MathParser_p->LPos]!='$') MathParser_p->LPos++;
MathParser_p->LPos++;while (isxdigit (MathParser_p->Line[MathParser_p->LPos]))
{ /*append */
i=strlen(MathParser_p->Token);
if (MathParser_p->Line[MathParser_p->LPos]!='\0')
{
MathParser_p->Token[i]=MathParser_p->Line[MathParser_p->LPos];
i++;
}
MathParser_p->Token[i]='\0';
MathParser_p->LPos++;
}
if(MathParser_p->Token[0]!='\0')
{
MathParser_p->TokType= TokHex;
break;
}
}while (isdigit(MathParser_p->Line[MathParser_p->LPos]))
{
i=strlen(MathParser_p->Token);
if (MathParser_p->Line[MathParser_p->LPos]!='\0')
{
MathParser_p->Token[i]=MathParser_p->Line[MathParser_p->LPos];
i++;
}
MathParser_p->Token[i]='\0';
MathParser_p->LPos++;
}MathParser_p->TokType=TokNumber;
if (MathParser_p->Line[MathParser_p->LPos]=='.')
{
i=strlen(MathParser_p->Token);
if (MathParser_p->Line[MathParser_p->LPos]!='\0')
{
MathParser_p->Token[i]='.';
i++;
}
MathParser_p->Token[i]='\0';
MathParser_p-> LPos++;
while (isdigit(MathParser_p->Line[MathParser_p->LPos]))
{
i=strlen(MathParser_p->Token);
if (MathParser_p->Line[MathParser_p->LPos]!='\0')
{
MathParser_p->Token[i]=MathParser_p->Line[MathParser_p->LPos];
i++;
}
MathParser_p->Token[i]='\0';
MathParser_p->LPos++;
}
if (!(MathParser_p->Token[0]!='.'))
{
MathParser_p->TokType=0;
break;
}
MathParser_p->TokType=TokReal;
}
while( (MathParser_p->Line[MathParser_p->LPos]==' ')||(MathParser_p->Line[MathParser_p->LPos]=='\t') ) MathParser_p->LPos++;
if ((MathParser_p->Line[MathParser_p->LPos]=='e')||(MathParser_p-> Line[MathParser_p->LPos]=='E'))
{
/*append string*/
// printf("\n Exp ");
i=strlen(MathParser_p->Token);
if (MathParser_p->Line[MathParser_p->LPos]!='\0' )
{
MathParser_p->Token[i]=MathParser_p->Line[MathParser_p->LPos];
i++;
}
MathParser_p->Token[i]='\0';
MathParser_p->LPos++;
while ((MathParser_p->Line[MathParser_p->LPos] ==' ')|| (MathParser_p->Line[MathParser_p->LPos]=='\t')) MathParser_p->LPos++;if((MathParser_p->Line[MathParser_p->LPos]=='+')||(MathParser_p->Line[MathParser_p->LPos]=='-'))
{
i=strlen(MathParser_p->Token);
if (MathParser_p->Line[MathParser_p->LPos]!='\0')
{
MathParser_p->Token[i]=MathParser_p->Line[MathParser_p->LPos];
i++;
}
MathParser_p->Token[i]='\0';
MathParser_p->LPos++;
while ((MathParser_p->Line[MathParser_p->LPos] ==' ')||( MathParser_p->Line[MathParser_p->LPos] =='\t')) MathParser_p->LPos++;
}
if (!(isdigit(MathParser_p->Line[MathParser_p->LPos])))
{
MathParser_p->TokType=0;
break;
}
while (isdigit(MathParser_p->Line[MathParser_p->LPos]))
{
i=strlen(MathParser_p->Token);
if (MathParser_p->Line[MathParser_p->LPos]!='\0')
{
MathParser_p->Token[i]=MathParser_p->Line[MathParser_p->LPos];
i++;
}
MathParser_p->Token[i]='\0';
MathParser_p->LPos++;
}
MathParser_p->TokType=TokReal;
}
break;
case '(':
case ')':
case '%':
case ',':
case '[':
case ']':
MathParser_p->Token[0]=MathParser_p->Line[MathParser_p->LPos];
MathParser_p->LPos++;
MathParser_p->TokType=TokSymbol;
break;
case '<':
case '>':
case '=':
MathParser_p->Token[0]=MathParser_p->Line[MathParser_p->LPos];
MathParser_p->LPos++;
MathParser_p->TokType=TokSymbol;
if((MathParser_p->Token[0] == '<')&&(MathParser_p->Line[MathParser_p->LPos]=='='))
{
MathParser_p->Token[0]='{';
MathParser_p->LPos++;
}
else
{
if((MathParser_p->Token[0]=='>')&&(MathParser_p->Line[MathParser_p->LPos]=='='))
{
MathParser_p->Token[0]='}';
MathParser_p->LPos++;
}
}
break;
case '+':
case '-':
case '/':
case '*':
case '^':
MathParser_p->Token[0] = MathParser_p->Line[MathParser_p->LPos];
MathParser_p->LPos++;
while ((MathParser_p->Line[MathParser_p->LPos] == ' ')||(MathParser_p->Line[MathParser_p->LPos]=='\t')) MathParser_p->LPos++;
if (!((MathParser_p->Line[MathParser_p->LPos] =='+')||(MathParser_p->Line[MathParser_p->LPos]=='-')))
{
MathParser_p->TokType=TokMath;
}
else
{
MathParser_p->Token[0] = '\0';
}
break;
case '\x01':
MathParser_p->TokType=TokEnd;
break;
}return;
}
void MathParser::Level0( TheData *MathParser_p, double *x)
{
int i;
for (i=0 ; i<MathParser_p->FDataCount;i++) MathParser_p->Flags[i]=0;
if (MathParser_p->LPos>=0)
{
MathParser_p->FResult=FDefault;
MathParser_p->LPos= 0; //1
MathParser_p->Err=-1;
// printf ("\n Level 0 x=%lf ", *x);
GetToken(MathParser_p);
// PrintToken(p ); // 1
Level1a(MathParser_p,x);
if (MathParser_p->Err>=0)
return;
if (MathParser_p->TokType!=TokEnd) MathParser_p->Err =0;
}
return;
}
void MathParser::Level1a(TheData *MathParser_p , double *x ) // < > <= >= = //
{
double y;
char op ;
double x2 ;
int b ;
Level1b(MathParser_p, x);
// printf ("\n Level 1a (afterLevel1b) *x=%lf",*x);
// PrintToken(p);
if (MathParser_p->Err>=0) return;
while ((MathParser_p->Token[0] == '<')||(MathParser_p->Token[0] == '>')||(MathParser_p->Token[0] == '{')||(MathParser_p->Token[0] == '}')||(MathParser_p->Token[0] == '='))
{
op=MathParser_p->Token[0];
// printf ("\n Level 1a op=%c \n",op);
GetToken(MathParser_p);
// PrintToken(p);
Level1b(MathParser_p, &x2);
// printf ("\n Level 1a x2=%lf",x2);
if (MathParser_p->Err>=0)
return;
b=0;
switch (op)
{
case '<':
if(*x<x2) b=1;
break;
case '>':
if(*x>x2) b=1;
break;
case '{':
if(*x<=x2) b=1;
break;
case '}':
if(*x>=x2) b=1;
break;
case '=':
if(*x==x2) b=1;
break;
default : b=0;
break;
}if (b==1) { *x= 1; }
else
{ *x= 0; }
}
return;
}
void MathParser::Level1b(TheData *MathParser_p , double *x) // + -
{
//printf ("\n Level 1b x=%lf\n",*x);
char op ;
double x2 ;
Level2(MathParser_p, x);
// printf ("\n Level 1b x=%lf\n",*x);
//printf ("\n Level 1b x2=%lf\n ",x2);
if (MathParser_p->Err>=0)
return;while ((MathParser_p->Token[0]=='-')||(MathParser_p->Token[0]=='+'))
{
op = MathParser_p->Token[0];
//printf ("\n Level 1b op=%c \n",op);
GetToken(MathParser_p);
// PrintToken(p);
Level2(MathParser_p,&x2);
// printf ("\n Level 1b x2=%lf\n",x2);
if (MathParser_p->Err>=0)
return;
if (op=='+') { *x=*x+x2; }
else *x=*x-x2;
// printf ("\n Level 1b x=%lf\n",*x);
}
return;
}void MathParser::Level2 ( TheData *MathParser_p , double *x) /* / * */
{
char op;
double x2;Level3(MathParser_p,x);
// printf ("\n Level 2, x=%lf\n ",*x ); //x=10
if (MathParser_p->Err>=0) return;
//Token[0]=*
while ((MathParser_p->Token[0]=='/')||(MathParser_p->Token[0]=='*') ||(strcmp(MathParser_p->Token, "div")==0)||(strcmp(MathParser_p->Token, "mod")==0))
{
op=MathParser_p->Token[0]; // *
GetToken(MathParser_p); // Token of 2nd arg-cos
// PrintToken(p);
Level3(MathParser_p, &x2); //
//printf ("\n Level 2, x2=%lf\n ",x2 );//arg 2 0
if (MathParser_p->Err>=0) return;
MathParser_p->Err=1;
switch (op)
{
case '*':
*x = *x*x2;
break;
case '/':
if (x2==0)
return;
else
*x=*x/x2;
break;
/*
case 'd':
if (round(x2))=0
return
else
x=round(x)%round(x2);
break;
*/
default :
if (round(x2)==0) { return; }
else *x=fmod(round(*x), round(x2));
break;
}
MathParser_p->Err =-1;
return;
}
return;
}
void MathParser::Level3( TheData *MathParser_p , double *x) // ^
{
double x2;Level4(MathParser_p,x);
if (MathParser_p->Err>=0)
return;
while(MathParser_p->Token[0]=='^')
{
GetToken(MathParser_p);
Level4(MathParser_p,&x2);
if (MathParser_p->Err>=0) return;
if ((round(x2)!=x2)&&(*x<=0)) { MathParser_p->Err=1; }
else *x=pow(*x, x2);
}
return;
}
void MathParser::Level4( TheData *MathParser_p , double *x) // unar + - not //
{
//printf ("\n Level 4, x=%lf\n ",*x );
char op;
op=' ';
if ((MathParser_p->Token[0] =='-')||(MathParser_p->Token[0] == '+'))
{
op= MathParser_p->Token[0];
GetToken(MathParser_p);
// PrintToken(p);
}
Level5(MathParser_p,x);
//PrintToken(p);
if (op =='-') *x=-(*x);
return;
}//void MathParser::ReadFunc (TheData *p , double *x , int f);
void MathParser::Level5(TheData *MathParser_p , double *x) // and or xor //
{
char op ;
double x2;
int b ;
//printf ("\n Level 5, x=%lf",*x );
// ( function
Level6(MathParser_p, x); // parsing %1,%2,%3 data and functions
//PrintToken(p);
//printf ("\n Level 5(after Level 6), x=%lf \n",*x );
if (MathParser_p->Err>=0) return;
MathParser_p->Err=-1;
return;
}
int hextoint(char buffer[])
{
return 0; //atoi (buffer);
}Friday, December 2, 2016 11:36 PM -
void MathParser::Level6( TheData *MathParser_p , double *x ) // ( function
{
int i,j ,k;
double Y=0;
double Arr[200];
double Mtx [200];
double Args[200];
// printf ("\n Level 6 x=%lf\n",*x);
if (MathParser_p->Token[0] == '%')
{
GetToken(MathParser_p); //
if (MathParser_p->TokType==TokNumber)
{
i= atoi(MathParser_p->Token)-1;
if (i<MathParser_p->FDataCount) //
{
GetToken(MathParser_p); // node number
{
if (MathParser_p->Flags[i]==1) { *x=Args[i]; }
else
{
Args[i]=MathParser_p->ArrayX[i];
*x=Args[i];
MathParser_p->Flags[i]=1;
// printf ("\n i= %d Args [i]=%lf p->Flags[i]=%d ",i, Args[i],p->Flags[i]);
}
return;
}// end if token =(-else
}
else { MathParser_p->Err=0; return; }
} //end If tokNumber
else { MathParser_p->Err=0; return; }
}//end if %
else
if (MathParser_p->Token[0]== '(')
{
GetToken(MathParser_p); //get arg in the ()
// PrintToken(p);
Level1a(MathParser_p,x); // PrintToken(p);
if (MathParser_p->Err>=0) return;
if (MathParser_p->Token[0]!= ')')
{
MathParser_p->Err=0; return;
}
}
else
switch (MathParser_p->TokType)
{
case TokName:
{ //Begin TokName
// printf ("\n p->TokType =TokName ");
// PrintToken(p);
if (strcmp(MathParser_p->Token, "pi")==0 ) { *x = pi; }
else if (strcmp(MathParser_p->Token, "e")==0 ) { *x = digE; }
else if (strcmp(MathParser_p->Token, "cos")==0 ) { ReadFunc(MathParser_p, x,1); }
else if (strcmp(MathParser_p->Token, "sin")==0 ) { ReadFunc(MathParser_p,x,2); }
else if (strcmp(MathParser_p->Token, "tg")==0 ) { ReadFunc(MathParser_p,x,3); }
else if (strcmp(MathParser_p->Token, "ctg")==0 ) { ReadFunc(MathParser_p,x,4); }
else if (strcmp(MathParser_p->Token, "arccos")==0 ) { ReadFunc(MathParser_p,x,5); }
else if (strcmp(MathParser_p->Token, "arcsin")==0 ) { ReadFunc(MathParser_p,x,6); }
else if (strcmp(MathParser_p->Token, "arctg")==0 ) { ReadFunc(MathParser_p,x,7); }
else if (strcmp(MathParser_p->Token, "arcctg")==0 ) { ReadFunc(MathParser_p,x,8); }
else if (strcmp(MathParser_p->Token, "atan")==0 ) { ReadFunc(MathParser_p,x,9); }
else if (strcmp(MathParser_p->Token, "ch")==0 ) { ReadFunc(MathParser_p,x,10); }
else if (strcmp(MathParser_p->Token, "sh")==0 ) { ReadFunc(MathParser_p,x,11); }
else if (strcmp(MathParser_p->Token, "th")==0 ) { ReadFunc(MathParser_p,x,12); }
else if (strcmp(MathParser_p->Token, "cth")==0 ) { ReadFunc(MathParser_p,x,13); }
else if (strcmp(MathParser_p->Token, "arcch")==0 ) { ReadFunc(MathParser_p,x,14); }
else if (strcmp(MathParser_p->Token, "arcsh")==0 ) { ReadFunc(MathParser_p,x,15); }
else if (strcmp(MathParser_p->Token, "arcth")==0 ) { ReadFunc(MathParser_p,x,16); }
else if (strcmp(MathParser_p->Token, "arccth")==0 ) { ReadFunc(MathParser_p,x,17); }
else if (strcmp(MathParser_p->Token, "log")==0 ) { ReadFunc(MathParser_p,x,18); }
else if (strcmp(MathParser_p->Token, "lg")==0 ) { ReadFunc(MathParser_p,x,19); }
else if (strcmp(MathParser_p->Token, "ln") ==0 ) { ReadFunc(MathParser_p,x,20); }
else if (strcmp(MathParser_p->Token, "exp")==0 ) { ReadFunc(MathParser_p,x,21); }
else if (strcmp(MathParser_p->Token, "sqr") ==0 ) { ReadFunc(MathParser_p,x,22); }
else if (strcmp(MathParser_p->Token, "sqrt")==0 ) { ReadFunc(MathParser_p,x,23); }
else if (strcmp(MathParser_p->Token, "abs") ==0 ) { ReadFunc(MathParser_p,x,24); }
else if (strcmp(MathParser_p->Token, "sign")==0 ) { ReadFunc(MathParser_p,x,25); }
else if (strcmp(MathParser_p->Token, "round")==0 ) { ReadFunc(MathParser_p,x,26); }
// else if (strcmp(p->Token, "frac") ==0 ) { ReadFunc(p,x,27); }
else if (strcmp(MathParser_p->Token, "trunc")==0 ) { ReadFunc(MathParser_p,x,28); }
// else if (strcmp(p->Token, "min")==0 ) { ReadFunc(p,x,29); }
// else if (strcmp(p->Token, "max") ==0 ) { ReadFunc(p,x,30); }
else if (strcmp(MathParser_p->Token, "floor")==0 ) { ReadFunc(MathParser_p,x,31); }
else if (strcmp(MathParser_p->Token, "ceil")==0 ) { ReadFunc(MathParser_p,x,32); }
// else if (strcmp(p->Token, "odd") ==0 ) { ReadFunc(p,x,33); }
// else if (strcmp(p->Token, "even")==0 ) { ReadFunc(p,x,34); }
//else else if (strcmp(p->Token, "integr")==0 ) { ReadFunc(p,x,35); }
else { MathParser_p->Err =0; if (MathParser_p->Err>=0) { break; }
}break;
} //End TokName
case TokReal:
case TokNumber:
// *x=atol(p->Token);
*x=atof(MathParser_p->Token); //double
// printf ("\n TokNumber ,*x=%lf \n ",*x);
break;case TokHex:
*x = hextoint(MathParser_p->Token);
break;
default: { MathParser_p->Err =0; break; }
} // End Switch-Case
GetToken(MathParser_p);
return;
}
void MathParser::ReadFunc (TheData *MathParser_p , double *x , int f)
{
printf ("\n ReadFunc, f=%d ",f);
int i;
double y=0;
GetToken(MathParser_p);
//PrintToken(p ); //cos
if (MathParser_p->Token[0]!= '(')
{
MathParser_p->Err=0;
return;
}//PrintToken(p); // cos
GetToken(MathParser_p); // (
// PrintToken(p);
Level1a(MathParser_p, x);
// printf ( "\n ReadFunc PrintToken afterLevel 1a");
// PrintToken(p);
if (MathParser_p->Err>=0) return;
//printf ("\n ReadFunc,Case , f=%d ",f);
// printf ("\n Before switch x=%lf\n",*x);
switch (f)
{
case 1 : //printf ("\n ReadFunc,Case , f=%d ",f);
//printf ("\n Before cos x=%lf\n",*x);
*x =cos( *x*AngleMode);
//printf ("\n After cos x=%lf\n",*x);
break;
case 2 : *x = sin(*x*AngleMode);
break;
case 3:
if (cos(*x*AngleMode)==0)
{
MathParser_p->Err=1;
break;
}
else
*x=tan(*x*AngleMode);
break;case 4:
if(sin(*x*AngleMode)==0)
{
MathParser_p->Err=1;
break;
}
else
*x = 1/tan(*x*AngleMode);
break;
case 5:
if ((*x>1)||(*x<-1) )
{
MathParser_p->Err=1;
break;
}
else *x = atan2(sqrt(1-(*x)*(*x)),*x)/AngleMode;
break;
case 6:
if((*x>1)||(*x<-1))
{
MathParser_p->Err=1;
break;
}
else
*x = atan2(*x,sqrt(1-(*x)*(*x)))/AngleMode;
break;
case 7: *x = atan2(*x,1)/AngleMode;
break;
case 8: *x =atan2(1,*x)/AngleMode;
break;
case 9:
if (MathParser_p->Token[0]!=',')
{
MathParser_p->Err=0;
break;
}
GetToken(MathParser_p);
Level1a(MathParser_p,&y); //arg 2
if (MathParser_p->Err>=0)
break;
*x=atan2(*x,y)/AngleMode;
break;case 10:
y = exp(*x);
*x = (y+1/y)/2;
break;
case 11:
y = exp(*x);
*x = (y-1/y)/2;
break;
case 12:
y = exp(2* (*x) );
*x = (y-1)/(y+1);
break;
case 13:
if (*x==0)
{
MathParser_p->Err=1;
break;
}
else
{
y = exp(2*(*x));
*x = (y+1)/(y-1) ;
}
break;case 14:
if (*x<1)
{
MathParser_p->Err=1;
break;
}
else
*x = log(*x+sqrt(*x*(*x)-1))/log(digE);
break;
case 15:
*x= log(*x+sqrt(*x*(*x)+1))/log(digE);
break;
case 16:
if ((*x>=1)||(*x<=-1))
{
MathParser_p->Err=1;
break;
}
else
*x =0.5*( log((1+*x)/(1-*x))/log(digE) ) ;
break;
case 17:
if ((*x<=1)&&(*x>=-1))
{
MathParser_p->Err=1;
break;
}
else
*x = 0.5*( log((*x+1)/(*x-1))/log (digE) );
break;
case 18:
if (MathParser_p->Token[0]!=',')
{
MathParser_p->Err=0;
break;
}
GetToken(MathParser_p);
//PrintToken(p );
Level1a(MathParser_p,&y);
if (MathParser_p->Err>=0) { break; }
if ((*x<=0)||(y<=0))
{
MathParser_p->Err=1;
break;
}
*x = log(y)/log(*x);
break;
case 19:
if (*x<=0)
{
MathParser_p->Err=1;
break;
}
else
*x = log10(*x);
break;
case 20:
if (*x<=0)
{
MathParser_p->Err=1;
break;
}
else *x= log(*x);
break;
case 21: *x = exp(*x);
break;
case 22: *x =(*x)*(*x);
break;
case 23:
if (x<0)
{
MathParser_p->Err=1;
break;
}
else *x = pow(*x,0.5);
break;
case 24: if (*x<0) {*x = -(*x);
break;
} //else {x =*x;break; }
case 25: if (*x<0) { *x =-1; }
else if (*x>0) {*x=1; }
break;
case 26:
case 27:
case 28:
{
y=1;
if (MathParser_p->Token[0]=',')
{
GetToken(MathParser_p);
//PrintToken(p );
Level1a(MathParser_p, &y);
if (MathParser_p->Err>=0) break;
}
switch (f) {
case 26: *x = y*round(*x/y);
break;
case 27: *x = y*((*x/y)-floor (*x/y));
break;
default: *x = y*trunc(*x/y);
break;
}}
case 29:
case 30:
while (MathParser_p->Token[0]==',')
{
GetToken(MathParser_p);
// PrintToken(p );
Level1a(MathParser_p,&y);
if (MathParser_p->Err>=0)
break;
if (f==29)
{
if (*x>y)
*x=y;
else if (*x<y) {*x=y; }
}
}break;
case 31: //floor
*x = floor(*x);
break;
case 32: //ceil
*x = ceil(*x);
break;
case 33:
//odd
// if ((x%2)!=0) { *x =1; }
// else {*x=0; }
break;
case 34:
//even
// if ((x%2)==0) { *x =1 ; }
// else {*x=0; }
break;
//case 35:
//
//GetToken(p);
// PrintToken(p );
// Level1a(p,&a);
// if (p->Err>=0)
// break;
//GetToken(p);
// PrintToken(p );
// Level1a(p,&b);
// if (p->Err>=0)
// break;
// getfxfromstring(p,f(x))
// *x=getromberg(a,b, f(x));
// break;
} /*end of switch-case */if (MathParser_p->Token[0]!= ')') MathParser_p->Err=0;
return;
}int main()
{
TheData *p1;
MathParser MParse;
double y;
// MParse.doInit();
MParse.doInputMathStr();
printf(" to do input vars");
getch();
MParse.doInputVars();
printf(" to do calc ");
getch();
MParse.doCalc();
printf(" to do calculated ");
getch();
/*
TheData *p;
int Error;
double y1;
double Arg[200];MParse.doClear( p);
MParse.doInputMathStr1( p );MParse.doInputVars1( p);
MParse.doCalc1(p, &y1, &Error );
printf("%lf",y1 );
*/
//printf("%lf",y );
return 0;
}Friday, December 2, 2016 11:39 PM -
It gets error . How to return data in { _p->Line[i]=string1[i]; } in (Error if i use void MathParser::doInputMathStr1(TheData *_p )) { ... return;} )
TheData* MathParser::doInputMathStr1(TheData *_p1 )
{
int i;
TheData *_p ;
_p =_p1;
char SafeLine [1024];
char string1[1024];
printf("\n Input math string ");
for (i=0;i<1024;i++) { string1[i]='\0'; _p->Line[i]='\0'; }
//scanf("%s",string1);
string1[0]='1';
string1[1]='+';
string1[2]='%';
string1[3]='1';
printf("\n %s",string1);
for (i=0;i< strlen(string1) ;i++) { _p->Line[i]=string1[i]; }_p->Line[strlen( _p->Line)]='\x01';
for (i=0;i<strlen( _p->Line);i++) { SafeLine[i]= _p->Line[i];}
for (i=0;i<strlen( _p->Line);i++)
{
if (( _p->Line[i]=='\n')||( _p->Line[i]=='\r'))
_p->Line[i]=' ';
}return _p ;
}Friday, December 2, 2016 11:44 PM