Infix To Prefix Conversion
Infix To Prefix Conversion
Infix To Prefix Conversion
h>
#include<string.h>
#include<limits.h>
#include<stdlib.h>
# define MAX 100
int top = -1;
char stack[MAX];
// Push function here, inserts value in stack and increments stack top by 1
void push(char item) {
if (isFull())
return;
top++;
stack[top] = item;
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
// Here, if we scan character is an ‘)’, we need to pop and print from the stack
// do this until an ‘(‘ is encountered in the stack.
else if (expression[i] == ')')
{
while (!isEmpty(stack) && peek(stack) != '(')
expression[++j] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1; // invalid expression
else
pop(stack);
}
else // if an opertor
{
while (!isEmpty(stack) && precedence(expression[i]) <=
precedence(peek(stack)))
expression[++j] = pop(stack);
push(expression[i]);
}
expression[++j] = '\0';
temp[j--]='\0';
while(exp[i]!='\0')
{
temp[j] = exp[i];
j--;
i++;
}
strcpy(exp,temp);
}
void brackets(char* exp){
int i = 0;
while(exp[i]!='\0')
{
if(exp[i]=='(')
exp[i]=')';
else if(exp[i]==')')
exp[i]='(';
i++;
}
}
void InfixtoPrefix(char *exp){
// reverse string
reverse(exp);
//change brackets
brackets(exp);
//get postfix
getPostfix(exp);
// reverse string again
reverse(exp);
}
int main()
{
printf("The infix is: ");
return 0;
}