#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int precedence(char c)
{
switch(c)
{
case '*':
case '/':
return(2);
case '+':
case '-':
return(0);
}
}
char *to_postfix(char *infix)
{
char stack[30],postfix[30];
int i=0,j=0,top=-1;
while(infix[i]!=NULL)
{
if(isalpha(infix[i])||isdigit(infix[i]))
{
postfix[j++]=infix[i++];
}
else if(infix[i]=='(')
{
stack[++top]=infix[i++];
}
else if(infix[i]==')')
{
while(stack[top]!='(')
{
postfix[j++]=stack[top--];
}
top--;
i++;
}
else
{
while(top!=-1&&stack[top]!='('&&precedence(infix[i])<precedence(stack[top]))
{
postfix[j++]=stack[top--];
}
stack[++top]=infix[i++];
}
}
while(top>-1)
{
postfix[j++]=stack[top--];
}
postfix[j]=NULL;
return(postfix);
}
void main()
{
char infix[30];
gets(infix);
puts(to_postfix(infix));
}
abrarsyed
0
Newbie Poster
Recommended Answers
Jump to PostFirst off, stop using the SMS abbreviations; no one will take you seriously here if you insist on that sort of thing. Write out clear, complete English sentences, to the best of your ability, please.
Second, while I know it is a picayune point to make,
void main()
is …
Jump to PostWith a bit of extra work, I think you'll find this solves (most) of the issues with your code:
#include<stdio.h> #include<ctype.h> #include<string.h> int precedence(char c) { switch(c) { case '*': case '/': return(2); case '+': case '-': return(0); } } void to_postfix(char *infix, char* postfix) { char …
All 6 Replies
abrarsyed
0
Newbie Poster
Schol-R-LEA
1,446
Commie Mutant Traitor
Featured Poster
Schol-R-LEA
1,446
Commie Mutant Traitor
Featured Poster
abrarsyed
0
Newbie Poster
Trentacle
112
Junior Poster in Training
abrarsyed
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.