#include <stdio.h>
#include <ctype.h>
int top_p = 0;
int top_s = 0;
void
push_p(char *s, int c)
{
s[top_p] = c;
top_p++;
}
void
push_s(char *s, int c)
{
s[top_s] = c;
top_s++;
}
int
pop_p(char *s)
{
top_p--;
return s[top_p];
}
int
pop_s(char *s)
{
top_s--;
return s[top_s];
}
void
infix_to_postfix(char *in, char *post, char *stk)
{
char operator[256] = {0};
operator['('] = 0;
operator['+'] = 1;
operator['-'] = 1;
operator['*'] = 2;
operator['/'] = 2;
while (*in) {
push_p(post, *in);
} else { /* Operator */
while (1) {
switch (*in) {
case '(':
push_s(stk, *in);
break;
case ')':
while (stk[top_s - 1] != '(') {
push_p(post, pop_s(stk));
}
pop_s(stk);
break;
default:
break;
}
if (*in == '(' && *in == ')') {
break;
}
/* go here */
if (!stk[top_s - 1]) {
while (operator[stk[top_s - 1]] >= operator[*in]) {
push_p(post, pop_s(stk));
if (stk[top_s - 1]) {
break;
}
}
}
break;
}
if (*in != '(' && *in != ')') {
push_s(stk, *in);
}
}
in++;
}
}
void
stack_empty(char *post, char *stk)
{
while (stk[top_s - 1]) {
push_p(post, pop_s(stk));
}
}
int
main(int argc, char **argv)
{
char infix[] = "2*((3-5)*2)";
char postfix[256] = {0};
char stack[256] = {0};
infix_to_postfix(infix, postfix, stack);
stack_empty(postfix, stack);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjdHlwZS5oPgoKaW50IHRvcF9wID0gMDsKaW50IHRvcF9zID0gMDsKCnZvaWQKcHVzaF9wKGNoYXIgKnMsIGludCBjKQp7CiAgICBzW3RvcF9wXSA9IGM7CiAgICB0b3BfcCsrOwp9Cgp2b2lkCnB1c2hfcyhjaGFyICpzLCBpbnQgYykKewogICAgc1t0b3Bfc10gPSBjOwogICAgdG9wX3MrKzsKfQoKaW50CnBvcF9wKGNoYXIgKnMpCnsKICAgIHRvcF9wLS07CiAgICByZXR1cm4gc1t0b3BfcF07Cn0KCmludApwb3BfcyhjaGFyICpzKQp7CiAgICB0b3Bfcy0tOwogICAgcmV0dXJuIHNbdG9wX3NdOwp9Cgp2b2lkCmluZml4X3RvX3Bvc3RmaXgoY2hhciAqaW4sIGNoYXIgKnBvc3QsIGNoYXIgKnN0aykKewogICAgY2hhciBvcGVyYXRvclsyNTZdID0gezB9OwoKICAgIG9wZXJhdG9yWycoJ10gPSAwOwogICAgb3BlcmF0b3JbJysnXSA9IDE7CiAgICBvcGVyYXRvclsnLSddID0gMTsKICAgIG9wZXJhdG9yWycqJ10gPSAyOwogICAgb3BlcmF0b3JbJy8nXSA9IDI7CiAgICB3aGlsZSAoKmluKSB7CiAgICAgICAgaWYgKGlzZGlnaXQoKmluKSkgewogICAgICAgICAgICBwdXNoX3AocG9zdCwgKmluKTsKICAgICAgICB9IGVsc2UgeyAvKiBPcGVyYXRvciAqLwogICAgICAgICAgICB3aGlsZSAoMSkgewogICAgICAgICAgICAgICAgc3dpdGNoICgqaW4pIHsKICAgICAgICAgICAgICAgICAgICBjYXNlICcoJzoKICAgICAgICAgICAgICAgICAgICAgICAgcHVzaF9zKHN0aywgKmluKTsKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgY2FzZSAnKSc6CiAgICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIChzdGtbdG9wX3MgLSAxXSAhPSAnKCcpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHB1c2hfcChwb3N0LCBwb3BfcyhzdGspKTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBwb3BfcyhzdGspOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmICgqaW4gPT0gJygnICYmICppbiA9PSAnKScpIHsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KLyogZ28gaGVyZSAqLwogICAgICAgICAgICAgICAgaWYgKCFzdGtbdG9wX3MgLSAxXSkgewogICAgICAgICAgICAgICAgICAgIHdoaWxlIChvcGVyYXRvcltzdGtbdG9wX3MgLSAxXV0gPj0gb3BlcmF0b3JbKmluXSkgewogICAgICAgICAgICAgICAgICAgICAgICBwdXNoX3AocG9zdCwgcG9wX3Moc3RrKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzdGtbdG9wX3MgLSAxXSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoKmluICE9ICcoJyAmJiAqaW4gIT0gJyknKSB7CiAgICAgICAgICAgICAgICBwdXNoX3Moc3RrLCAqaW4pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGluKys7CiAgICB9Cn0KCnZvaWQKc3RhY2tfZW1wdHkoY2hhciAqcG9zdCwgY2hhciAqc3RrKQp7CiAgICB3aGlsZSAoc3RrW3RvcF9zIC0gMV0pIHsKICAgICAgICBwdXNoX3AocG9zdCwgcG9wX3Moc3RrKSk7CiAgICB9Cn0KCmludAptYWluKGludCBhcmdjLCBjaGFyICoqYXJndikKewogICAgY2hhciBpbmZpeFtdID0gIjIqKCgzLTUpKjIpIjsKICAgIGNoYXIgcG9zdGZpeFsyNTZdID0gezB9OwogICAgY2hhciBzdGFja1syNTZdID0gezB9OwoKICAgIGluZml4X3RvX3Bvc3RmaXgoaW5maXgsIHBvc3RmaXgsIHN0YWNrKTsKICAgIHN0YWNrX2VtcHR5KHBvc3RmaXgsIHN0YWNrKTsKICAgIHByaW50ZigiJXNcbiIsIHBvc3RmaXgpOwogICAgcmV0dXJuIDA7Cn0=