/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/
/**
* program for infix to postfix without bracket
*/
#include<stdio.h>
#define MAX 100
void main()
{
int TOP = -1, PTOP = 0;
char s[MAX];
char post[MAX] = {'\0'};
int pre(char);
int push(char[], int, char);
int pop(char *, int *);
char c[MAX] = {'\0'}, symb, x;
int i;
printf("Enter expression: ");
gets(c);
/*
* gets will give error when compiling on modern compiler
*/
for (i = 0; c[i] != '\0'; i++) // NULL will give error on iso compiler
{
symb = c[i];
if (isalpha(symb) > 0)
{
post[PTOP] = symb;
PTOP++;
}
else
{
while ((pre(symb)) <= (pre(s[TOP])) && TOP >= 0)
{
x = pop(s, &TOP);
post[PTOP] = x;
PTOP++;
}
TOP = push(s, TOP, symb);
}
}
while (TOP >= 0)
{
x = pop(s, &TOP);
post[PTOP] = x;
PTOP++;
}
post[PTOP] = '\0';
printf("\nPostfix of entered (infix) expression is:\n %s\n", post);
// getch(); // win not work on linux
}
int push(char *s, int top, char ele)
{
int i;
if (top >= MAX)
{
printf("\nStack Overflow");
}
else
{
s[++top] = ele;
}
return top;
}
int pop(char *a, int *top)
{
if ((*top) >= 0)
{
(*top) = (*top) - 1;
return a[(*top) + 1];
}
else
{
printf("Stack underflow\n");
return 0;
}
}
int pre(char x)
{
int a;
switch (x)
{
case '+':
case '-':
a = 2;
break;
case '/':
case '*':
case '\\':
case '%':
a = 5;
break;
case '$':
case '^':
a = 10;
break;
}
return a;
}
Showing posts with label infix to postfix without bracket. Show all posts
Showing posts with label infix to postfix without bracket. Show all posts
Monday, January 10, 2011
infix to postfix without bracket
infix to postfix without bracket
/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/
/**
* program for infix to postfix without bracket
*/
#include<stdio.h>
#define MAX 100
void main()
{
int TOP = -1, PTOP = 0;
char s[MAX];
char post[MAX] = {'\0'};
int pre(char);
int push(char[], int, char);
int pop(char *, int *);
char c[MAX] = {'\0'}, symb, x;
int i;
printf("Enter expression: ");
gets(c);
/*
* gets will give error when compiling on modern compiler
*/
for (i = 0; c[i] != '\0'; i++) // NULL will give error on iso compiler
{
symb = c[i];
if (isalpha(symb) > 0)
{
post[PTOP] = symb;
PTOP++;
}
else
{
while ((pre(symb)) <= (pre(s[TOP])) && TOP >= 0)
{
x = pop(s, &TOP);
post[PTOP] = x;
PTOP++;
}
TOP = push(s, TOP, symb);
}
}
while (TOP >= 0)
{
x = pop(s, &TOP);
post[PTOP] = x;
PTOP++;
}
post[PTOP] = '\0';
printf("\nPostfix of entered (infix) expression is:\n %s\n", post);
// getch(); // win not work on linux
}
int push(char *s, int top, char ele)
{
int i;
if (top >= MAX)
{
printf("\nStack Overflow");
}
else
{
s[++top] = ele;
}
return top;
}
int pop(char *a, int *top)
{
if ((*top) >= 0)
{
(*top) = (*top) - 1;
return a[(*top) + 1];
}
else
{
printf("Stack underflow\n");
return 0;
}
}
int pre(char x)
{
int a;
switch (x)
{
case '+':
case '-':
a = 2;
break;
case '/':
case '*':
case '\\':
case '%':
a = 5;
break;
case '$':
case '^':
a = 10;
break;
}
return a;
}
Subscribe to:
Posts (Atom)