Expression storage complete

/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define OPERAND 255
#define OPERATOR -255
#define INT 33
#define INIT_PRIORITY -1
enum Priority  
{
    ZERO_OP,
    SUB_OP,
    ADD_OP,
    DIV_OP,
    MUL_OP,
    LAST_OP
};
struct Symbol
{
   char *symbol;
   char type;
   char *value;
};
struct Symbol symtbl[256];
int max_sym=0;
void Symbol_insert(char *sym,char typ,char *val)
{
    symtbl[max_sym].symbol =(char*) malloc(sizeof(char)*strlen(sym));
    strncpy(symtbl[max_sym].symbol,sym,strlen(sym));
    symtbl[max_sym].type = typ;
    symtbl[max_sym].value = (char*)malloc(sizeof(char)*strlen(val));
    strncpy(symtbl[max_sym].value,val, strlen(sym));
    max_sym++;
}
int Symbol_search(char *sym)
{
    for(int i=0; i<max_sym; i++)
    {
        if(strcmp(sym,symtbl[i].symbol)==0)
        {
            
            return i;
        }
    }
   return -1;
}
struct ListNode
{
    int isOperatorOrOperand;
    int priority;
    struct Symbol *str;
    char op;
    struct ListNode* next;
};
struct ListNode *tail,*head ; // Priority Queue
int size =0;
int maximum()
{
    int i=size - 1;
    int max = -1;
    while(i >= 0)
    {
        if(max < head->priority)
          max = head->priority;
        
        head = head->next;
        i--;  
    }
    return max;
}
void fill(int isType,int pr, struct Symbol *s, char op )
{
  struct ListNode *ptr = (struct ListNode*)malloc(sizeof(struct ListNode));
  ptr->isOperatorOrOperand = isType;
  ptr->priority = pr;
  if(isType ==  OPERAND)
  {
    ptr->str = s;
    ptr->isOperatorOrOperand = OPERAND;
  }
  else
  {  ptr->op = op;
     ptr->isOperatorOrOperand = OPERATOR;
  }
  if(tail == NULL)
  {
      tail = ptr;
      head = ptr;
  }
  else
  {
      tail->next =ptr;
      tail = tail->next;
  }
  size++;
}
int getNextPriority(char* str)
{
    int pr=0;
      char *s =str;
      printf(" forward looking char is %c\n", *s);
    while(*s != '+' && *s != '-' && *s != '/' && *s != '*')
     s++;
     printf(" forward looking char is %c\n", *s);
    switch(*s)
    {
      case '+':
      pr=ADD_OP;
      break;
      case '-':
      pr=SUB_OP;
      break;
      case '/':
      pr=DIV_OP;
      break;
      case '*':
      pr=MUL_OP;
      break;
    }
    return pr;
}
int mapBlocklevel(int blocklevel)
{
    return blocklevel + 4;
}
int insert(char *str)
{
    static int blocklevel = 0;
   static int priority =INIT_PRIORITY;
   char *s;
   char string_c[100];
   
   if(*str == '*')
   {
       priority = MUL_OP;
       if(blocklevel > 0)
       {  printf("Symbol founded is %c, priority is %d\n !",*str , priority+mapBlocklevel(blocklevel));
              fill(OPERATOR,priority + mapBlocklevel(blocklevel),NULL ,MUL_OP);
       } else
       {
         fill(OPERATOR,priority + blocklevel,NULL, MUL_OP );
       }
       
       return 1;
   }
   else if(*str == '/')
   {
       priority = DIV_OP;
       if(blocklevel > 0)
       {  printf("Symbol founded is %c, priority is %d\n !",*str , priority+mapBlocklevel(blocklevel));
              fill(OPERATOR,priority + mapBlocklevel(blocklevel),NULL ,DIV_OP);
       } else
       {
         fill(OPERATOR,priority + blocklevel,NULL, DIV_OP );
       }
       return 1;
   }
   else if(*str == '+')
   {
       priority = ADD_OP;
       
          if(blocklevel > 0)
           {  printf("Symbol founded is %c, priority is %d\n !",*str , priority+mapBlocklevel(blocklevel));
              fill(OPERATOR,priority + mapBlocklevel(blocklevel),NULL ,ADD_OP);
           } else
           {
               printf("Symbol founded is %c, priority is %d\n !", *str , priority);
               fill(OPERATOR,priority,NULL ,ADD_OP);
           }
       
       return 1;
   }
   else if(*str == '-')
   {
       priority = SUB_OP;
          if(blocklevel > 0)
           {  printf("Symbol founded is %c, priority is %d\n !",*str , priority+mapBlocklevel(blocklevel));
              fill(OPERATOR,priority + mapBlocklevel(blocklevel),NULL ,SUB_OP);
           } else
           {
                  fill(OPERATOR,priority + blocklevel,NULL, SUB_OP );
           }
       return 1;
   }
   else if(*str == '(')
   {
       blocklevel++;
       priority = getNextPriority(str+1);
       return 1;
   }
   else if(*str == ')')
   {
       blocklevel--;
       priority = getNextPriority(str+1);
       return 1;
   }
   else if(isalpha(*str))
   {   int i=0,index=-1;
       s = str;
       
       while(isalpha(*str) || isdigit(*str))
       {  str++;
           i++;
       }
       strncpy(string_c,s,i);
       string_c[i]='\0';
        printf("Symbol inserted is %s\n !",string_c );
       string_c[i+1]='\0';
       index=Symbol_search(string_c);
       if(index != -1)
       {
           if(priority == INIT_PRIORITY)
              priority = getNextPriority(str);
           else if( *(str+1) != '\0' &&  priority < getNextPriority(str))
              priority = getNextPriority(str);
           if(blocklevel > 0)
           {  printf("Symbol founded is %s, priority is %d\n !",string_c , priority+mapBlocklevel(blocklevel));
              fill(OPERAND,priority + mapBlocklevel(blocklevel),&symtbl[max_sym] ,'\0');
           } else
           {
               printf("Symbol founded is %s, priority is %d\n !",string_c , priority);
               fill(OPERAND,priority,&symtbl[max_sym] ,'\0');
           }
           
       }
       else
       {
          printf("Symbol %s not found !",string_c );
       }
       
       return i;
   }
return 0;        
}
int main()
{
    int i;
    char arr[] = "a+b-c*c-d";
    char a[]="a";
    char b[]="b";
    char c[]="c";
    char d[]="d";
    char a_value[] ="9";
    char b_value[] ="3";
    char c_value[] ="2";
    char d_value[] ="1";
    Symbol_insert(a,INT , a_value);
    Symbol_insert(b,INT ,b_value);
    Symbol_insert(c,INT ,c_value);
    Symbol_insert(d,INT ,d_value);
    for( i=0; i< strlen(arr); )
    {
        printf("insert is %s\n",arr );
        insert(arr+i);
        i++;
    }
    printf("\nmaximum value is %d\n", maximum());
    return 0;
}

Comments