program for infix expression to post fix expression
10:49 PM
/*program to calculate the given infix expression to post fix expression
Auther :Suman Thapa
Roll No.0215
Sec: A
Practical Sheet 2
Date:2013//07/07*//
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define STACKSIZE 20
struct stack
{
int top;
char items[STACKSIZE];
};
int precedence (char c);
char pop (struct stack *s);
void push (struct stack *s,char data);
int isOperand(char c);
int emptyStack(struct stack *s);
int main (void)
{
char a = '*';
printf("%d",a);
char postfix[8];
char infix[8];
int i = 0,j=0 ,n;
char symbol , topSymbol, temp;
struct stack oprstk;
printf("Enter the expression:");
scanf("%s",infix);
oprstk.top = -1;
oprstk.items[oprstk.top++] = '#';
while (infix[i] != '\0')
{
symbol = infix[i];
//printf("\nsymbol : %c",symbol);
if (isOperand(symbol) == 1)
{
postfix[j++] = symbol;
}
else
{
while (precedence(oprstk.items[oprstk.top] ) >= precedence(symbol))
{
topSymbol = pop(&oprstk);
postfix[j++] = topSymbol;
} // end of while
push(&oprstk,symbol);
} // end of else
i++;
} // end of outer while
for (i=0;oprstk.top!=0;i++)
{
postfix[j++] = pop(&oprstk);
}
postfix[j] = '\0';
for (i=0;postfix[i] != '\0';i++)
printf("%c",postfix[i]);
return 0;
}
void push (struct stack *s,char data)
{
s->top++;
s->items[s->top] = data;
}
char pop (struct stack *s)
{
return (s->items[(s->top)--]);
}
int isOperand (char c)
{
if ((c >= 'a' && c<= 'z') || ( c >= 'A' && c <= 'Z'))
return 1;
return 0;
}
int precedence (char c)
{
switch (c)
{
case '$':
return 3;
case '/':
case '*':
return 2;
case '+':
case '-':
return 1;
case '#':
return 0;
}
}
int emptyStack(struct stack *s)
{
if (s->top == -1)
return 1;
return 0;
}
Auther :Suman Thapa
Roll No.0215
Sec: A
Practical Sheet 2
Date:2013//07/07*//
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define STACKSIZE 20
struct stack
{
int top;
char items[STACKSIZE];
};
int precedence (char c);
char pop (struct stack *s);
void push (struct stack *s,char data);
int isOperand(char c);
int emptyStack(struct stack *s);
int main (void)
{
char a = '*';
printf("%d",a);
char postfix[8];
char infix[8];
int i = 0,j=0 ,n;
char symbol , topSymbol, temp;
struct stack oprstk;
printf("Enter the expression:");
scanf("%s",infix);
oprstk.top = -1;
oprstk.items[oprstk.top++] = '#';
while (infix[i] != '\0')
{
symbol = infix[i];
//printf("\nsymbol : %c",symbol);
if (isOperand(symbol) == 1)
{
postfix[j++] = symbol;
}
else
{
while (precedence(oprstk.items[oprstk.top] ) >= precedence(symbol))
{
topSymbol = pop(&oprstk);
postfix[j++] = topSymbol;
} // end of while
push(&oprstk,symbol);
} // end of else
i++;
} // end of outer while
for (i=0;oprstk.top!=0;i++)
{
postfix[j++] = pop(&oprstk);
}
postfix[j] = '\0';
for (i=0;postfix[i] != '\0';i++)
printf("%c",postfix[i]);
return 0;
}
void push (struct stack *s,char data)
{
s->top++;
s->items[s->top] = data;
}
char pop (struct stack *s)
{
return (s->items[(s->top)--]);
}
int isOperand (char c)
{
if ((c >= 'a' && c<= 'z') || ( c >= 'A' && c <= 'Z'))
return 1;
return 0;
}
int precedence (char c)
{
switch (c)
{
case '$':
return 3;
case '/':
case '*':
return 2;
case '+':
case '-':
return 1;
case '#':
return 0;
}
}
int emptyStack(struct stack *s)
{
if (s->top == -1)
return 1;
return 0;
}
0 comments: