locked
MathParser RRS feed

  • 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     7

      double 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;
    }

    Saturday, February 21, 2015 8:40 PM

All replies

  • /* ************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;
    }

    Saturday, February 21, 2015 8:41 PM