Expression storage
/******************************************************************************
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 <string.h>
#define OPERAND 255
#define OPERATOR -255
enum Priority
{
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 = sym;
symtbl[max_sym].type = typ;
symtbl[max_sym].value = val;
max_sym++;
}
int Symbol_search(char *sym)
{
for(int i=0; i<max_sym; i++)
{
if(strcmp(sym,symtbl[max_sym].symbol)==0)
{
return i;
}
}
return -1;
}
struct ListNode
{
int isOperatorOrOperand;
int priority;
struct Symbol *str;
char op;
};
struct ListNode prQue[1024]; // Priority Queue
int size =0;
int maximum()
{
}
void fill(int isType,int pr, struct Symbol *s, char op )
{
prQue[size].isOperatorOrOperand = isType;
prQue[size].priority = pr;
if(isType == OPERAND)
{
prQue[size].str = s;
prQue[size].isOperatorOrOperand = OPERAND;
}
else
{ prQue[size].op = op;
prQue[size].isOperatorOrOperand = OPERATOR;
}
size++;
}
int insert(char *str)
{
static int blocklevel = LAST_OP - 1;
static int priority =1;
char *s;
char string_c[100];
if(*str == '*')
{
priority = MUL_OP;
fill(OPERATOR,priority + blocklevel,NULL, MUL_OP );
return 1;
}
else if(*str == '/')
{
priority = DIV_OP;
fill(OPERATOR,priority + blocklevel,NULL, DIV_OP );
return 1;
}
else if(*str == '+')
{
priority = ADD_OP;
fill(OPERATOR,priority + blocklevel,NULL, ADD_OP );
return 1;
}
else if(*str == '-')
{
priority = SUB_OP;
fill(OPERATOR,priority + blocklevel,NULL, SUB_OP );
return 1;
}
else if(*str == '(')
{
blocklevel++;
return 1;
}
else if(*str == ')')
{
blocklevel--;
return 1;
}
else if(isalpha(*str))
{ int i=0,index;
s = str;
while(isalpha(*str) || isdigit(*str))
{ str++;
i++;
}
strncpy(string_c,s,i);
string_c[i]='\0';
index=Symbol_search(string_c);
if(index != -1)
{
fill(OPERAND,priority + blocklevel,&symtbl[max_sym] ,'\0');
}
else
{
printf("Symbol %s not found !",string_c );
}
return i;
}
}
int main()
{
char arr[100] = "a+b-c*d";
for(int i=0; i< strlen(arr); )
{
i+= insert(arr+i);
}
return 0;
}
Comments
Post a Comment