Write a prefix method to accept an infix string and create the prefix form of that string, assuming that the string is read from right to left and that the prefix string is created from right to left.
Write a prefix method to accept an infix string and create the prefix form of that string, assuming that the string is read from right to left and that the prefix string is created from right to left.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
char stack[MAX];
int top = -1;
char pop();
void push(char item);
int precedence(char symbol)
{
switch(symbol)
{
case ‘+’:
case ‘-‘:
return 2;
case ‘*’:
case ‘/’:
return 4;
case ‘^’:
case ‘$’:
return 6;
case ‘(‘:
case ‘)’:
case ‘#’:
return 1;
}
}
int isoperator(char symbol)
{
switch(symbol)
{
case ‘+’:
case ‘-‘:
case ‘*’:
case ‘/’:
case ‘^’:
case ‘$’:
case ‘(‘:
case ‘)’:
return 1;
default:
return 0;
}
}
void convert_infix_to_prefix(char infix[],char prefix[])
{
int i,symbol,j=0;
char test[MAX];
infix=strrev(infix);
stack[++top]=’#’;
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();//pop out (.
}
else
{
if(precedence(symbol)>precedence(stack[top]))
{
push(symbol);
}
else
{
while(precedence(symbol)<=precedence(stack[top]))
{
prefix[j]=pop();
j++;
}
push(symbol);
}//end of else.
}//end of else.
}//end of first else.
}//end of for.
while(stack[top]!=’#’)
{
prefix[j]=pop();
j++;
}
prefix[j]=’