Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2009 12:18
Show Gist options
  • Save anonymous/142773 to your computer and use it in GitHub Desktop.
Save anonymous/142773 to your computer and use it in GitHub Desktop.
#include<stdio.h>
char stack[51];
char c,p;
int order(char);
int isOperator(char);
int main()
{
int i,n,j;
scanf("%d",&n);
getchar();
getchar();
for(i=0;i<n;++i)
{
if(i>0)
putchar('\n');
j = 0;
while(1)
{
c = getchar();
if(c == '\n' || c == -1)
break;
if(c == '(')
stack[++j] = c;
if(isdigit(c))
putchar(c);
if(c == ')')
{
while(stack[j]!='(')
{
putchar(stack[j]);
j--;
}
j--;
}
if(isOperator(c))
{
if(j > 0)
while(order(c) <= order(stack[j]) && j)
{
putchar(stack[j]);
j--;
}
stack[++j] = c;
}
getchar();
}
while(j)
putchar(stack[j--]);
putchar('\n');
}
return 0;
}
int order(char a)
{
switch(a)
{
case '+':
case '-':
return 3;
break;
case '*':
case '/':
return 4;
break;
case '(':
return 0;
break;
}
}
int isOperator(char a)
{
if(a == '+' || a == '-' || a == '*' || a == '/')
return 1;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment