#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define SIZE 100
char stack[SIZE],top=-1;
void push(char item)
{
if(top>=SIZE-1)
printf("\n stack Overflow");
else
{
top=top+1;
stack[top]=item;
}
}
char pop()
{
char x;
if(top<0)
{
printf("\n stack underflow");
getchar();
}
x=stack[top];
top=top-1;
return(x);
}
int precedence(char symbol)
{
if(symbol=='^') return 3;
else if(symbol=='*' || symbol=='/') return 2;
else if(symbol=='+' || symbol=='-') return 1;
else return 0;
}
int isOperator(char item)
{
if(item=='+' || item =='-' || item=='*' || item=='/' || item=='^') return 1;
else return 0;
}
void infixToPostfix(char infix[], char postfix[])
{
int i,j;
char item,x;
push('(');
strcat(infix,")");
i=0,j=0;
item=infix[i];
while(item!='\0')
{
if(item=='(')
{
push(item);
}
else if(isdigit(item) || isalpha(item))
{
postfix[j]=item;
j++;
}
else if(isOperator(item)==1)
{
x=pop();
while(isOperator(x)==1 && precedence(x)>=precedence(item))
{
postfix[j]=x;
j++;
x=pop();
}
push(x);
push(item);
}
else if(item==')')
{
x=pop();
while(x!='(')
{
postfix[j]=x;
j++;
x=pop();
}
}
else
{
printf("\n Invalid expression");
return;
}
i++;
item=infix[i];
}//end while
if(top>0)
{
printf("\n Invalid expression");
return;
}
postfix='\0';
}
int main()
{
int choice;
char item,i=0;
char infix[SIZE],postfix[SIZE],prefix[SIZE];
printf("\n Enter Infix Expression:");
gets(infix);
printf("press 1->Infix to postfix evaluation \n 2->Infix to prefix Evaluation");
scanf("%d",&choice);
switch(choice)
{
case 1:
infixToPostfix(infix,postfix);
printf("\n Postfix Expression");
puts(postfix);
break;
case 2:
/*reverse the Infix and replace '(' by ')' then find as postfix then once again reverse it found prefix*/
strrev(infix);
i=0;
item=infix[i];
while(infix[i]!='\0')
{
if(infix[i]=='(')
infix[i]=')';
else if(infix[i]==')')
infix[i]='(';
i++;
}
infixToPostfix(infix,postfix);
printf("\n Prefix Expression for given Infix:");
puts(strrev(postfix));
break;
default:
printf("\n Wrong choice");
}
return 0;
}
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define SIZE 100
char stack[SIZE],top=-1;
void push(char item)
{
if(top>=SIZE-1)
printf("\n stack Overflow");
else
{
top=top+1;
stack[top]=item;
}
}
char pop()
{
char x;
if(top<0)
{
printf("\n stack underflow");
getchar();
}
x=stack[top];
top=top-1;
return(x);
}
int precedence(char symbol)
{
if(symbol=='^') return 3;
else if(symbol=='*' || symbol=='/') return 2;
else if(symbol=='+' || symbol=='-') return 1;
else return 0;
}
int isOperator(char item)
{
if(item=='+' || item =='-' || item=='*' || item=='/' || item=='^') return 1;
else return 0;
}
void infixToPostfix(char infix[], char postfix[])
{
int i,j;
char item,x;
push('(');
strcat(infix,")");
i=0,j=0;
item=infix[i];
while(item!='\0')
{
if(item=='(')
{
push(item);
}
else if(isdigit(item) || isalpha(item))
{
postfix[j]=item;
j++;
}
else if(isOperator(item)==1)
{
x=pop();
while(isOperator(x)==1 && precedence(x)>=precedence(item))
{
postfix[j]=x;
j++;
x=pop();
}
push(x);
push(item);
}
else if(item==')')
{
x=pop();
while(x!='(')
{
postfix[j]=x;
j++;
x=pop();
}
}
else
{
printf("\n Invalid expression");
return;
}
i++;
item=infix[i];
}//end while
if(top>0)
{
printf("\n Invalid expression");
return;
}
postfix='\0';
}
int main()
{
int choice;
char item,i=0;
char infix[SIZE],postfix[SIZE],prefix[SIZE];
printf("\n Enter Infix Expression:");
gets(infix);
printf("press 1->Infix to postfix evaluation \n 2->Infix to prefix Evaluation");
scanf("%d",&choice);
switch(choice)
{
case 1:
infixToPostfix(infix,postfix);
printf("\n Postfix Expression");
puts(postfix);
break;
case 2:
/*reverse the Infix and replace '(' by ')' then find as postfix then once again reverse it found prefix*/
strrev(infix);
i=0;
item=infix[i];
while(infix[i]!='\0')
{
if(infix[i]=='(')
infix[i]=')';
else if(infix[i]==')')
infix[i]='(';
i++;
}
infixToPostfix(infix,postfix);
printf("\n Prefix Expression for given Infix:");
puts(strrev(postfix));
break;
default:
printf("\n Wrong choice");
}
return 0;
}
No comments:
Post a Comment