We can implement a valid infix expression to a valid postfix expression using stacks in c as shown:
//C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20
char stack[MAX];
int istack[MAX];
int top = -1;
char pop();
void push(char item);
int prcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;break;
case '*':
case '/':return 4;break;
case '$':return 6;break;
case '(':
case ')':
case '#':return 1;break;
}
}
int isoperator(char symbol)
{
if(symbol=='+'||symbol=='-'||symbol=='*'||symbol=='/'||symbol=='$')
return 1;
else if(symbol==')'||symbol=='(')
return -1;
else
return 0;
}
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.
}
long int oper(char c,int op1,int op2)
{
switch(c)
{
case '+':return op2+op1+48;
case '-':return op2-op1+48;
case '*':return op2*op1+48;
case '/':return op2/op1+48;
case '$':return pow(op2,op1)+48;
default: printf("Invalid operation!!");exit(1);
}
}
int eval(char postfix[])
{
int i;
char c;
float value ,op1,op2;
for(i=0;(c=postfix[i])!='\0';i++)
{
if(isoperator(postfix[i])==0)
push(postfix[i]);
else
{
op1=pop();
op2=pop();
op1-=48;
op2-=48;
value = oper(c,op1,op2) ;
push(value);
}
}
return (pop()-48);
}
int main()
{
char infix[20],postfix[20];
int i;
char res;
do
{
printf("\nEnter the valid infix string: ");
fflush(stdin);
gets(infix);
for(i=0;infix[i]!='\0';i++)
{
if(isoperator(infix[i])==1 && isoperator(infix[i+1])==1)
{
printf("\n\tINPUT ERROR : Invalid Infix Expression !!");
getch();
exit(0);
}
}
convertip(infix,postfix);
printf("The corresponding postfix string is: ");
puts(postfix);
printf("\nThe value of postfix expression : %d",eval(postfix));
printf("\n\n\tDo you want to continue -[y/n]? ");
scanf("%c",&res);
}while(res =='y'||res=='Y');
return 0;
}
void push(char item)
{
top++;
stack[top]=item;
}
char pop()
{
char a;
a=stack[top];
top--;
return a;
}
check out for more expression conversion codes
ReplyDeletehttp://purecodes.blogspot.in/search/label/Stack
Hey there, You have done a fantastic job. I will certainly digg it and personally suggest to my friends. I’m confident they’ll be benefited from this website.
ReplyDeleteC Programming Courses in Chennai
Distributor Kuota
ReplyDeletePT Lampung Service
Service Center HP Indonesian
Service Center iPhone Indonesian
Service Center Acer Indonesian
PT Lampung Service
Nice Blog! Thanks for providing the knowledgeable content, it was a great post to developing my skills. Well done...!
ReplyDeletePrimavera Training in Chennai
Primavera Course in Chennai
Pega Training in Chennai
Unix Training in Chennai
Spark Training in Chennai
Power BI Training in Chennai
Tableau Training in Chennai
Oracle Training in Chennai
Oracle DBA Training in Chennai
Primavera Training in OMR
Primavera Training in T Nagar
Hello! I could have sworn I’ve visited this blog before but after going through some of the articles I realized it’s new to me. Anyways, I’m certainly happy views I found it and I’ll be book-marking it and checking back regularly!
ReplyDeleteRekordBox DJ 6.6.5 Crack 2023 is a music management application for Pioneer D products. It can transfer music files from Windows. Rekordbox Dj License Key
ReplyDeleteKindly Visit my website for cracked software, Windows & Mac. Download Now Latest Versions.
ReplyDeleteTally ERP 9 Crack