/*--------------------------------
convert infix to postfix
--------------------------------*/
void infixToPostfix(char infix[max], char postfix[max]){
	emptyStack();
	int i=0,j=0;
	while(infix[i]!='\0'){
		if(isOperator(infix[i])!=1){ // if is operand
			postfix[j++]=infix[i];
		}else if(infix[i]== '(')
			push(infix[i]);
		else if(infix[i]==')'){
			while(stack[top]!='(')
				postfix[j++]=pop();
			top--;
		}else{
			while(top!=-1 && prcd(infix[i])<=prcd(stack[top]))
				postfix[j++]=pop();
			push(infix[i]);
		}
		i++;
	}
	while(top!=-1)
		if(stack[top]!=')' || stack[top]!='(')
			postfix[j++]=pop();
		else top--;
	postfix[j]='\0';
}
void postfix(char *infix,char *result)
{
int i=0,under,outpos=0;
char topsymb='+',symb;
p.top=-1;
while(infix[i]!='\0')
{
symb=infix[i++];
if(isoperand(symb)==1)
result[outpos++]=symb;
else
{
pop(&p,&topsymb,&under);
while(under==0 && prcd(topsymb,symb))
{
result[outpos++]=topsymb;
pop(&p,&topsymb,&under);
}
if(under==0)
push(&p,topsymb);
if(under==1 || (symb!=')'))
push(&p,symb);
else
pop(&p,&topsymb,&under);
}
}
while(empty(&p)!=1)
{
pop(&p,&topsymb,&under);
result[outpos++]=topsymb;
}
result[outpos]='\0';
}
Exemple #3
0
//Infix to Postfix conversion
void posfix(char infix[], char postr[]) {
    int position, und;
    int outpos = 0;
    char topsymb = '+';
    char symbol;
    OPERANDSTACK opstk;
    opstk.top = -1;
    for (position = 0; (symbol = infix[position]) != '\0'; position++) {
        if (isOperand(symbol))
        {
            postr[outpos++] = symbol;
        } else {
            popAndTest(&opstk, &topsymb, &und);
            while (!und && prcd(topsymb, symbol)) {
                postr[outpos++] = topsymb;
                popAndTest(&opstk, &topsymb, &und);
            }
            if (!und) {
                push(&opstk, topsymb);
            }
            if (und || (symbol != ')')) {
                push(&opstk, symbol);
            } else {
                topsymb = pop(&opstk);
            }
        }
    }
    while (!isEmpty(&opstk)) {
        postr[outpos++] = pop(&opstk);
    }
    postr[outpos] = '\0';
    return;
}
Exemple #4
0
inline void convertip(char infix[],char postfix[])
{
  int i,symbol,j=0;
  stack[++top]='#';
  for(i=0;i<strlen(infix);i++)
  {
    symbol=infix[i];
    if(isoperator(symbol)==0)
    {
      postfix[j]=symbol;
      j++;
    }
    else{
      if(symbol=='(')push(symbol);
      else if(symbol==')')
      {
        while(stack[top]!='(')
        {
          postfix[j]=pop();
          j++;
        }
        pop();//pop out (.
      }
      else{
        if(prcd(symbol)>prcd(stack[top]))
        push(symbol);
        else{
          while(prcd(symbol)<=prcd(stack[top]))
          {
            postfix[j]=pop();
            j++;
          }
          push(symbol);
        }//end of else.
      }//end of else.
    }//end of else.
  }//end of for.
  while(stack[top]!='#')
  {
    postfix[j]=pop();
    j++;
  }
  postfix[j]='\0';//null terminate string.
}
Exemple #5
0
//--------------------------------------------------------
void infixtoprefix(char infix[20],char prefix[20]) 
{
	int i,j=0;
	char symbol;
	stack[++top]='#';
	reverse(infix);
	for (i=0;i<strlen(infix);i++) {
		symbol=infix[i];
		if (isOperator(symbol)==0) {
			prefix[j]=symbol;
			j++;
		} else {
			if (symbol==')') {
				push(symbol);
			} else if(symbol == '(') {
				while (stack[top]!=')') {
					prefix[j]=pop();
					j++;
				}
				pop();
			} else {
				if (prcd(stack[top])<=prcd(symbol)) {
					push(symbol);
				} else {
					while(prcd(stack[top])>=prcd(symbol)) {
						prefix[j]=pop();
						j++;
					}
					push(symbol);
				}
				//end for else
			}
		}
		//end for else
	}
	//end for for
	while (stack[top]!='#') {
		prefix[j]=pop();
		j++;
	}
	prefix[j]='\0';
}
void convert(char infix[size],char postfix[size])
{
    int i,j=0;
    char ch,op;
    struct stack s;
    s.top=-1;
    for(i=0; infix[i]!='\0'; i++)
    {
     //   display(&s);
        ch=infix[i];
        if(isopnd(ch)==1)
        {
            postfix[j]=ch;
            j++;
        }
        else if(ch=='(')
        {
            push2(&s,ch);
        }
        else if(ch==')')
        {
            char k=pop2(&s);
            while(k!='(')
            {
                postfix[j]=k;
                j++;
                k=pop2(&s);
            }
            //pop(&s);
        }
        else
        {
            while(!isempty(s))
            {
                //result=prcd(ch,s.arr[s.top]);
                if(prcd(s.arr[s.top],ch)||ch=='(')
                    break;
                op=pop2(&s);
                postfix[j]=op;
                j++;
            }
            push2(&s,ch);
        }
    }
    while(!isempty(s))
    {
        op=pop2(&s);
        postfix[j]=op;
        j++;
    }
    postfix[j]='\0';
}
Exemple #7
0
void convert(stack *s,char infx_str[])
{
    char pstfx_str[30];
    int i,j;
    char z;
    for(i=0,j=0;infx_str[i]!='\0';i++)
    {
        if(infx_str[i]>='0' && infx_str[i]<='9' || infx_str[i]>='a' && infx_str[i]<='b')
            pstfx_str[j++]=infx_str[i];
        else
        {
            switch(infx_str[i])
            {
                case '(':
                    push(s,infx_str[i]);
                    break;
                case ')':
                        do{
                            z=pop(s);
                            if(z!='(')
                                pstfx_str[j++]=z;
                        }while(z!='(');
                        break;
                default:
                    while(!isempty(s) && prcd(peek(s))>=prcd(infx_str[i]))
                        pstfx_str[j++]=pop(s);
                    push(s,infx_str[i]);
            }
        }
    }
    while(!isempty(s))
        pstfx_str[j++]=pop(s);
    pstfx_str[j]='\0';
    printf("\npostfix string is ");
        for(i=0;pstfx_str[i]!='\0';i++)
            printf("%c",pstfx_str[i]);
    //return pstfx_str;
}
Exemple #8
0
/*To Covert Infix To Prefix*/
void intopre(char str1[],char pre[])
{
 	 STK s1;
int len,flag;
len=strlen(str1);
int check=0,cnt=len-1,pos=0;
char elem;

while(cnt>=0)  /*while condition*/
{
flag=0;
if(isoperand(str1[cnt]))   /*Checking for Operand*/
{
printf("%c",str1[cnt]);
cnt--;
pos++;
}
else
{
check=prcd(str1[cnt]);
while(check==false)
{
pre[pos]=str1[cnt];
flag=1;
pos++;
cnt--;
}
if(flag==0)
{
elem=pop(&s1);
printf("%c",elem);
}
}

}
}
void convert(char infix[50],char prefix[50])
{
    char temp[50],out[50];
    int i,j;
    struct stack *st=(struct stack*)malloc(sizeof(struct stack));
    (*st).tos=-1;
    //reverse the infix expression
    for(i=strlen(infix)-1,j=0;i>=0;i--,j++)
    {
        temp[j]=infix[i];
    }
    temp[j]='\0';

    printf("\nThe temp expression is: %s\n",temp);
    for(i=0,j=0;temp[i]!='\0';i++)
    {
        /*if temp[i] is an operand write it to out[j]
        if it is an operator(op) and stack is empty, push it onto stack and if stack is not empty , pop from stack
        and compare the popped element and operator(op) precedence .if the precedence of popped operator is
        greater than that of the operator(op) write popped opearator into out[j] otherwise push operator(op)
        onto the stack. repeat the process
        */
        if(isopnd(temp[i]))
        {
            out[j]=temp[i];
            j++;
        }
        else if(temp[i]==')')
        {
            push2(st,temp[i]);
        }
        else if(temp[i]=='(')
        {
            char k=pop2(st);
            while(k!=')')
            {
                out[j]=k;
                j++;
                k=pop2(st);
            }
            pop2(st);
        }
        else
        {
            char op1;
            int flag=0;
            while(1)
            {
            op1=temp[i];
            if(isempty((st)))
            {
                push2(st,op1);
                flag=1;
                break;
            }
            else
            {
                char op2=pop2(st);

                if(prcd(op1,op2)||op2=='(')   //prcd op1>op2
                {
                    push2(st,op2);

                    break;
                }
                else
                {
                    out[j]=op2;
                    j++;
                }
            }
            }

        if(flag==0)
            push2(st,op1);
        }

    }
    while(!isempty(st))
    {
        printf("in");
        out[j]=pop2(st);
        j++;
    }
    out[j]='\0';
    //printf("\nThe output expression is: %s\n",out);
    //reverse the output string
    for(i=strlen(out)-1,j=0;i>=0;i--,j++)
    {
        prefix[j]=out[i];
    }
    prefix[j]='\0';
    return;


}