Pages

Wednesday, October 21, 2009

Source code for Data Structure Section (Infix to Postfix Notation) code in 'c'....


#include"iostream.h"
#include"conio.h"
#include"stdio.h"
#include"math.h"
char ch[20],eq[20];
char top;
int x,y,z;
int prcd(char,char);
void main()
{
char st[30];
top='+';
clrscr();
cout<<"ENTER AN INFIX EQUATION : ";
gets(st);
x=y=0;
z=-1;
while(st[x]!='\0')
    {
    if((st[x]>64&&st[x]<92)||(st[x]>96&&st[x]<123))
        ch[y++]=st[x];
    else if(st[x]==')')
        {
        while(eq[z]!='(')
            ch[y++]=eq[z--];
        top=eq[--z];
        }
    else
        {
        while(z>=0&&prcd(top,st[x])==1)
            {
            ch[y++]=top;
            top=eq[--z];
            }
        eq[++z]=st[x];
        top=st[x];
        }
        x++;
        }
        while(z>=0)
            {
            if(eq[z]=='(')
                z--;
            else
                ch[y++]=eq[z--];
            }
    ch[y]='\0';
    cout<<<<"THE POSTFIX FORM IS = ";
    for(x=0;x
        cout<
    getch();

}

int prcd(char g, char r)
{
if(r=='('||r=='$')
    return(0);
else if(g=='$'||g=='*'||g=='/')
    return(1);
else if(r=='*'||g=='('||r=='/')
    return(0);
else
    return(1);
}





No comments:

Post a Comment