#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
static char str[30];
int top=-1;
int priority(char c);
void push(char c);
int pop();
void main()
{
char in[30],post[30],ch;
int i,j,l;
clrscr();
printf("enter the string");
gets(in);
l=strlen(in);
printf("%d\n",l);
for(i=0,j=0;i<l;i++)
{
if(isalpha(in[i]))
post[j++]=in[i];
else
{
if(in[i]=='(')
push(in[i]);
else if(in[i]==')')
while((ch=pop())!='(')
post[j++]=ch;
else
{
while(priority(in[i])<=priority(str[top]))
post[j++]=pop();
push(in[i]);
} // End else
} // End else
} // End for loop
while(top!=-1)
post[j++]=pop();
post[j]='\0';
printf("\n equivalent infix to postfix is:%s",post);
getch();
} //End main
int priority (char c)
{
switch(c)
{
case'+':
case'-': return 1;
case'*':
case'/': return 2;
case '^':return 3;
}
return 0;
}
void push(char c)
{
str[++top]=c;
}
int pop()
{
return(str[top--]);
}
#include<ctype.h>
#include<string.h>
#include<conio.h>
static char str[30];
int top=-1;
int priority(char c);
void push(char c);
int pop();
void main()
{
char in[30],post[30],ch;
int i,j,l;
clrscr();
printf("enter the string");
gets(in);
l=strlen(in);
printf("%d\n",l);
for(i=0,j=0;i<l;i++)
{
if(isalpha(in[i]))
post[j++]=in[i];
else
{
if(in[i]=='(')
push(in[i]);
else if(in[i]==')')
while((ch=pop())!='(')
post[j++]=ch;
else
{
while(priority(in[i])<=priority(str[top]))
post[j++]=pop();
push(in[i]);
} // End else
} // End else
} // End for loop
while(top!=-1)
post[j++]=pop();
post[j]='\0';
printf("\n equivalent infix to postfix is:%s",post);
getch();
} //End main
int priority (char c)
{
switch(c)
{
case'+':
case'-': return 1;
case'*':
case'/': return 2;
case '^':return 3;
}
return 0;
}
void push(char c)
{
str[++top]=c;
}
int pop()
{
return(str[top--]);
}
No comments:
Post a Comment