C PROGRAMMINGDATA STRUCTURES

postfix and prefix expression evaluation

expression evaluation postfix and prefix

In this part we will create a Stack and corresponding functions for stack
#include<stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<string.h>

struct stack
{
int data;
struct stack *link;
};

struct stack *top=NULL;

void push(int c)
{
struct stack *temp;
temp=(struct stack *)malloc(sizeof(struct stack));
//strcpy(temp->data,c); strcpy () works string type data
temp->data=c;
if(top==NULL)
temp->link=NULL;
else
temp->link=top;
top=temp;
printf("Pushed to stack %dn",temp->data);
}

int pop()
{
int data;
//strcpy(data,top->data);
data=top->data;
printf("Poped from Stack %d n",top->data);
top=top->link;
return data;
}
In this part of code we will create a function/method for implementing postfix evaluation
int postfixEvaluation(char postfix[])	
{
int i=0;
int op1,op2;
char c;

while(postfix[i]!='')
{
c = postfix[i];
     printf("Item Scanned %cn",postfix[i]);
       if(postfix[i]>='0' && postfix[i]<='9')
         {
             push(c-48); //sir here i subtracted 48 from pushed elemet because                     //it was giving pushed element + 48 like for 1 it gave 49
}
        else if(c == '+' || c =='-' || c =='*' || c == '/')
        {
     op2=pop();
     op1=pop();


switch(c)
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;

case '*':
push(op1*op2);
break;

case '/':
push(op1/op2);
break;
}
}
i++;
getch();
}
return pop();
}
In this part of code we will create a function/method for implementing prefix evaluation

int prefixEvaluation(char prefix[])
{
int i=0;
int op1,op2;
char c;
while(prefix[i]!='')
{
i++;
}
i--;
while (i>=0)
{
c = prefix[i];
printf("Item Scanned %c n",prefix[i]);
if(prefix[i]>='0' && prefix[i]<='9')
{
push(c-48);
}
else if(c == '+' || c =='-' || c =='*' || c == '/')
{
op1=pop();
op2=pop();

switch(c)
{
case '+':
push(op1+op2);
break;

case '-':
push(op1-op2);
break;
case '*':

push(op1*op2);
break;
case '/':

push(op1/op2);
break;
}

}
getch();
i--;
}
return pop();
}
In this part of code we will create main function to take input form user the expression and implement postfix evaluation or prefix evaluation as specified by the user using menu options

void main()
{
char postfix[100],prefix[100];
int choice;

do
{
printf("nn*********Menu************nn");
printf("Press 1 for postfix evaluationnn");
printf("press 2 for Prefix evaluationnn");
printf("any other key to exitn");
scanf("%d",&choice);
switch(choice)

{
case 1:
printf("Enter postfix Epression : ");
scanf("%s",postfix);
printf("Postfix Expression %sn",postfix);
printf("n Result after expression evaluation is : %d",postfixEvaluation(postfix));
break;

case 2:
printf("nEnter prefix Expressionn");
scanf("%s",prefix);
printf("prefix Expression=%sn",prefix);
printf("ninfix after conversion of prefix is: %d", prefixEvaluation(prefix));
break;

default:
exit(0);
}
}while(choice);
getch();
}
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button