#include<stdio.h>
#include<conio.h>
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
int s[SIZE];
int top=-1; /* Global declarations */
pushe(int elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}
int pope()
{ /* Function for POP operation */
return(s[top--]);
}
struct stack
{
char optr;
struct stack *next;
};
typedef struct stack stack;
int isoperand(char c)
{
// return((c>='A'&&c<='Z')||(c>='a'&&c<='z')); //For characters string eg:- a+b*c....
return((c>=48)&&(c<=57)); //For int strings eg: 3+5*8......
}
int canbepushed(stack* top,char optr)
{
if(!top)
return 1;
if(optr=='(')
return 1;
if(optr==')')
return 0;
if(top->optr=='(')
return 1;
switch(optr)
{
case '*':
case '/':if(top->optr=='+'||top->optr=='-')
return 1;
}
return 0;
}
stack* makenode(char optr)
{
stack *nn=(stack*)malloc(sizeof(stack));
nn->optr=optr;
nn->next=NULL;
return nn;
}
stack* push(stack* top,stack* nn)
{
nn->next=top;
return nn;
}
stack* pop(stack *top)
{
stack *tmp;
if(!top)
return top;
tmp=top;
top=top->next;
free(tmp);
return top;
}
void exp(char *pofx)
{ /* Main Program */
char ch;
int i=0,op1,op2;
while( (ch=pofx[i++]) != '\0')
{
if(isdigit(ch)) pushe(ch-'0'); /* Push the operand */
else
{ /* Operator,pop two operands */
op2=pope();
op1=pope();
switch(ch)
{
case '+':pushe(op1+op2);break;
case '-':pushe(op1-op2);break;
case '*':pushe(op1*op2);break;
case '/':pushe(op1/op2);break;
}
}
}
printf("\nResult after Evaluation: %d\n",s[top]);
}
void main()
{
int i,j;
char infix[50],postfix[50];
stack *top=NULL,*nn;
printf("\nEnter the expression [Infix Mode]:");
gets(infix);
for(i=j=0;infix[i];i++)
{
if(isoperand(infix[i]))
postfix[j++]=infix[i];
else
{
while(!canbepushed(top,infix[i]))
{
if(top->optr=='(')
{
top=pop(top);
break;
}
postfix[j++]=top->optr;
top=pop(top);
}
if(infix[i]!=')')
{
nn=makenode(infix[i]);
top=push(top,nn);
}
}
}
while(top)
{
postfix[j++]=top->optr;
top=pop(top);
}
postfix[j]='\0';
printf("\nPostfix:%s",postfix);
exp(postfix);
getch();
}
#include<conio.h>
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
int s[SIZE];
int top=-1; /* Global declarations */
pushe(int elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}
int pope()
{ /* Function for POP operation */
return(s[top--]);
}
struct stack
{
char optr;
struct stack *next;
};
typedef struct stack stack;
int isoperand(char c)
{
// return((c>='A'&&c<='Z')||(c>='a'&&c<='z')); //For characters string eg:- a+b*c....
return((c>=48)&&(c<=57)); //For int strings eg: 3+5*8......
}
int canbepushed(stack* top,char optr)
{
if(!top)
return 1;
if(optr=='(')
return 1;
if(optr==')')
return 0;
if(top->optr=='(')
return 1;
switch(optr)
{
case '*':
case '/':if(top->optr=='+'||top->optr=='-')
return 1;
}
return 0;
}
stack* makenode(char optr)
{
stack *nn=(stack*)malloc(sizeof(stack));
nn->optr=optr;
nn->next=NULL;
return nn;
}
stack* push(stack* top,stack* nn)
{
nn->next=top;
return nn;
}
stack* pop(stack *top)
{
stack *tmp;
if(!top)
return top;
tmp=top;
top=top->next;
free(tmp);
return top;
}
void exp(char *pofx)
{ /* Main Program */
char ch;
int i=0,op1,op2;
while( (ch=pofx[i++]) != '\0')
{
if(isdigit(ch)) pushe(ch-'0'); /* Push the operand */
else
{ /* Operator,pop two operands */
op2=pope();
op1=pope();
switch(ch)
{
case '+':pushe(op1+op2);break;
case '-':pushe(op1-op2);break;
case '*':pushe(op1*op2);break;
case '/':pushe(op1/op2);break;
}
}
}
printf("\nResult after Evaluation: %d\n",s[top]);
}
void main()
{
int i,j;
char infix[50],postfix[50];
stack *top=NULL,*nn;
printf("\nEnter the expression [Infix Mode]:");
gets(infix);
for(i=j=0;infix[i];i++)
{
if(isoperand(infix[i]))
postfix[j++]=infix[i];
else
{
while(!canbepushed(top,infix[i]))
{
if(top->optr=='(')
{
top=pop(top);
break;
}
postfix[j++]=top->optr;
top=pop(top);
}
if(infix[i]!=')')
{
nn=makenode(infix[i]);
top=push(top,nn);
}
}
}
while(top)
{
postfix[j++]=top->optr;
top=pop(top);
}
postfix[j]='\0';
printf("\nPostfix:%s",postfix);
exp(postfix);
getch();
}
No comments:
Post a Comment